Difference between spline(x,y,xq) and interp1(x,y,xq,'spline')

조회 수: 19 (최근 30일)
Mohammad Shojaei Arani
Mohammad Shojaei Arani 2022년 6월 5일
댓글: Bruno Luong 2022년 6월 5일
Hello friends,
I have a simple question. I am wondering what is the difference between the commands spline(x,y,xq) and interp1(x,y,xq,'spline'). They
should do the same job but they strongly differ from each other by their different execution times. interp1(x,y,xq,'spline') is way faster. I
have some codes which are computationally expensive. So, if I use the command interp1(x,y,xq,'spline') then I can reduce the computational time greatly.
Do spline(x,y,xq) and interp1(x,y,xq,'spline') differe in their accuracies? I hope interp1(x,y,xq,'spline') is more accurate but I guess that spline(x,y,xq) is more accurate since in this world nothing is for free!
Thanks in advance,
Babak

채택된 답변

Torsten
Torsten 2022년 6월 5일
  댓글 수: 3
Torsten
Torsten 2022년 6월 5일
편집: Torsten 2022년 6월 5일
Didn't you see in the link that you only have to call "spline" once to calculate the spline coefficients and then use "ppval" to evaluate the spline in the query points ?
But you are right: Testing it with MATLAB online, it's still the most time-consuming option. I don't know why.
tic
S = spline(x,y);
for i=1:N
y1 = ppval(S,xq);
end
toc
Mohammad Shojaei Arani
Mohammad Shojaei Arani 2022년 6월 5일
Thanks Torsten!
Yes, I carefully read the link you sent me but some of the stuff did not make sense to me and were very much in contrast with my test. You are right, the best is to do the test, no matter what happens inside matlab!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bruno Luong
Bruno Luong 2022년 6월 5일
spline is a MATLAB file that build the intermediate piecewise polynomial then evalutae it (in your case).
INTERP1 call an internal low-level implementation, perhaps no need the intermediate pp strcuture. Thus it is just faster. There is no accuracy penalty.
Both has matlab wrapper, you can follow with the debugger.
  댓글 수: 5
Paul
Paul 2022년 6월 5일
Yes, but the three-input form of spline is designed to return the result at the query values, just like interp1.
Also, based on @Mohammad Shojaei Arani results in this comment, even the explicit two-step process using griddedInterpolant is an order of magnitude better than the two-step process of spline(x,y,xq).
Absent other information, do you agree that TMW can improve spline(x,y,xq) ?
Bruno Luong
Bruno Luong 2022년 6월 5일
They could, but there are also many reasons why they keep it that way :
  • to warranty consistency results in 2-step and 1-step call of spline
  • to have the same code less dependant the rest, which is also good source mainenace, for not breaking things wespecially hen one forget what depends on what.
If was them I don't make any cross call like what tey decide to do.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by