Effacer les filtres
Effacer les filtres

Iteration in Matlab

2 vues (au cours des 30 derniers jours)
Thomas Humphrey
Thomas Humphrey le 21 Nov 2011
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

Réponse acceptée

Sven
Sven le 21 Nov 2011
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 commentaires
Thomas Humphrey
Thomas Humphrey le 21 Nov 2011
got it to work by adding an error function instead of a==b.
Thanks a lot for the help
Sven
Sven le 21 Nov 2011
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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by