MATLAB: Error: Not enough input arguments

errornot enough input

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.

Best 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.
  • Related Question