NaN generated as output
Show older comments
Hello
I've an function which calculates the dew point pressure. When i call it to generate the outputs such a x(the composition in the liquid phase) and Pressure. It produces output with no values and content of 'NaN'.
If i run the function, segment by segment to see where the error might lie. I think i've narrowed it down to a loop. This is stated below:
iter=0
while max(abs((PD*z-Ps.*x.*gamma)./(PD*z)))>0.001 && iter<100
PD=1/sum(z./Ps.*gamma);
x=(PD*z)./(Ps.*gamma);
[gamma,a]=ACTIVITY_WILSON(MW,rhoL,BIP,T,x);
iter=iter+1;
end
Though it is possible the issue lies elsewhere. The function is stated below:
function [PD,x]=PD_VLE_WILSON(C,MW,rhoL,BIP,T,z)
c=length(z);
for i=1:c
Ps(i)=exp(C(i,1)+C(i,2)/T+C(i,3).*log(T)+C(i,4).*(T).^(C(i,5)));
end
[PD,x]=PD_VLE_ID(C,T,z);
[gamma,a]=ACTIVITY_WILSON(MW,rhoL,BIP,T,x);
iter=0
while max(abs((PD*z-Ps.*x.*gamma)./(PD*z)))>0.001 && iter<100
PD=1/sum(z./Ps.*gamma);
x=(PD*z)./(Ps.*gamma);
[gamma,a]=ACTIVITY_WILSON(MW,rhoL,BIP,T,x);
iter=iter+1;
end
if iter<1000
PD=1/sum(z./Ps.*gamma);
x=(PD*z)./(Ps.*gamma);
else
PD=0;
x=0;
end
end
The first subprogram:
function [gamma,a]=ACTIVITY_WILSON(MW,rhoL,BIP,T,x)
%This program calculates the activity coefficients (gamma) and the
%activities (a) of each component of a mixture of c components using the
%Wilson model.
%INPUT PARAMETERS: MW: vector (1xc) reporting the molecular weights of the
%c components; rhoL, vector (1xc) reporting the liquid density of the c
%pure components at temperature T; BIP is a matrix cxc reporting the energy
%interaction parameters (BIP(i,j)=lambda_ij-lambda_ii, J/mol). The energy
%interaction parameters are temperature independent; T: temperature of the
%system; x vector (1xc) reporting the mole fractions of the components of
%the mixture.
%OUTPUT PARAMETERS: gamma: vector 1xc reporting the activity
%coefficients of the components of the mixture; a: vector 1xc reporting the
%activities of the components of the mixture.
%Unless otherwise stated, all input/output parameters are expressed
%according to MKS.
R=8.314;
c=length(x);
%Molar volumes of the pure liquid components composing the mixture
VL=1./((rhoL*1000)./MW);
%Lambda terms (dimensionless) of the Wilson formula
for i=1:c
for j=1:c
Lambda(i,j)=(VL(j)/VL(i))*exp(-BIP(i,j)/(R*T));
end
end
for i=1:c
for j=1:c
A=sum(x.*Lambda(j,:));
C(j)=x(j)*Lambda(j,i)/A;
end
lngamma(i)=1-log(sum(x.*Lambda(i,:)))-sum(C);
gamma(i)=exp(lngamma(i));
a(i)=gamma(i)*x(i);
end
end
The second subprogram.
function [PD,x]=PD_VLE_ID(C,T,z)
c=length(z);
for i=1:c
Ps(i)=exp(C(i,1)+C(i,2)/T+C(i,3).*log(T)+C(i,4).*(T).^(C(i,5)));
end
PD=1/sum(z./Ps);
x=(PD*z)./Ps;
end
And my input if you need to run it:
T=343;
z=[0.1 0.9];
P=4000000;
MW=[46 18];
rhoL=[789 997];
BIP=[1972.2 3700.10;1972.2 3700.10];
C1=[73.649 74.475];
C2=[7258.2 -7164.3];
C3=[-7.3037 -7.327];
C4=[4.17E-06 3.13E-06];
C5=[2 2];
C = [C1' C2' C3' C4' C5'];
Answers (1)
Steven Lord
on 29 May 2019
1 vote
Set an error breakpoint to force MATLAB to enter debug mode as soon as a NaN or Inf value is generated. Then you can see the exact line that generated the NaN or Inf value and examine the values of your variables at that point. If you need to see with what values your function was called you can work your way up the function call stack using the techniques in the first section on this documentation page.
Categories
Find more on Thermodynamics and Heat Transfer 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!