# I have this for loop, and it seems to not end.

조회 수: 1(최근 30일)
Josiah Jarenee Comia 2021년 5월 13일
편집: Pranav Verma 2021년 5월 17일
for i=1:n
Phi1=1;
Phi2=1;
gamma1calc(i)=exp(alpha/(1+(alpha/beta)*(x1(i)/x2(i)))^2);
gamma2calc(i)=exp(beta/(1+(beta/alpha)*(x2(i)/x1(i)))^2);
Pcalc(i) = ((x1(i)*gamma1calc(i)*P1sat)/(Phi1))+((x2(i)*gamma2calc(i)*P2sat)/(Phi2));
AE1(i)=1;
while AE1(i) > 0.0001
y1calc(i) = (x1(i)*gamma1calc(i)*P1sat)/(Phi1);
y2calc(i) = 1-y1calc(i);
Phi1calc(i)=exp((B1.*(Pcalc(i)-P1sat)+Pcalc(i).*y2calc(i).^2.*(2.*B12-B1-B2))./(R*T));
Phi2calc(i)=exp((B2.*(Pcalc(i)-P2sat)+Pcalc(i).*y1calc(i).^2.*(2.*B12-B1-B2))./(R*T));
Pnew(i) = ((x1(i)*gamma1calc(i)*P1sat)/Phi1calc(i))+((x2(i)*gamma2calc(i)*P2sat)/Phi2calc(i));
AE2 (i)=abs(Pnew(i)-Pcalc(i));
if AE2(i) > 0.0001
Pcalc(i)=Pnew(i);
end
end
Pcalc(i)=Pnew(i);
end
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Stephen 2021년 5월 13일
"This should work right?"
You did not describe the intended algorithm, so I have no way to check if it is implemented correctly or should "work".
"I mean the loop structure is correct and all right?"
That looks like the correct syntax for a WHILE loop.

댓글을 달려면 로그인하십시오.

### 답변(1개)

Pranav Verma 2021년 5월 17일
편집: Pranav Verma 2021년 5월 17일
Hi Josiah,
I would recommend you to print out the value of AE1(i) inside the loop along with appropriate messages. This will allow you to watch the value of AE1(i) and debug the code easily.
You can use disp function to print out the value and messages.
Thanks

댓글을 달려면 로그인하십시오.

R2019b

### Community Treasure Hunt

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

Start Hunting!