Info

This question is locked. 編集または回答するには再度開いてください。

loop while loop for

3 ビュー (過去 30 日間)
Matthew
Matthew 2024 年 4 月 20 日
Locked: Rena Berman 2024 年 6 月 5 日
i'm trying to get this while loop to quite based on the rate of change of v being less than .05%.
the while loop quits now just based on t(i)<7; but i'm trying to figure out how i can get this loop to stop based on ((v(2)-v(1))/(t(2)-t(1))<= 0.05.
clc
clear
m = 36/1000;
g=9.81;
T=5;
A=.00044316;
Cd=0.75;
rho=1.203;
a(1)=0;
v(1)=0;
h(1)=0;
t(1)=0;
t_step=0.001;
i=1;
u(1)=0;
while t(i)<7;
a(i+1)= ((T)-(m*g)-(0.5*rho*A*Cd*v(i)^2))/m;
v(i+1)= a(i+1)*t_step+v(i);
h(i+1)= v(i+1)*t_step+h(i);
t(i+1)=t(i)+t_step;
i=i+1;
end
plot(t,v)
  2 件のコメント
John D'Errico
John D'Errico 2024 年 4 月 21 日
編集済み: John D'Errico 2024 年 4 月 21 日
iF you will remove your question once you get an answer, I'd ask you not to ask a question at all. When you remove the question, you hurt the site, because the answer is no longer meaningful. It does not allow anyone else to learn from your question and this answer. You make it less likely that your next question will find someone willing to spend the time to answer your questions.
If you think you are not supposed to ask a question like this, because your teacher would not want you to do so, then WHY DID YOU ASK THE QUESTION IN THE FIRST PLACE?
Rena Berman
Rena Berman 2024 年 6 月 5 日

(Answers Dev) Restored edit

採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2024 年 4 月 20 日
I suppose that what you are trying to get is dv/dt >=0.05. Here is how you can get it done:
clc; clearvars;
m = 36/1000;
g=9.81;
T=5;
A=.00044316;
Cd=0.75;
rho=1.203;
a(1)=0;
v(1)=0;
h(1)=0;
t(1)=0;
t_step=0.001;
i=1;
u(1)=0;
dvdt = 1;
while dvdt>=0.05
a(i+1)= ((T)-(m*g)-(0.5*rho*A*Cd*v(i)^2))/m;
v(i+1)= a(i+1)*t_step+v(i);
h(i+1)= v(i+1)*t_step+h(i);
t(i+1)=t(i)+t_step;
dvdt = (v(i+1)-v(i))/(t(i+1)-t(i));
i=i+1;
end
plot(t,v, 'r-.o')
grid on
text(0.5, 10, ['The simulation is halted after: ' num2str(i) ' iterations and ' num2str(t(end)) ' [seconds]'], 'backgroundcolor', 'w')
xlabel('Time, [s]')
ylabel('Velocity, [m/s]')
  1 件のコメント
Matthew
Matthew 2024 年 4 月 20 日
Thank you, I appreciate the help.

その他の回答 (0 件)

This question is locked.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by