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

1 view (last 30 days)
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 Comments
Josiah Jarenee Comia
Josiah Jarenee Comia on 13 May 2021
My bad, I entered the wrong code, I just tested that because im not getting anything. Here's the original. This should work right? I mean the loop structure is correct and all right?
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));
AE1(i)=abs(Pnew(i)-Pcalc(i));
if AE1(i) > 0.0001
Pcalc(i)=Pnew(i);
end
end
Pcalc(i)=Pnew(i);
end
Stephen23
Stephen23 on 13 May 2021
"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.

Sign in to comment.

Answers (1)

Pranav Verma
Pranav Verma on 17 May 2021
Edited: Pranav Verma on 17 May 2021
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!