Iteration in Matlab
7 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
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 Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!