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

1 次查看(过去 30 天)
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 个评论
Josiah Jarenee Comia
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 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
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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by