Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
Debug while statement with iterative
조회 수: 5 (최근 30일)
이전 댓글 표시
I am having problem with the code shown below. My equations are correct. I think there is something I can't see since I am not much on programming. I did a previous problem the exact same way except it was on 4 equations.
%Define Variables This is my initial guess
T1=95;
T2=91;
T3=90;
T4=95;
T5=92;
T6=86;
T7=82;
T8=87;
T9=70;
T10=50;
T11=40;
T12=30;
%Equations and Residuals
R1=(abs(-(4*T1)+T2+2*T4+100));
R2=(abs(T1-(4*T2)+T3+2*T5));
R3=(abs(200*T2-(400*T3)+200*T6+100));
R4=(abs(T1-(4*T4)+T5+T7+100));
R5=(abs(T2+T4-(4*T5)+T6+T8));
R6=(abs(200*T3+400*T5-(800*T6)+200*T9+200));
R7=(abs(T4-(4*T7)+T8++T10)+100);
R8=(abs(T5+T7-(4*T8)+T9+T11));
R9=(abs(200*T6+400*T8+200*T12-(800*T9)+200));
R10=(abs(400*T7-(1800*T10)+200*T11+40000+100));
R11=(abs(400*T8+200*T10+20000-(1800*T11)+200*T12));
R12=(abs(200*T9+200*T11+10000+100-(900*T12)));
Rt=[R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12];
while max(Rt)>0.1
Rm=max(Rt);
if Rt(1)==Rm
T1=(((2*T4+100+T2))/4);
Rt=residi_2(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12);
elseif Rt(2)==Rm
T2=((T1+2*T4+100+T2))/4;
Rt=residi_2(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12);
elseif Rt(3)==Rm
T3=((200*T2+200*T6+100)/400);
Rt=residi_2(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12);
elseif Rt(4)== Rm
T4=((T1+100+T7+T5)/4);
Rt=residi_2(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12);
elseif Rt(5)==Rm
T5=((T2+T4+T8+T6)/4);
Rt=residi_2(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12);
elseif Rt(6)==Rm
T6=((200*T3+400*T5+200*T9+200)/800);
Rt=residi_2(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12);
elseif Rt(7)== Rm
T7=((T4+100+T10+T8)/4);
Rt=residi_2(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12);
elseif Rt(8)==Rm
T8=((T5+T7+T11+T9))/4;
Rt=residi_2(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12);
elseif Rt(9)==Rm
T9=((200*T6+400*T8+200*T12+200)/800);
Rt=residi_2(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12);
elseif Rt(10)== Rm
T10=((100+400*T7+200*T11+20000+20000)/1800);
Rt=residi_2(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12);
elseif Rt(11)==Rm
T11=((400*T8+200*T10+20000+20000+200*T12)/1800);
Rt=residi_2(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12);
elseif Rt(12)==Rm
T12=((200*T9+200*T11+10000+100)/900);
Rt=residi_2(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12);
end
This is my function to keep updating the Residuals equations
function [Rt] = residi_2(T1,T2,T3,T4,T5, T6,T7, T8, T9, T10, T11, T12)
R1=(abs(2*T4+100+T2-(4*T1)));
R2=(abs(T1+2*T5+T3-(4*T2)));
R3=(abs(200*T2+200*T6+100-(400*T3)));
R4=(abs(T1+100+T7+T5-(4*T4)));
R5=(abs(T2+T4+T8+T6-(4*T5)));
R6=(abs(200*T3+400*T5+200*T9-(800*T6)+200));
R7=(abs(T4+100+T10+T8-(4*T7)));
R8=(abs(T5+T7+T11+T9-(4*T8)));
R9=(abs(200*T6+400*T8+200*T12-(800*T9)+200));
R10=(abs(100+400*T7+20000+200*T11-(1800*T10)+20000));
R11=(abs(400*T8+200*T10-(1800*T11)+20000+200*T12));
R12=(abs(200*T9+200*T11+10000+100-(900*T12)));
Rt=[R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12];
return
I do know T1 = 99.88 and T12=29.54. If you can find a mistake you are probably the master of Matlab.
end
댓글 수: 0
답변 (1개)
Roger Stafford
2016년 6월 23일
편집: Roger Stafford
2016년 6월 23일
As far as I can see, your equations are entirely linear, and therefore you can solve them very fast using matrix division. Prepare an appropriate matrix A of coefficients of the various terms, and a vector b of the constants. For example, the first row in A would be
-4 1 0 2 0 0 0 0 0 0 0 0
and the corresponding first value in b would be
-100
This corresponds to the equation -4*T1+1*T2+0*T3+2*T4+0*T5... = -100. When you have the full 12 by 12 matrix A and the 12 by 1 column vector b worked out, just write
T = A\b
and get the result in just one step. This is what matlab is all about!
The method you describe is bound to be highly inefficient and may not work at all. It isn't a matter of making some mistake in computation, it is a matter of selecting the wrong kind of procedure altogether. Even if your equations were not linear, your method would not be appropriate - instead you should use something like 'fsolve' or 'solve'.
댓글 수: 3
Roger Stafford
2016년 6월 23일
“I have to solve these equation in this format” That doesn’t make sense to me, Robert. If I had spent just a few more minutes I could have derived the entire A and b arrays for you. What is so hard about that? What kind of crazy restrictions are you operating under that would make you avoid such an easy method. In any case, this isn’t any reason to use the horrible method you have described. If you positively are not permitted to alter the nature of your twelve equations, you can alway send them directly to ‘fsolve’ essentially as is, or to ‘solve’ for a symbolic solution.
Roger Stafford
2016년 6월 24일
I couldn’t resist solving your equations using matrix division: A\b. I found that you are off on your values for T1 and T12. They should be T1 = 82.54 and T12 = 29.35 (close, but no cigar.)
이 질문은 마감되었습니다.
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!