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 个评论
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
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
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
2019-3-19
编辑:Enrico Bussetti
2019-3-19
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
Enrico Bussetti
2019-3-19
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!