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

1 回表示 (過去 30 日間)
Josiah Jarenee Comia 2021 年 5 月 13 日

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 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
Josiah Jarenee Comia 2021 年 5 月 13 日
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 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 日

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 Center および File ExchangeLoops 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