I'm trying to write a function which uses global variables this way:
function F=phi(P)
global Tc Pc T omega equation
[Z,A,B,~,~]=CompressibilityFactor(equation,Tc,Pc,omega,T,P);
ZL=Z(3);
ZV=Z(1);
if equation=="vdw"
logphiL=ZL-1-A/ZL-log(ZL-B);
logphiV=ZV-1-A/ZV-log(ZV-B);
elseif equation=="rk"
logphiL=ZL-1-A/B*log((ZL+B)/ZL)-log(ZL-B);
logphiV=ZV-1-A/B*log((ZV+B)/ZV)-log(ZV-B);
elseif equation=="rks"
logphiL=ZL-1-A/B*log((ZL+B)/ZL)-log(ZL-B);
logphiV=ZV-1-A/B*log((ZV+B)/ZV)-log(ZV-B);
elseif equation=="pr"
logphiL=ZL-1-A/(2*sqrt(2)*B)*log((ZL+B*(1+sqrt(2)))/ZL+B*(1-sqrt(2)))-log(ZL-B);
logphiV=ZV-1-A/(2*sqrt(2)*B)*log((ZV+B*(1+sqrt(2)))/ZV+B*(1-sqrt(2)))-log(ZV-B);
end
F=logphiL/logphiV-1;
end
CompressibilityFactor is another function and I know it works. When I define the global viariables with a script and then run this function I get this error
Error in phi (line 4)
[Z,A,B,~,~]=CompressibilityFactor(equation,Tc,Pc,omega,T,P);
I can't understand why.

8 件のコメント

Alex Mcaulley
Alex Mcaulley 2019 年 3 月 19 日
編集済み: Alex Mcaulley 2019 年 3 月 19 日
CompressibilityFactor function is needed. By the way, more input parameters are needed in this function.
Stephen23
Stephen23 2019 年 3 月 19 日
Note that global variables are the cause of many beginners problems, they are slow, buggy, and hard to debug. You should avoid using them:
Enrico Bussetti
Enrico Bussetti 2019 年 3 月 19 日
function [Z,A,B,S,k]=CompressibilityFactor(equation,Tc,Pc,omega,T,P)
R=8.314472; % R[j/mol*K]
Tr=T/Tc;
validateattributes(equation,{'string'},{'nonempty'})
validateattributes(Tc,{'numeric'},{'>',0,'nonempty'})
validateattributes(Pc,{'numeric'},{'>',0,'nonempty'})
validateattributes(T,{'numeric'},{'>',0,'nonempty'})
validateattributes(P,{'numeric'},{'>',0,'nonempty'})
validateattributes(omega,{'numeric'},{'>=',0})
if equation=="vdw"
a=(0.421875*(R*Tc)^2)/Pc;
b=(0.125*R*Tc)/Pc;
u=0;
w=0;
elseif equation=="rk"
k=1/Tr^0.5;
a=0.42748*(R*Tc)^2*k/Pc;
b=0.08664*R*Tc/Pc;
u=1;
w=0;
elseif equation=="rks"
S=0.48+1.574*omega-0.1766*omega^2;
k=(1+S*(1-Tr^.5))^2;
a=0.42748*(R*Tc)^2*k/Pc;
b=0.08664*R*Tc/Pc;
u=1;
w=0;
elseif equation=="pr"
S=0.37464+1.54226*omega-0.26992*omega^2;
k=(1+S*(1-Tr^.5))^2;
a=0.45724*(R*Tc)^2*k/Pc;
b=0.007780*R*Tc/Pc;
u=2;
w=-1;
else
error('the equation selected is not an option')
end
A=a*P/(R*T)^2;
B=b*P/(R*T);
alpha=-1-B+u*B;
beta=A+w*B^2-u*B-u*B^2;
gamma=-A*B-w*B^2-w*B^3;
Z=[1 alpha beta gamma];
Z=roots(Z);
end
Alex Mcaulley could you explain what other inputs are needed? thanks
Guillaume
Guillaume 2019 年 3 月 19 日
編集済み: Guillaume 2019 年 3 月 19 日
You've told us the line that cause the error, but not what the error is. Please give us the full text of the error message (everything that is red).
Why do the variables have to be global? Why can't they be input arguments of phi (just as they are inputs of CompressibilityFactor. Tracking the state of global variables is difficult and becomes more so as the code grows in complexity. As Stephen said, globals are the source of many problems.
Enrico Bussetti
Enrico Bussetti 2019 年 3 月 19 日
編集済み: Enrico Bussetti 2019 年 3 月 19 日
They can't be inputs of phi because I need to zero the function phi with fsolve.
if I try to do it this is what I get (P1 being a numerical value)
>> fsolve(phi,P1)
Not enough input arguments.
Error in phi (line 4)
[Z,A,B,~,~]=zabsk(equation,Tc,Pc,omega,T,P);
Alex Mcaulley
Alex Mcaulley 2019 年 3 月 19 日
Are you sure that this is the function that you are calling? I mean: Do you have more functions with the same name(older versions) in another folder? Is the following line giving the folder you are expecting?
which CompressibilityFactor
For sure, global variables are not a good idea
Torsten
Torsten 2019 年 3 月 19 日
編集済み: Torsten 2019 年 3 月 19 日
fsolve(@(P)phi(equation,Tc,Pc,omega,T,P),P1)
Take a look at "parametrizing functions" in order to avoid global variables.
Enrico Bussetti
Enrico Bussetti 2019 年 3 月 19 日
Yes, the folder is the right one. And I have no other functions with the same name

サインインしてコメントする。

 採用された回答

Guillaume
Guillaume 2019 年 3 月 19 日

0 投票

They can't be inputs of phi because I need to zero the function phi with fsolve.
Yes, they can:
function F=phi(P, Tc, Pc, T, omega, equation)
%no global. everything is an input
%... rest of the code as it is
end
%main code that call fsolve:
Tc = xxx;
Pc = yyy;
T = zzz;
omega = uuu;
equation = something;
result = fsolve(@(p) phi(p, Tc, Pc, T, omega, equation), P1);
With regards to the error you get, the problem is not at all with the function itself. Apart from the dangerous use of global variables the code is fine. It's with the way you use fsolve. Proper syntax is:
fsolve(@phi, P1) %The @ is critical
With fsolve(phi, P1), that line calls phi with no input and if it didn't error would solve the function returned by phi.
With fsolve(@phi, P1), you're solving the function phi.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by