Clear Filters
Clear Filters

How does interp1 work?

16 views (last 30 days)
massi
massi on 7 Apr 2015
Commented: John D'Errico on 21 Jun 2018
Hello, I am not understanding why this little program for interpolation of sin(x) changing the number of points, is not working. Could you help me, please?
x = 0:10;
y = sin(x);
xi = 0:.25:10;
yi = interp1(x,y,xi);
plot(x,y,'o',xi,yi)
Thanks Bye massi
  1 Comment
Geoff Hayes
Geoff Hayes on 7 Apr 2015
Massi - please clarify what you mean by is not working. Are you suggesting that if you change the number of points then there is no change to the plot? Re you expecting a better solution if you increase the number of points?

Sign in to comment.

Answers (3)

Adam
Adam on 7 Apr 2015
Default interpolation is linear. If you add in 3 points between every pair of points and then linearly interpolate you will just get extra points on the same lines that you had before.
'spline' or 'pchip' will provide some difference.
interpolating a function like that seems a waste of time though as sin(x) itself will do the interpolation job if rerun as:
yi = sin(xi);
  1 Comment
massi
massi on 7 Apr 2015
yes thank you, I did it only to try if I am using this function correctly, but I obtain every time the following error:
Error in interpol (line 4) yi = interp1(x,y,xi);

Sign in to comment.


John D'Errico
John D'Errico on 7 Apr 2015
Edited: John D'Errico on 7 Apr 2015
DON'T name a script (or function) interp1. Don't replace existing tools in MATLAB, as then you will later have problems, just like this.
You clearly did so, in a way that apparently calls interpol, a tool that you found on the file exchange. See that this is the error message fragment that is reported by you. It clearly mentions interpol, as called by interp1. In fact, interp1 NEVER calls a function that they don't supply.
"Error in interpol (line 4) yi = interp1(x,y,xi);"
So now you are asking how interp1 works, but really, it is using interpol, a perhaps lesser quality tool that you got from the FEX.
Try these things, and see what it gives you.
which interp1 -all
which interpol -all
type interp1

Mritu Sivaraman
Mritu Sivaraman on 21 Jun 2018
Can anyone explain me how this interp function works? Thanks
  2 Comments
Walter Roberson
Walter Roberson on 21 Jun 2018
interp1() offers a number of different interpolation methods. Which one are you interested in?
Also, is the question about interp1() or about interp2()?
John D'Errico
John D'Errico on 21 Jun 2018
There are MANY methods inside interp1:
'linear' - (default) linear interpolation
'nearest' - nearest neighbor interpolation
'next' - next neighbor interpolation
'previous' - previous neighbor interpolation
'spline' - piecewise cubic spline interpolation (SPLINE)
'pchip' - shape-preserving piecewise cubic interpolation
'cubic' - same as 'pchip'
'v5cubic' - the cubic interpolation from MATLAB 5, which does not
extrapolate and uses 'spline' if X is not equally
spaced.
'makima' - modified Akima cubic interpolation
To answer this question could take many hours to write. Some of those methods are alone worth many hours to write a complete explanation. So, if you want to understand interp1, then read the documentation for interp1. At the end, you will find references.
Or, ask a SPECIFIC question, about a SPECIFIC method in interp1.

Sign in to comment.

Categories

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