フィルターのクリア

Iteration in Matlab

2 ビュー (過去 30 日間)
Thomas Humphrey
Thomas Humphrey 2011 年 11 月 21 日
Hi, I'm trying to iterate an equation in matlab, but just can't seem to get the answer out no matter what I do and was wondering if anyone could help
the equation that Ive got is
M_f*C_pf*(T_f-T_cw) = M_d*C_pd*(T_d-T_o) + M_b*C_pb*(T_b-T_o)
I am trying to find T_f and T_o, all the other variables I know apart from C_pf which depends on T_f to be calculated.
I currently have this code to be able to calculate the answers for me but every time it just runs to it's maximum limit. I was wondering if anyone could help.
C_pd = Specific_Heat(T_d,X_d);
C_pb = Specific_Heat(T_b,X_b);
for T_f = 50:80
C_pf = Specific_Heat(T_f,X_f);
a = M_f*C_pf*(T_f-T_cw);
for T_o = 25:0.1:60
b = M_d*C_pd*T_d + M_b*C_pb*T_b - T_o*(M_d*C_pd + M_b*C_pb);
if a==b
break
else
continue
end
end
end
Would be very much appreciated. Thank you
Tom

採用された回答

Sven
Sven 2011 年 11 月 21 日
Your "break" command only breaks out of one loop.
Instead try:
exitLoop = false;
C_pd = Specific_Heat(T_d,X_d);
C_pb = Specific_Heat(T_b,X_b);
for T_f = 50:80
if exitLoop, break; end
C_pf = Specific_Heat(T_f,X_f);
a = M_f*C_pf*(T_f-T_cw);
for T_o = 25:0.1:60
b = M_d*C_pd*T_d + M_b*C_pb*T_b - T_o*(M_d*C_pd + M_b*C_pb);
if a==b
exitLoop = true;
break;
end
end
end
  3 件のコメント
Thomas Humphrey
Thomas Humphrey 2011 年 11 月 21 日
got it to work by adding an error function instead of a==b.
Thanks a lot for the help
Sven
Sven 2011 年 11 月 21 日
No problem. Keep in mind that if all of your variables (C_pd, a, etc) are scalars, then I think there might be a simpler way to solve your problem that may not involve loops so much. At least you could consider replacing the inner loop entirely with:
b = M_d*C_pd*T_d + M_b*C_pb*T_b - (25:0.1:60).*(M_d*C_pd + M_b*C_pb);
[minVal, idx] = min(abs(b))
if minVal<yourError, break; end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by