Output argument "Yeq" (and maybe others) not assigned during call Error Message

7 views (last 30 days)
Hi
I'm trying to use a function i keep getting the same error. Even though in my function i have assigned all outputs. I keep getting the same error message :
>> TestingPtFlash
Output argument "Yeq" (and maybe others) not assigned during call to "PTFLASH_VLE_Wilson".
Error in TestingPtFlash (line 17)
[Xeq, Yeq, alphaV, Fl, Fv]=PTFLASH_VLE_Wilson(C,MW,rhoL,BIP,P,T,Z)
The function i'm using is
function[Xeq, Yeq, alphaV, Fl, Fv]=PTFLASH_VLE_Wilson(C,MW,rhoL,BIP,P,T,Z)
%this is a function that calculates PT flash for non -ideal systems where K
%cannot be explicit since it depends on X which is also a variable
c=length(Z);
for i=1:c
Ps=exp((C(i,1))+(C(i,2)/T)+(C(i,3)*log(T))+(C(i,4)*T^C(i,5)));
end
% validating the possibility of having VLE
[PB,y]=PB_VLE_Wilson(Z,C,MW,rhoL,T,BIP);
[PD,x]=PD_VLE_wilson (C,MW,rhoL,BIP,T,Z);
% checking if there is VLE
if P<=PD %% y here represents the mole fraction of componenets
Xeq=0;
Yeq=y;
alphaV=1;
Fl=0;
Fv=P*Z;
elseif P>=PB %% x here represents the mole fraction of components
Xeq=x;
Yeq=0;
alphaV=0;
[gamma,a]=ACTIVITY_WILSON(MW,rhoL,BIP,T,Xeq);
Fl=Ps.*Xeq.*gamma;
Fv=0;
else
Xeq=(x+y)/2 ; % first guess on x
[gamma,a]=ACTIVITY_WILSON(MW,rhoL,BIP,T,Xeq); %first guess on gamma
K=(Ps.*gamma)/P; % first guess on K
Psi_0=sum(Z.*(K-1));
Psi_1=sum(Z.*(K-1)./K);
if Psi_0*Psi_1>=0 % bad initial guess
Xeq=0;
Yeq=0;
alphaV=0;
Fl=0;
Fv=0;
disp("bad initial guess")
else %supposedly good initial guess
Fl=zeros(1,c); % trick to enter the while cycle at the begininng
Fv=Fl+1;
iter=0; % initialization of iteration count
while max(abs((Fv-Fl)./Fv))>0.00001 && iter<10000 && Psi_0*Psi_1<0
[ alphaV ] = RACHFORDRICE_BISECT( K,Z );
xeq=Z./((1-alphaV)+alphaV*K);
Yeq=K.*xeq;
[gamma,a]=ACTIVITY_WILSON(MW,rhoL,BIP,T,Z); %Gamma new guess
K=(Ps.*gamma)/P;
psi_0=sum(z.*(K-1));
psi_1=sum(z.*(K-1)./K);
Fv=P*Yeq;
Fl=Ps.Xeq.*gamma;
iter=iter+1;
end
end
end
end
There are other subprograms being used. If want to run it yourself i can upload them as well. But i feel the main function is at fault here
  1 Comment
Stephen23
Stephen23 on 24 May 2019
Edited: Stephen23 on 24 May 2019
"Even though in my function i have assigned all outputs."
No you haven't. If P>PD and P<PB and Psi_0*Psi_1<0 and the while loop condition is not true (i.e. the loop does not iterate even once) then Yeq and alphaV are not defined.
You might want to argue that "that value combination is impossible", but static code analysis cannot test with values, it can only check to see if there is an execution path where those variables are not defined. And there is such a path.
Note that your code is very badly aligned. Badly aligned code is one way that beginners hide bugs in their code. This is a good example: the very misleading
if Psi_0*Psi_1>=0
alignment makes it almost impossible to understand what that statement refers to, or where its (possible) else and/or elseif statements might be. And yet that if is critical to understanding this warning!
You should align your code consistently, e.g. using the MATLAB editor's default settings.
You can align your code: select all text, ctrl+i.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 24 May 2019
The easiest way to avoid that error is to assign all the outputs as NaN initially. The code will then replace them as necessary. If it does not, the NaN outputs will help you locate your problem. (This assumes that they would not otherwise be assigned NaN values as the result of calculations in your function.)
Example —
function[Xeq, Yeq, alphaV, Fl, Fv]=PTFLASH_VLE_Wilson(C,MW,rhoL,BIP,P,T,Z)
%this is a function that calculates PT flash for non -ideal systems where K
%cannot be explicit since it depends on X which is also a variable
[Xeq, Yeq, alphaV, Fl, Fv] = deal(NaN);
c=length(Z);
. . . (REST OF YOUR CODE) . . .
end

More Answers (0)

Categories

Find more on Error Handling 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!