for loop does not save the values in the respective array

조회 수: 1 (최근 30일)
Juliana Quintana
Juliana Quintana 2022년 2월 20일
댓글: Ersad Mert Mutlu 2022년 2월 20일
Hello everyone, I'm looking for help about a for loop. I think it works and makes the loop, but it does not save the values in an array I made. It just show the last value taht seems to be the same value as the inizialazation I made. I would really appreciate your help.
clc
clear all
close all
%________________
%parámetros
% Pc y Tc tomado de https://www.linde-gas.es/es/images/n-Butano_tcm316-612756.pdf
% w tomado de https://termoapunefm.files.wordpress.com/2011/10/41-1-_constantes_y_coeficientes_de_propiedades_fisicas.pdf
n = 100;
T = 800; %K
P = linspace(0.005,0.02,n) ; %atm
Pc = 37.46; %atm
Tc =425.16 ; % K
w = 0.1954 ; %
R = 0.08206 ; %atm
%_______________________________
%valores de while
h=1e-8;
tol=1e-6;
iterMax=1000;
error = 10;
iter = 0;
%_______vector de respuesta_______
Vf = zeros(n,1);
for i = 1:n
P = linspace(0.005,0.02,n) ; %atm
V0 = 13129;
while (error>tol && iter <iterMax)
fun = patel(P(i),V0);
derivada = (patel(P(i),(V0+h))-patel(P(i),V0))./h ;
vol = V0 - fun./derivada;
error = abs((vol-V0)./V0);
iter = iter + 1;
V0 = vol;
end
end
Vf(i)= vol;
plot(P, Vf)
function resp = patel(P,V)
T = 800; %K
Pc = 37.46; %atm
Tc =425.16 ; % K
w = 0.1954 ; %
R = 0.08206 ; %atm
Tr = T/Tc;
%__________Funciones del factor acentrico_______-
F = 0.42413 + 1.30982*w - 0.295937*(w^2);
zetac = 0.329032 - 0.076799*w + 0.0211947*(w^2);
%_____________omega b ______________
poli = [1 ,(2-(3.*zetac)), 3.*(zetac.^2), (-zetac.^3)];
sol = roots(poli);
sol = double(sol);
[omega_b, other] = min(sol(imag(sol)==0 & real(sol)>0));
%_____________omega a _________
omega_a =3*zetac^2+3*(1-2*zetac)*omega_b +omega_b^2+(1-3*zetac^3);
%___________ omega c _________
omega_c = 1-3*zetac ;
%_________constantes
a = omega_a*(R^2*Tc^2/Pc)*(1+F*(1-sqrt(Tr)))^2;
b = omega_b*(R*Tc/Pc);
c = omega_c*(R*Tc/Pc);
%__________residual_______
der = R.*T./(V-b);
izq = a./(V.*(V+b)+c.*(V-b));
resp = der - izq - P;
end
  댓글 수: 5
Juliana Quintana
Juliana Quintana 2022년 2월 20일
I just checked the script and it works, but it does not give the correct answer. I will check again , maybe there is something wrong with my equations. Thank you soo much for the help.

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2022년 2월 20일
편집: Torsten 2022년 2월 20일
%________________
%parámetros
% Pc y Tc tomado de https://www.linde-gas.es/es/images/n-Butano_tcm316-612756.pdf
% w tomado de https://termoapunefm.files.wordpress.com/2011/10/41-1-_constantes_y_coeficientes_de_propiedades_fisicas.pdf
n = 100;
T = 800; %K
P = linspace(0.005,0.02,n) ; %atm
Pc = 37.46; %atm
Tc =425.16 ; % K
w = 0.1954 ; %
R = 0.08206 ; %atm
%_______________________________
%valores de while
h=1e-8;
tol=1e-6;
iterMax=1000;
error = 10;
iter = 0;
V0 = 13129;
%_______vector de respuesta_______
Vf = zeros(n,1);
volold = V0;
for i = 1:n
while iter<iterMax
derivada = (patel(P(i),(volold+h))-patel(P(i),volold))./h ;
fun = patel(P(i),volold);
volnew = volold - fun/derivada;
error = abs((volold-volnew)/volold);
volold = volnew;
if error>tol
iter = iter + 1;
else
break
end
end
Vf(i) = volnew;
end
plot(P, Vf)
end
function resp = patel(P,V)
T = 800; %K
Pc = 37.46; %atm
Tc =425.16 ; % K
w = 0.1954 ; %
R = 0.08206 ; %atm
Tr = T/Tc;
%__________Funciones del factor acentrico_______-
F = 0.42413 + 1.30982*w - 0.295937*(w^2);
zetac = 0.329032 - 0.076799*w + 0.0211947*(w^2);
%_____________omega b ______________
poli = [1 ,(2-(3.*zetac)), 3.*(zetac.^2), (-zetac.^3)];
sol = roots(poli);
sol = double(sol);
[omega_b, other] = min(sol(imag(sol)==0 & real(sol)>0));
%_____________omega a _________
omega_a =3*zetac^2+3*(1-2*zetac)*omega_b +omega_b^2+(1-3*zetac^3);
%___________ omega c _________
omega_c = 1-3*zetac ;
%_________constantes
a = omega_a*(R^2*Tc^2/Pc)*(1+F*(1-sqrt(Tr)))^2;
b = omega_b*(R*Tc/Pc);
c = omega_c*(R*Tc/Pc);
%__________residual_______
der = R.*T./(V-b);
izq = a./(V.*(V+b)+c.*(V-b));
resp = der - izq - P;
end
  댓글 수: 3
Torsten
Torsten 2022년 2월 20일
I made a small change to the program. It runs properly now without producing Inf values.
Juliana Quintana
Juliana Quintana 2022년 2월 20일
Perfect, I will check it out.
Thank you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by