Error: Not enough input arguments

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 Comments

Alex Mcaulley
Alex Mcaulley on 19 Mar 2019
Edited: Alex Mcaulley on 19 Mar 2019
CompressibilityFactor function is needed. By the way, more input parameters are needed in this function.
Note that global variables are the cause of many beginners problems, they are slow, buggy, and hard to debug. You should avoid using them:
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 on 19 Mar 2019
Edited: Guillaume on 19 Mar 2019
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 on 19 Mar 2019
Edited: Enrico Bussetti on 19 Mar 2019
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);
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 on 19 Mar 2019
Edited: Torsten on 19 Mar 2019
fsolve(@(P)phi(equation,Tc,Pc,omega,T,P),P1)
Take a look at "parametrizing functions" in order to avoid global variables.
Yes, the folder is the right one. And I have no other functions with the same name

Sign in to comment.

 Accepted Answer

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.

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!