Interpolating between calling specific elements of the table

2 次查看(过去 30 天)
Hello,
I have created this table of thermodynamic properties, however, I would like to be able to obtain a linearly interpolated value whenever the property I insert on my function is in between two values as listed on the table. I have tried different things such as 'if' function, but i cannot even start getting the progam to run with those.
The way the function works is I insert a pressure value for example instead of P, and a desired column from which i would like to obtain the property: i.e. R134a(80,'temp').
Any help would be greatly appreciated. Thanks in advance!
Below is the function:
function out=R134a(P,a)
M=[60 -36.9 0.0007098 0.3112 3.8 209.1 3.9 223.9 227.8 0.0164 0.9481 0.9645; 80 -31.1 0.0007185 0.2376 11.2 212.5 11.2 220.2 231.5 0.0472 0.9100 0.9572];
T=array2table(M,'variablenames',{'pres', 'temp','v_f','v_g', 'u_f', 'u_g', 'h_f', 'h_fg', 'h_g', 's_f', 's_fg', 's_g'}); [I,J]=find(M==P); T(I,a)
end

采纳的回答

Ahmet Cecen
Ahmet Cecen 2016-11-10
Had something like this already, here it is:
function out=R134a(P,a)
% Your Table
M=[60 -36.9 0.0007098 0.3112 3.8 209.1 3.9 223.9 227.8 0.0164 0.9481 0.9645; ...
80 -31.1 0.0007185 0.2376 11.2 212.5 11.2 220.2 231.5 0.0472 0.9100 0.9572];
T=array2table(M,'variablenames',{'pres', 'temp','v_f','v_g', 'u_f', 'u_g', 'h_f', 'h_fg', 'h_g', 's_f', 's_fg', 's_g'});
% Find the location of 2 nearest Pressure Values in the Table - This can accomodate
% larger tables.
lowerPind = max(find( M(:,1) <= P ));
higherPind = min(find( M(:,1) >= P ));
% Find the interpolated value
if higherPind ~= lowerPind
out = T{lowerPind,a} + (P-M(lowerPind,1))*(T{higherPind,a} - T{lowerPind,a}) /...
(M(higherPind,1) - M(lowerPind,1));
else
out = T{lowerPind,a};
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by