Error to find the Molor Volume using Lee/Kesler Data Table

1 view (last 30 days)
clear,clc,close all
filename = 'Data.xlsx'
filename = 'Data.xlsx'
DataStruct = importdata(filename);
Coefficients = DataStruct.data.DataGeneral
Coefficients = 8×15
1.0e+03 * 0.0399 0 0.1509 0.0490 0.0746 0.2980 6.0000 0.0208 0.0000 -0.0000 0.0000 -0.0000 -0.0062 0.1800 0 0.0709 0.0001 0.4172 0.0771 0.1240 0.2980 1.0000 0.0331 0.0122 -0.0121 0.0044 -0.0002 -0.0108 0.2590 0 0.0440 0.0002 0.3042 0.0738 0.0940 0.2980 1.2000 0.0250 0.0552 -0.0337 0.0079 -0.0001 -0.4036 0.2282 -0.3935 0.0280 0.0000 0.1262 0.0340 0.0892 0.2980 6.0000 0.0261 0.0082 -0.0020 0.0002 0.0000 -0.0080 0.2210 0 0.0320 0.0000 0.1546 0.0504 0.0734 0.2980 6.0000 0.0297 0.0061 -0.0012 0.0001 -0.0002 -0.0099 0.2379 0 0.0365 0.0001 0.3247 0.0831 0.0810 0.2980 1.2000 0.0321 -0.0135 0.0199 -0.0068 -0.0000 -0.1016 0.2287 -0.0923 0.1538 0.0002 0.5564 0.0456 0.2760 0.2980 6.0000 0.1031 0.0042 -0.0011 0.0001 -0.0019 -0.1333 0.4224 -0.0960 0.0180 0.0003 0.6471 0.2206 0.0559 0.5000 1.7000 0.0301 0.0068 0.0068 -0.0025 0.0001 -0.2509 0.2234 -0.2418
Z0_table = DataStruct.data.DataZ0
Z0_table = 41×16
0 0.0100 0.0500 0.1000 0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 1.5000 2.0000 3.0000 5.0000 7.0000 10.0000 0.3000 0.0029 0.0145 0.0290 0.0579 0.1158 0.1737 0.2315 0.2892 0.3479 0.4335 0.5775 0.8648 1.4366 2.0048 2.8507 0.3500 0.0026 0.0130 0.0261 0.0522 0.1043 0.1564 0.2084 0.2604 0.3123 0.3901 0.5195 0.7775 1.2902 1.7987 2.5539 0.4000 0.0024 0.0119 0.0239 0.0477 0.0953 0.1429 0.1904 0.2379 0.2853 0.3563 0.4744 0.7095 1.1758 1.6373 2.3211 0.4500 0.0022 0.0110 0.0221 0.0442 0.0882 0.1322 0.1762 0.2200 0.2638 0.3294 0.4384 0.6551 1.0841 1.5077 2.1338 0.5000 0.0021 0.0103 0.0207 0.0413 0.0825 0.1236 0.1647 0.2056 0.2465 0.3077 0.4092 0.6110 1.0094 1.4017 1.9801 0.5500 0.9804 0.0098 0.0195 0.0390 0.0778 0.1166 0.1553 0.1939 0.2323 0.2899 0.3853 0.5747 0.9475 1.3137 1.8520 0.6000 0.9849 0.0093 0.0186 0.0371 0.0741 0.1109 0.1476 0.1842 0.2207 0.2753 0.3657 0.5446 0.8959 1.2398 1.7440 0.6500 0.9881 0.9377 0.0178 0.0356 0.0710 0.1063 0.1415 0.1765 0.2113 0.2634 0.3495 0.5197 0.8526 1.1773 1.6519 0.7000 0.9904 0.9504 0.8958 0.0344 0.0687 0.1027 0.1366 0.1703 0.2038 0.2538 0.3364 0.4991 0.8161 1.1341 1.5729
Z1_table = DataStruct.data.DataZ1
Z1_table = 41×16
0 0.0100 0.0500 0.1000 0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 1.5000 2.0000 3.0000 5.0000 7.0000 10.0000 0.3000 -0.0008 -0.0040 -0.0081 -0.0161 -0.0323 -0.0484 -0.0645 -0.0806 -0.0966 -0.1207 -0.1608 -0.2407 -0.3996 -0.5572 -0.7915 0.3500 -0.0009 -0.0046 -0.0093 -0.0185 -0.0370 -0.0554 -0.0738 -0.0921 -0.1105 -0.1379 -0.1834 -0.2738 -0.4523 -0.6279 -0.8863 0.4000 -0.0010 -0.0048 -0.0095 -0.0190 -0.0380 -0.0570 -0.0758 -0.0946 -0.1134 -0.1414 -0.1879 -0.2799 -0.4603 -0.6365 -0.8936 0.4500 -0.0009 -0.0047 -0.0094 -0.0187 -0.0374 -0.0560 -0.0745 -0.0929 -0.1113 -0.1387 -0.1840 -0.2734 -0.4475 -0.6162 -0.8608 0.5000 -0.0009 -0.0045 -0.0090 -0.0181 -0.0360 -0.0539 -0.0716 -0.0893 -0.1069 -0.1330 -0.1762 -0.2611 -0.4253 -0.5831 -0.8099 0.5500 -0.0314 -0.0043 -0.0086 -0.0172 -0.0343 -0.0513 -0.0682 -0.0849 -0.1015 -0.1263 -0.1669 -0.2465 -0.3991 -0.5446 -0.7521 0.6000 -0.0205 -0.0041 -0.0082 -0.0164 -0.0326 -0.0487 -0.0646 -0.0803 -0.0960 -0.1192 -0.1572 -0.2312 -0.3718 -0.5047 -0.6928 0.6500 -0.0137 -0.0772 -0.0078 -0.0156 -0.0309 -0.0461 -0.0611 -0.0759 -0.0906 -0.1122 -0.1476 -0.2160 -0.3447 -0.4653 -0.6346 0.7000 -0.0093 -0.0507 -0.1161 -0.0148 -0.0294 -0.0438 -0.0579 -0.0718 -0.0855 -0.1057 -0.1385 -0.2013 -0.3184 -0.4270 -0.5785
Chem_list = DataStruct.textdata.DataGeneral(2:9,1)
Chem_list = 8×1 cell array
{'Ar' } {'Cl2' } {'CO2' } {'N2' } {'O2' } {'HCl' } {'CCl4'} {'H2O' }
Chem_indx = listdlg('PromptString','Please choose the chemical under invesigation.',...
'SelectionMode','single','ListString',Chem_list)
Error using matlab.internal.lang.capability.Capability.require
Support for Java user interfaces is required, which is not available on this platform.

Error in listdlg (line 58)
Capability.require(Capability.Swing);
Chem =Chem_list{Chem_indx}
promt ={'Enter Temperature (C):','Enter pressure(bar):'}
dlgtitle = 'Input'
dims = [1 35]
answer = inputdlg(promt,dlgtitle,dims)
T = str2num(answer{1}) + 273.15;
P = str2num(answer{2});
Tc = Coefficients(Chem_indx,3);
Pc = Coefficients(Chem_indx,4);
Tr = T/Tc;
Pr = P/Pc;
Pr_ind = find(Z0_table(1,:)>Pr,1)-1;
Tr_ind = find(Z0_table(:,1)>Tr,1)-1;
Pr_match = (Z0_table(1,Pr_ind) == Pr);
Tr_match = (Z0_table(Tr_ind,1) == Tr);
if(Pr_match && Tr_match)
disp('Perfect match, no interpolation needed!')
Z0 = Z0_table(Tr_ind,Pr_ind);
elseif (Pr_match && ~ Tr_match)
disp('Need to interpolate temperature')
Tr_1 = Z0_table(Tr_ind,1)
Tr_2 = Z0_table(Tr_ind+1,1)
Z0_1 = Z0_table(Tr_ind,Pr_ind)
Z0_2 = Z0_table(Tr_ind+1,Pr_ind)
Z0 = interpolate(Z0_1,Z0_2,Tr_1,Tr_2,Tr)
elseif (~Pr_match && Tr_match)
disp('Need to interpolate pressure')
Pr_1 = Z0_table(1,Pr_ind)
Pr_2 = Z0_table(1,Pr_ind+1)
Z0_1 = Z0_table(Tr_ind,Pr_ind)
Z0_2 = Z0_table(Tr_ind+1,Pr_ind)
Z0 = interpolate(Z0_1,Z0_2,Pr_1,Pr_2,Pr)
else
disp('Bilinear interpolation needed')
Pr_1 = Z0_table(1,Pr_ind)
Pr_2 = Z0_table(1,Pr_ind+1)
Tr_1 = Z0_table(Tr_ind,1)
Tr_2 = Z0_table(Tr_ind+1,1)
Z01_1_1 = Z0_table(Tr_ind,Pr_ind)
Z01_1_2 = Z0_table(Tr_ind,Pr_ind+1)
Z01_2_1 = Z0_table(Tr_ind+1,Pr_ind)
Z01_2_2 = Z0_table(Tr_ind+1,Pr_ind+1)
Z0_1 = interpolate(Z0_1_1,Z0_2_1,Tr_1,Tr_2,Tr)
Z0_2 = interpolate(Z0_1_2,Z0_2_2,Tr_1,Tr_2,Tr)
Z0 = interpolate(Z0_1,Z0_2,Pr_1,Pr_2,Pr)
end
Z = Z0;
R = 8.3144;
P = P*100000
Molar_Volume = (R*T*Z)/P

Accepted Answer

Walter Roberson
Walter Roberson on 2 Oct 2023
Pr = P/Pc;
That value is being calculated -- and it is not an integer.
Pr_match = (Z0_table(1,Pr_ind) == Pr);
Z0_table is an array of floating point numbers that are not integers, and you are doing == comparisons against a calculated value. == between numeric values has the following rules:
  • false if either or both of the values are any of the forms of NaN
  • true if both of the values represent or both of the values represent
  • true if the numeric difference between the items is zero
  • false otherwise
In the case where both of the values are class single or class double the conditions can be rephrased as:
  • false if either or both of the values are any of the forms of NaN
  • true if both of the values are exactly 0, including if one is negative zero and the other is non-negative zero
  • true if the two values are bitwise exactly the same (excluding NaNs)
  • false otherwise
There is no approximation involved. If one of the values is equivalent to 0.333333333333333 and the other is equivalent to 0.333333333333334 then == will say they are not equal.
T = 1/3;
1-2*T == T %--> will be false!
Next:
Pr_ind = find(Z0_table(1,:)>Pr,1)-1;
What happens if there are no such entries and Pr_ind ends up empty?
  4 Comments
Walter Roberson
Walter Roberson on 2 Oct 2023
As a general rule:
  • never use == to compare floating point values that have been calculated two different ways, except possibly if they are definitely integers
  • consider using ismembertol() instead, or abs(a-b)<=tolerance instead of a==b
  • linspace() and the : operator probably do not work the way you expect them to, so if you use them with floating point values, the results might not be exactly what you expect
  • MATLAB does not use decimal computations, it uses binary. 1/10 cannot be exactly represented in any finite-length binary fixed-point or floating-point number representation. This is not a MATLAB bug, and it is not a "bug" that Mathworks chose binary instead of decimal. If you choose any finite-length "base" numeric representation, there will always be ratios that cannot be exactly represented. For example in decimal, if 1/3 = 0.333333333333333 then twice that is 0.666666666666666 and three times it is 0.999999999999999 -- so 3 * (1/3) is not exactly 1 and that would hold true even if you used 50000 decimal places. If you use "base N" arithmetic for any integer base N > 1, then the ratio 1/(N+1) cannot be exactly represented in a finite length. The problem is in mathematics, not in MATLAB.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!