Iteration in Matlab
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
Sven
el 21 de Nov. de 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 comentarios
Sven
el 21 de Nov. de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!