MATLAB Answers

# Interpolating between calling specific elements of the table

8 views (last 30 days)
Kosta on 7 Nov 2016
Commented: Kosta on 10 Nov 2016
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
##### 1 CommentShowHide None
Peter Perkins on 9 Nov 2016
Kosta, I think you're going to have to give a short example of exactly what you need to do.

Sign in to comment.

### Accepted Answer

Ahmet Cecen on 10 Nov 2016
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
##### 1 CommentShowHide None
Kosta on 10 Nov 2016
Thanks! I actually managed to use the interp1 function, it seems quite useful.

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!