Printing a variable inside a while loop

1 回表示 (過去 30 日間)
Natalia Oñate Gomez
Natalia Oñate Gomez 2020 年 4 月 29 日
編集済み: Ameer Hamza 2020 年 4 月 30 日
I'm trying to print rt, which is the time when the voltage reaches 27.4643, but it won't print anything. I think there's a problem inside the while or there's something wrong with the if. Basically, how can I make it print rt?
t=[0:0.5:100];
while (Vr>Vf)
V=Vo*exp(-t/(R*C));
I=Io*exp(-t./(R*C));
Q=Qo*exp(-t./(R*C));
U=Uo*exp(-2*t./(R*C));
Vr=V;
if(V==27.4643)
rt=-R*C*(log(V/Vo)); %ecuación diferencial despejada para obtener tiempo
disp(rt)
end
end

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 4 月 29 日
編集済み: Ameer Hamza 2020 年 4 月 30 日
The value will probably never become exactly equal to 27.4643. You need to define some tolerance limits for this comparison.
if(abs(V-27.4643)<0.5)
If the value of V is within 0.5 of 27.4643, the condition will become true. Adjust the tolerance values according to your requirement.
  2 件のコメント
Natalia Oñate Gomez
Natalia Oñate Gomez 2020 年 4 月 29 日
Thank you, that was really useful.
Ameer Hamza
Ameer Hamza 2020 年 4 月 30 日
I am glad to be of help.

サインインしてコメントする。

カテゴリ

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