Error: Not enough input arguments

1 Ansicht (letzte 30 Tage)
Enrico Bussetti
Enrico Bussetti am 19 Mär. 2019
Kommentiert: Enrico Bussetti am 19 Mär. 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 Kommentare
Torsten
Torsten am 19 Mär. 2019
Bearbeitet: Torsten am 19 Mär. 2019
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 am 19 Mär. 2019
Yes, the folder is the right one. And I have no other functions with the same name

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 19 Mär. 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.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by