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

1 view (last 30 days)
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

Accepted Answer

Torsten
Torsten on 23 Sep 2023
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

More Answers (1)

Dyuman Joshi
Dyuman Joshi on 23 Sep 2023
Edited: Dyuman Joshi on 23 Sep 2023
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

Categories

Find more on Chemistry 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!