Matlab
Matlab
Sigma = sm
Omega = om
Otro = ps
The MATLAB function cubicEOSZ implements the cubic equation of state. This function uses
the built-in function fzero to estimate the compressibility factor and molar volume. A simple calling
syntax is
[Z V] = cubicEOSZ(state, eos, T, P, Tc, Pc, w)
Here, state denotes the state of the fluid: 'l' or 'L' when the fluid is liquid, 'l' or 'L' when the fluid is
vapor. eos is the equation of state being used: 'VDW' when the van der Waals equation is used, 'RK'
when the Redlich–Kwong equation is used, 'SRK' when the Soave–Redlich–Kwong equation is
used, and 'PR' when the Peng–Robinson equation is used (both capital and lowercase letters are permitted).
T and P are the temperature (K) and pressure (bar), respectively; Tc and Pc are the critical
temperature (K) and pressure (bar), respectively; and w is the acentric factor. Z and V are estimated
values of the compressibility factor and the molar volume (cm3/mol).
function [Z V] = cubicEOSZ(state,eos,T,P,Tc,Pc,w)
% Estimation of Z and V using cubic equations of state
% input
% state: fluid state (liquid: L, vapor: V)
% eos: type of equation being used (VDW, RK, SRK, PR)
% T,P: temperature (K) and pressure (bar)
% Tc,Pc: critical temperature (K) and pressure (bar)
% w: acentric factor
% Tr and Pr (reduced T and P)
Tr = T/Tc; Pr = P/Pc; R = 83.14; % cm^3*bar/mol/K
nc = length(w);
% set parameters
eos = upper(eos);
switch eos
case {'VDW'}
sm = 0; ep = 0; om = 0.125; ps = 0.42188; mx = [0];
case{'RK'}
ep = 0; sm = 1; om = 0.08664; ps = 0.42748;
al = 1./sqrt(Tr);
mx = (Tr.^(-1/4) - 1)./(1 - sqrt(Tr));
case{'SRK'}
ep = 0; sm = 1; om = 0.08664; ps = 0.42748;
al = (1+(0.48+1.574*w-0.176*w.^2).*(1-sqrt(Tr))).^2;
mc = [0.48 1.574 0.176];
mx = [ones(nc,1) w -w.^2]*mc';
case{'PR'}
ep = 1 - sqrt(2); sm = 1 + sqrt(2); om = 0.07780; ps = 0.45724;
mc = [0.37464 1.54226 0.26992];
mx = [ones(nc,1) w -w.^2]*mc';
end
% calculation of alpha, beta and q
al = (1 + mx.*(1-sqrt(Tr))).^2;
beta = om*Pr./Tr; q = ps*al./(om*Tr);
[Z,V,phi] = phimix(ni,P,T,Pc,Tc,w,k,state,eos)
where ni is the number of moles (or mole fractions) of each component in a mixture (vector), P is
the pressure (Pa), T is the temperature (K), w is the acentric factor, k is a matrix of binary
interaction
parameters, state denotes the state of the fluid ('L': liquid, 'V': vapor), eos is the equation of state
being used ('RK', 'SRK', or 'PR'), Z is the compressibility factor, V is the molar volume, and phi is
the fugacity coefficients of all components (vector).
% Roots
Z = roots(c);
iz = abs(imag(Z)); Z(and(iz>0,iz<=1e-6)) = real(Z(and(iz>0,iz<=1e-6)));
for i = 1:length(Z), zind(i) = isreal(Z(i)); end
Z = Z(zind);
if state == 'L'
Z = min(Z);
else
Z = max(Z);
end
V = R*T*Z/P;
% fugacity coefficients
bara = (2*ni'*am - a*ones(1,length(ni)))'; barb = bi;
phi = exp((Z - 1)*barb/b - log((V - b)*Z/V) + (a/(b*R*T))/(ep – sm)*...
log((V + sm*b)/(V + ep*b))*(1 + bara/a - barb/b));
end