How to select one number from a loop. I want to calculate the time t when m5=99.9. when I run the program it doesn't show anything. I don't know how to print t from the time loop when m5=99.99

1 回表示 (過去 30 日間)
dt = 0.01; % Step size
tend = 50; % End time
t = 0:dt:tend; % Time vector
%Loop for m5 calculation
for i=1:(length(t)-1)
for j=1:length(i)
k1 = f5(t(i),m5(i));
k2 = f5(t(i)+0.5*dt,m5(i)+0.5*dt*k1);
k3 = f5((t(i)+0.5*dt),(m5(i)+0.5*dt*k2));
k4 = f5((t(i)+dt),(m5(i)+k3*dt));
end
m5(i+1) = m5(i) + (1/6)*(k1(j)+2*k2(j)+2*k3(j)+k4(j)).*dt;
if m5==99.9
break
fprintf('%d',t(i))
end

採用された回答

the cyclist
the cyclist 2018 年 11 月 8 日
編集済み: the cyclist 2018 年 11 月 8 日
The reason you don't an exact match has to do with the representation of floating point numbers.
Instead of testing with
if m5==99.9
try
tol = 1.e-5;
if abs(m5-99.9) < tol

その他の回答 (0 件)

カテゴリ

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