How can use while loop in this case.
Show older comments
I need the code to simulate from my random p (guess value) till it solves the code and gets both f_g and f_l less than the initial guess. I added the while loop so that it runs couple of time additing 0.01 to the guess value till the statement is true. But I fail to understand what went wrong. Please help! I'm new with Matlab.
clc
clear all
%Temperature and pressure
T = 173.15; %K
p = 3e6;; %Pa (guess)
K=1000;
L=1000;
while all(K, L) < p
p = p - 0.01
%Constant for EOS
Tc = 190.564; %Critical Temerpature (K)
Pc = 4.5992e6; %Critical Pressure (Pa)
Vc = 1.3094e-4; %in m^3/mol
R = 8.3145; %Universal gas constant (J/mol.K)
w = 0.01142 %Acentric factor
k = 0.480 + 1.574*w - 0.176*(w^2);
sqrt_alphaT = 1 + k*(1 - sqrt(T./Tc));
alphaT = (sqrt_alphaT)^2;
%Calculate constants of the EOS.
a = 0.42748*(R*R*(Tc^2)/Pc)*alphaT; %m^3.J.K^0.5/mol^2
b = 0.08664*R*Tc/Pc; %m3/mol
%Soave-Redlich-Kwong EOS defined with an anonymous function
P = @(T,V) R*T./(V - b) - a./(V.*(V+b)); %Pa
%The EOS, C, with an anonymous function
%C = @(T,V) (R*T./V)-(R*T./(V - b) - a./(V.*(V+b))); %Pa/K
%volume
v = 1.2*b; %m3/mol
v_ideal= R*T/p; %m3/mol
%Calculated volume V_l and V_g (m3/mol)
V_l = fsolve(@(V) P(T,V) - p,v);
V_g = fsolve(@(V) P(T,V) - p,v_ideal);
%Calculated initial and final compressibilities
z_l = p*V_l/(R*T);
z_g = p*V_g/(R*T);
%Then solving the related equation
ln_phi_l = (1/(R*T))*(integral(@(V) R*T./V - P(T,V), inf, V_l)) - log(z_l) + (z_l-1);
fugacity_l = exp(ln_phi_l);
ln_phi_g = (1/(R*T))*(integral(@(V) R*T./V - P(T,V), inf, V_g)) - log(z_g) + (z_g-1);
fugacity_g = exp(ln_phi_g);
%Calculating fugacity
fl= fugacity_l*p;
K = abs(fl);
fg= fugacity_g*p;
L = abs(fg);
%Tolarance
Tolarence = ((fl/fg)-1)*100;
end
Tolarence
p

4 Comments
Walter Roberson
on 25 Mar 2021
while all(K, L) < p
if the intention is test that all members of K and all members of L are less than p then
while all([K(:);L(:)] < p)
Syed Muntaha Manzoor
on 25 Mar 2021
Walter Roberson
on 25 Mar 2021
what does
all(K, L)
mean to you? Is "all" the name of a 2d array?
Syed Muntaha Manzoor
on 25 Mar 2021
Answers (1)
Jan
on 25 Mar 2021
As far as I can see, K and L are scalars. Then replace:
while all(K, L) < p
by
while K < p & L < p
2 Comments
Syed Muntaha Manzoor
on 25 Mar 2021
Jan
on 25 Mar 2021
If you start at p=1e6 and reduce p in steps of 0.01, it might take millions of iterations to find matching results.
Categories
Find more on Thermal Analysis 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!