필터 지우기
필터 지우기

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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by