How does interp1 work?

24 vues (au cours des 30 derniers jours)
massi
massi le 7 Avr 2015
Commenté : John D'Errico le 21 Juin 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 commentaire
Geoff Hayes
Geoff Hayes le 7 Avr 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?

Connectez-vous pour commenter.

Réponses (3)

Adam
Adam le 7 Avr 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 commentaire
massi
massi le 7 Avr 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);

Connectez-vous pour commenter.


John D'Errico
John D'Errico le 7 Avr 2015
Modifié(e) : John D'Errico le 7 Avr 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
  1 commentaire
massi
massi le 7 Avr 2015
OK thank you very much

Connectez-vous pour commenter.


Mritu Sivaraman
Mritu Sivaraman le 21 Juin 2018
Can anyone explain me how this interp function works? Thanks
  2 commentaires
Walter Roberson
Walter Roberson le 21 Juin 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 le 21 Juin 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interpolation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by