필터 지우기
필터 지우기

How I can run following code for different p and t values?

조회 수: 4 (최근 30일)
Aiden James
Aiden James 2023년 9월 23일
댓글: Aiden James 2023년 9월 25일
function [x] = LiBrH2O(t,p)
% Function LiBrH2O calculates the concentration for the corresponding
% Lithium Bromide - Water solution temperature and Saturation Pressure,
% within the range as per as curve-fitting equation.
%
% INPUTS:
% p = Saturation Pressure in kPa
% t = Lithium Bromide - Water solution temperature in degree-Celsius
% OUTPUT:
% x = concentration of Lithium Bromide - Water solution in kJ/kg
t=[82 83 84 85 86 87 88 89]
p=[8 7 6 5 4 3 6 8]
if (t>5 && t<175) %---Checks temperature range
%---Constants
A0 = -2.00755; A1 = 0.16976; A2 = -3.133362*10^(-3); A3 = 1.97668*10^(-5);
B0 = 124.937; B1 = -7.71649; B2 = 0.152286; B3 = -7.95090*10^(-4);
C = 7.05; D = -1596.49; E = -104095.5;
%---Refrigerant Temperature Calculation from Saturation Pressure
T = -2*E/(D + (D*D - 4*E*(C-log10(p)))^(0.5)); %---Temp in Kelvin
t1 = T-273; %---Temp in Degree-Celsius
if (t1>-15 && t1<110)
%---Creating polynomial coefficient
P0 = B0 + t1*A0;
P1 = B1 + t1*A1;
P2 = B2 + t1*A2;
P3 = B3 + t1*A3;
%---Calculation for Concentration
P = [P3 P2 P1 (P0 - t)];
X = roots(P);
[r c] = size(X);
for l=1:r
if (X(l,c)>45 && X(l,c)<70)%---Checks Concentration range
x = X(l,c)
end
end
else
disp(t1);
disp('Error: Refrigerant temperature out of range: t1>-15 && t1<110');
end
else
disp(t);
disp('Error: Solution temperature out of range: t>5 && t<175');
end

채택된 답변

Torsten
Torsten 2023년 9월 23일
t=[82 83 84 85 86 87 88 89];
p=[8 7 6 5 4 3 6 8];
x = arrayfun(@(t,p)LiBrH2O(t,p),t,p);
x
x = 1×8
58.2131 59.9810 61.8768 63.9662 66.3658 69.3153 63.5742 61.3198
function [x] = LiBrH2O(t,p)
% Function LiBrH2O calculates the concentration for the corresponding
% Lithium Bromide - Water solution temperature and Saturation Pressure,
% within the range as per as curve-fitting equation.
%
% INPUTS:
% p = Saturation Pressure in kPa
% t = Lithium Bromide - Water solution temperature in degree-Celsius
% OUTPUT:
% x = concentration of Lithium Bromide - Water solution in kJ/kg
if (t>5 && t<175) %---Checks temperature range
%---Constants
A0 = -2.00755; A1 = 0.16976; A2 = -3.133362*10^(-3); A3 = 1.97668*10^(-5);
B0 = 124.937; B1 = -7.71649; B2 = 0.152286; B3 = -7.95090*10^(-4);
C = 7.05; D = -1596.49; E = -104095.5;
%---Refrigerant Temperature Calculation from Saturation Pressure
T = -2*E/(D + (D*D - 4*E*(C-log10(p)))^(0.5)); %---Temp in Kelvin
t1 = T-273; %---Temp in Degree-Celsius
if (t1>-15 && t1<110)
%---Creating polynomial coefficient
P0 = B0 + t1*A0;
P1 = B1 + t1*A1;
P2 = B2 + t1*A2;
P3 = B3 + t1*A3;
%---Calculation for Concentration
P = [P3 P2 P1 (P0 - t)];
X = roots(P);
[r c] = size(X);
for l=1:r
if (X(l,c)>45 && X(l,c)<70)%---Checks Concentration range
x = X(l,c);
end
end
else
disp(t1);
disp('Error: Refrigerant temperature out of range: t1>-15 && t1<110');
end
else
disp(t);
disp('Error: Solution temperature out of range: t>5 && t<175');
end
end

추가 답변 (1개)

Dyuman Joshi
Dyuman Joshi 2023년 9월 23일
편집: Dyuman Joshi 2023년 9월 23일
You can run a for loop through the elements of t and p and get the output. Another option is to use arrayfun, which I have done below.
However, you need to define x for values of other cases - if t is not in the range (5,175) or if t1 is not in the range (-15,110), otherwise the code will give an error.
I have used NaN here, see the added 2 pair of values -
t = [82 83 84 85 86 87 88 89 190 150];
p = [8 7 6 5 4 3 6 8 9 500];
x = arrayfun(@LiBrH2O, t, p)
190 Error: Solution temperature out of range: t>5 && t<175 150.4243 Error: Refrigerant temperature out of range: t1>-15 && t1<110
x = 1×10
58.2131 59.9810 61.8768 63.9662 66.3658 69.3153 63.5742 61.3198 NaN NaN
function [x] = LiBrH2O(t,p)
% Function LiBrH2O calculates the concentration for the corresponding
% Lithium Bromide - Water solution temperature and Saturation Pressure,
% within the range as per as curve-fitting equation.
%
% INPUTS:
% p = Saturation Pressure in kPa
% t = Lithium Bromide - Water solution temperature in degree-Celsius
% OUTPUT:
% x = concentration of Lithium Bromide - Water solution in kJ/kg
x = NaN;
if (t>5 && t<175) %---Checks temperature range
%---Constants
A0 = -2.00755; A1 = 0.16976; A2 = -3.133362*10^(-3); A3 = 1.97668*10^(-5);
B0 = 124.937; B1 = -7.71649; B2 = 0.152286; B3 = -7.95090*10^(-4);
C = 7.05; D = -1596.49; E = -104095.5;
%---Refrigerant Temperature Calculation from Saturation Pressure
T = -2*E/(D + (D*D - 4*E*(C-log10(p)))^(0.5)); %---Temp in Kelvin
t1 = T-273; %---Temp in Degree-Celsius
if (t1>-15 && t1<110)
%---Creating polynomial coefficient
P0 = B0 + t1*A0;
P1 = B1 + t1*A1;
P2 = B2 + t1*A2;
P3 = B3 + t1*A3;
%---Calculation for Concentration
P = [P3 P2 P1 (P0 - t)];
X = roots(P);
[r c] = size(X);
for l=1:r
if (X(l,c)>45 && X(l,c)<70)%---Checks Concentration range
x = X(l,c);
end
end
else
disp(t1);
disp('Error: Refrigerant temperature out of range: t1>-15 && t1<110');
end
else
disp(t);
disp('Error: Solution temperature out of range: t>5 && t<175');
end
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by