Error: Not enough input arguments
1 view (last 30 days)
Show older comments
Enrico Bussetti
on 19 Mar 2019
Commented: Enrico Bussetti
on 19 Mar 2019
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
Accepted Answer
Guillaume
on 19 Mar 2019
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)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!