Interpolation on tables using matlab

Hello, I am solving several probems in Thermodynamics and one of my most time consuming things solving problems is the interpolation.
That not exactly the same as an interpolation on a plot, so thats why Im writing a new post.
I had my exam and succeed in MatLab last week but was a short tour of 15 hours so Im still new at this! :)
So the thing is I have a table giving several temperatures and for the given temperatures I have columns next to them giving the related energy, enthalpy and entropy.
So if I have temperature that is not given on the table I have to do an interpolation for the temperature I need, so I was thinking about a function that could read the 3 temperatures as x1 x2 x3 then read the 2 energys lets say as y1 y3 and I will need to calculate the y2 which is what Im looking for.
The equation Im using on my calculator looks like y2={[(x2-x1)/(x3-x1)]*(y3-y1)}+y1
I know is simple but Im just too short on time and cant get it to work :(

Answers (1)

If you are using MATLAB, just use the interp1 function. Just be sure to follow the expected syntax, described in the linked documentation page.

7 Comments

Thank you I wasnt sure if it will work as I coundnt understand fully the descreption I will check it thanks!
Syntax splits the knowns and unknowns.
% known x,y values
x = [1 3];
y = [2 6];
% Interporate to find y when x=2.
xq = 2;
yq = interp1(x,y,xq)
yq = 4
% Just to check, plot results
plot(x,y)
hold on
plot(xq,yq,'ro')
unfortunatly this is not faster than calculating by hand.
Iv seen a function in Fortran that you could just type in 5 values and it calculates the 6th value for you.
Like your example will be 1 (2)3 2 6 and the function knows the order and has the equation in the memory solving for 2, each time you give a number you just press enter , the program was like: read x1 read x2 ... etc, can something similar be done on matlab?
You could write your own function to do whatever you want.
Not sure what is making this so slow for you. I would argue that interp1 also takes 5 values and calculates a 6th.
yq = interp1([1 3],[2 6],2)
yq = 4
yes it takes the value and calculates the 6th, but is just way more time consuming than in fortran, I just cant instal fortran on my laptop or I would so I tought of trying to do it in matlab.
fortran program looks like
program interpolation
implicit none
real x1,x2,x3,y1,y3,y2
print*, ''First the 3 known values then the 2 values than contain the unknown value in the middle
read*,x1
read*,x2
read*,x3
read*,y1
read*,y3
y2=(((x2-x1)/(x3-x1))*(y3-y1))+y1
print*, y2
end program interpolation
hope this helps,
I tried writing something simillar but it wont work thats why I thought maybe someone can help on this one.
I tried this on a friends laptop and this takes just a few secs you just type in the values in order and it gives the y2 value in no time
Like I said, you can write a similar function in MATLAB if you are particular about the calling syntax. You could write that same function in 3 lines in MATLAB. If that's what you prefer, see this documentation page on how to create a function.
Thanks I will try it!

Sign in to comment.

Categories

Products

Asked:

on 13 Dec 2020

Commented:

on 13 Dec 2020

Community Treasure Hunt

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

Start Hunting!