I am trying to run my code but it isn´t work. I don´t know what is wrong whit

2 次查看(过去 30 天)
clear clc
% Temperatura
disp('Ingrese Temperatura observada en °F:');
Tobs = input('');
while Tobs < -58 || Tobs > 302
disp('Ingrese un valor de temperatura dentro del rango [-58,302]');
Tobs = input('');
end
J = menu('Seleccionar el tipo de fluido', 'Crudo', 'Productos refinados');
% API
disp('Ingrese API observado:');
API = input('');
dA60 = 999.016; % Densidad del agua a 60 °F (kg/m3)
df = (dA60 * 141.5) / (API + 131.5); % Densidad (kg/m3)
if df < 470.5 || df > 1201.8
disp('La densidad se sale del rango permitido');
else
if df < 610.6
df = 610.6;
end
if df > 1163.5
df = 1163.5;
end
end
P0 = 0;
% Se van a realizar iteraciones para hallar la densidad del fluido a 60°F y así calcular el API a 60°F
% Paso 1: Estimar una densidad a 60°F
df60 = df; % Densidad a 60 °F (kg/m3)
n = 0;
while n <=15
end
% Valores de entrada
% Paso 2: Iniciar el proceso iterativo para hallar la densidad a 60°F con
% respecto a la estimada, a partir de las condiciones observadas.
% Conversión de temperatura ITS-90 a IPTS-68
Tobsc = (Tobs - 32) / 1.8; % Temperatura consistente con ITS-90 (°C)
t = Tobsc / 630; % Valor de temperatura escalado
% Coeficientes ai
a1 = -0.148759;
a2 = -0.267408;
a3 = 1.080760;
a4 = 1.269056;
a5 = -4.089591;
a6 = 1.871251;
a7 = 7.438081;
a8 = -3.536296;
deltat=(a1+(a2+(a3+(a4+(a5+(a6+(a7+a8*t)*t)*t)*t)*t)*t)*t)*t; % Corrección de Temperatura consistente con ITS-90 a IPTS-68
% Valores de Temperatura consistente en IPTS-68
TobscIPTS = Tobsc - deltat; % Temperatura consistente con IPTS-68 (°C)
TobsIPTS = 1.8 * TobscIPTS + 32; % Temperatura consistente con IPTS-68 (°F)
if J == 1
% Valores de k en crudo
K0 = 341.0957; % (kg2/m6 °F)
K1 = 0; % (kg/m3 °F)
K2 = 0; % (°F^-1)
% Cálculos
D60 = 0.01374979547; % Valor de cambio de temperatura
% Cálculo de la densidad a IPTS-68 a 60°F
A = (D60 / 2) * (((K0 / df60) + K1) * (1 / df60) + K2);
B = (2 * K0 + K1 * df60) / (K0 + (K1 + K2 * df60) * df60);
dIPTS = df60 * (1 + ((exp(A * (1 + 0.8 * A)) - 1) / (1 + A * (1 + 1.6 * A) * B))); % Densidad cambiada a IPTS-68 a 60°F (rho*) (kg/m3)
% Cálculo del factor de expansión térmica a 60°F
a60 = ((K0 / dIPTS) + K1) * (1 / dIPTS) + K2; % Factor de expansión térmica a 60 °F (°F^-1)
% Cálculo del CTL
deltaT = TobsIPTS - 60.0068749; % Diferencia entre la temperatura consistente IPTS-68 y la base 60°F
CTL = exp(-a60 * deltaT * (1 + 0.8 * a60 * (deltaT + D60))); % Factor de corrección de volumen por temperatura
% Cálculo del factor de compresibilidad escalado
Fp = exp(-1.9947 + 0.00013427 * TobsIPTS + (793920 + 2326.06 * TobsIPTS) / dIPTS^2); % Factor de compresibilidad escalado (psi^-1)
% Cálculo del CPL
CPL = 1; % Factor de corrección de volumen debido a la presión
VCF = CTL * CPL; % Factor de corrección de volumen combinado debido a la temperatura y la presión
E = (df / (CTL * CPL)) - df60; % Factor de corrección por densidad utilizado en el procedimiento iterativo
Da = 2;
DT = Da * a60 * deltaT * (1 + 1.6 * a60 * deltaT); % Factor de corrección por temperatura utilizado en el procedimiento iterativo
Dp = 0; % Solo en crudo 5A porque P=0
deltad60 = E / (1 + DT + Dp); % Cambio aplicado al valor de densidad en la n-ésima iteración para obtener el valor en la siguiente iteración (kg/m3)
df60 = df60 + deltad60;
n = n + 1;
end
if J == 2
% Valores de K en productos refinados
if df60 >= 838.3127 || df60 <= 1163.5
K0 = 103.8720; % (kg2/m6 °F)
K1 = 0.2701; % (kg/m3 °F)
K2 = 0; % (°F^-1)
Da = 1.3;
end
if df60 >= 787.5195 || df60 < 838.3127
K0 = 330.3010;
K1 = 0;
K2 = 0;
Da = 2;
end
% Aquí debes completar las condiciones para otros valores de df60
end

回答(2 个)

Walter Roberson
Walter Roberson 2023-10-8
while n <=15
end
You have the end of that while immediately after, the very next statement. You do not change n within that empty loop, so there is no way to exit the very short loop.
  3 个评论
Walter Roberson
Walter Roberson 2023-10-8
What inputs are you using? And what are you expecting as output? How can you tell if it is working or not?
María Belén Oyola Gutiérrez
My inputs are Tobs, J and API and my output is CTL. I say it's not work because when I run the code it doesn't give me any results.

请先登录,再进行评论。


Image Analyst
Image Analyst 2023-10-8
It probably gets stuck in an infinite loop. Don't you understand that when you do this:
n = 0;
while n <=15
end
n is less than fifteen so it will enter the loop but will never be able to exit it. You forgot to use a failsafe on your loop, so it enters but never, ever leaves. You need a failsafe like this:
% Demonstration of how to avoid an infinite loop by setting up a failsafe.
% Set up a failsafe
maxIterations = 100; % Way more than you think it would ever need.
loopCounter = 0;
% Now loop until we obtain the required condition: a random number equals exactly 0.5.
% If that never happens, the failsafe will kick us out of the loop so we do not get an infinite loop.
n = 0; % Initialize so we can enter the loop the first time.
while (n <= 15) && loopCounter < maxIterations
loopCounter = loopCounter + 1;
fprintf('Iteration #%d.\n', loopCounter)
% Now SOMEHOW change n!!!!!
% n = whatever..........
end
% Alert user if we exited normally, or if the failsafe kicked us out to avoid an infinite loop.
if loopCounter < maxIterations
% Then the loop found the condition and exited early, which means normally.
fprintf('Loop exited normally after %d iterations.\n', loopCounter);
else
% Then the loop never found the condition and exited when the number of iterations
% hit the maximum number of iterations allowed, which means abnormally.
fprintf('Loop exited abnormally after iterating the maximimum number of iterations (%d) without obtaining the exit criteria.\n', maxIterations);
end
fprintf('All done after %d iterations.\n', loopCounter)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by