How does interp1 work?
16 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Geoff Hayes
2015 年 4 月 7 日
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?
回答 (3 件)
Adam
2015 年 4 月 7 日
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);
John D'Errico
2015 年 4 月 7 日
編集済み: John D'Errico
2015 年 4 月 7 日
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
2018 年 6 月 21 日
Can anyone explain me how this interp function works? Thanks
2 件のコメント
Walter Roberson
2018 年 6 月 21 日
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
2018 年 6 月 21 日
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!