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 commentaires

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
Syed Muntaha Manzoor le 25 Mar 2021
Basically the intention is to set a random P value which will run in loops till the condition is satisfied that using the coorect p values both f's are less than that p.
what does
all(K, L)
mean to you? Is "all" the name of a 2d array?
I was actually trying to put "all" to refer K and L less than p instead of saying individually.
I tried
while all([K(:);L(:)] < p)
end
But not I'm getting my tollarance >1 which is not accepable. How do I limit it in such a way that K,L < p guessed and also my tollarance <1?

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 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 commentaires

Syed Muntaha Manzoor
Syed Muntaha Manzoor le 25 Mar 2021
The code keeps on running and doesnt set an output. Only value it shows out if I guess p=1e6
Jan
Jan le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Thermal Analysis dans Centre d'aide et File Exchange

Produits

Version

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by