필터 지우기
필터 지우기

Matlab help regarding code

조회 수: 2 (최근 30일)
Nasir Qazi
Nasir Qazi 2012년 2월 11일
편집: dav 2013년 10월 9일
% Calculate the vapor Pressure of n-heptane using SRK-EOS
% P = RT / V - b - alpha*a / V(V-b)
R = 8.134e-3; % MPa m3 / mole.K
n = 0;
n = n+1;
for i=1:5
Pn = 1.6714;
Tr = 0.9;
Tc = 540.13; % K
Pc = 2.74; % Mpa;
T = Tr*Tc;
w = 0.350;
m = 0.480+1.574*w -0.176*w^2;
al=(1+m*(1-Tr^0.5))^2;
a = 0.42748*(((R*Tc)^2) /Pc);
b = 0.08664*(R*Tc / Pc);
% Z3 - Z2 + (A-B-B^2)Z-AB=0
A = (a*Pn*al)/(R*T)^2;
B = (b*Pn)/(R*T);
Zv=max(roots([1 -1 A-B-B^2 -A*B]));
Zl=min(roots([1 -1 A-B-B^2 -A*B]));
disp(Zv)
disp(Zl)
phiv(i)=exp((Zv -1)-log(Zv -B)-(A/B) *(log(Zv+B/Zv )));
phil(i)=exp((Zl -1)-log(Zl -B)-(A/B) *(log(Zl+B/Zl )));
Fv=Pn*phiv;
Fl=Pn*phil;
disp(phiv)
disp(phil);
disp(Fv);
disp(Fl);
A=Fl-Fv;
if (A < 10^-4);
Pnew=Pn;
else
Pn+1 =Pn * (phil/phiv);
end
end
Basically in this code I am trying to assume a pressure 'Pn' to calculate the results which satisfy this relation (A<10^-4) if this satisfy's then our assume pressure is right else if not then we use the relation Pn+1 = Pn*(Phil/Phiv) to caluclate another pressure and update the above pressure (previously assume) with this one. I have error with this and need advice please help.
regards

답변 (1개)

Walter Roberson
Walter Roberson 2012년 2월 11일
The syntax
Pn+1 =Pn * (phil/phiv);
is not valid. The left side of an assignment must be a valid variable name or array reference. For example,
Pn_plus_1 =Pn * (phil/phiv);
Or
P(n+1) =Pn * (phil/phiv);
In your "while" loop, you do not recalculate "A", so if the loop is entered at all, it is going to be repeated indefinitely. When you assign a variable value, the assignment does not affect any values that were already calculated before that point, including not any assignments that used that same variable name in the formula.
foo = 5;
bar = foo;
foo = 7;
bar
At the end of this, foo will be 7 and bar will still be what it was at the time the assignment for it was executed, namely 5. The update to the variable "foo" that was used in initializing "bar" does not cause an update to any variable already assigned to.
  댓글 수: 5
Image Analyst
Image Analyst 2012년 2월 12일
I still don't understand. Maybe you can give a Pn where it does get into that part of the if statement and then bails out of the loop. But I see no reason why it should exit the loop and stop the program. There is no break statement anywhere so why should it exit the loop or "stop there" whatever that means. You mean like stop with an error because an exception got thrown? And how/where is Pnew ever used, even if it did get assigned?
Nasir Qazi
Nasir Qazi 2012년 2월 12일
% these are the correct codes, have a look again
% Calculate the vapor Pressure of n-heptane using SRK-EOS
% P = RT / V - b - alpha*a / V(V-b)
R = 8.134e-3; % MPa m3 / mole.K
n = 0;
n = n+1;
for i=1:5
Pn = 1.6714; % Assume Pressure
Tr = 0.9;
Tc = 540.13; % K
Pc = 2.74; % Mpa;
T = Tr*Tc;
w = 0.350;
m = 0.480+1.574*w -0.176*w^2;
al=(1+m*(1-Tr^0.5))^2;
a = 0.42748*(((R*Tc)^2) /Pc);
b = 0.08664*(R*Tc / Pc);
% Z3 - Z2 + (A-B-B^2)Z-AB=0
A = (a*Pn*al)/(R*T)^2;
B = (b*Pn)/(R*T);
Zv=max(roots([1 -1 A-B-B^2 -A*B]));
Zl=min(roots([1 -1 A-B-B^2 -A*B]));
disp(Zv)
disp(Zl)
phiv(i)=exp((Zv -1)-log(Zv -B)-(A/B) *(log(Zv+B/Zv )));
phil(i)=exp((Zl -1)-log(Zl -B)-(A/B) *(log(Zl+B/Zl )));
Fv=Pn*phiv;
Fl=Pn*phil;
disp(phiv)
disp(phil);
disp(Fv);
disp(Fl);
error=(Fl-Fv);
if (error < 10^-4); % this statement has to be verify if not
Pn; % |
else % |
Pn+1 =Pn * (phil/phiv); % V
P(n+1) = Pn ( this pressure should update the previous assume pressure )
end
end

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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by