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

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

 Accepted Answer

3 Comments

Hello Torsten,
Thanks for your kind help!
I studied the link you sent me and saw contacting opinions over the speeds of the 3 commands spline(x,y,xq), interp1(x,y,xq,'spline'), griddedInterpolant(x,y,'spline'). In the following test I find that interp1 and griddedInterpolant have similar execution times (with interp1 being slightly better) and both outperform spline(x,y,xq) greatly. But, no one confirms this!!! Why? !!!
Do you agree with me?
x=linspace(-2*pi,2*pi,20);y=sin(x);
xq=linspace(-2*pi,2*pi,10000);
N=10000;
tic;
for n=1:N
y1=spline(x,y,xq);
end
t1=toc;
tic;
for n=1:N
y2=interp1(x,y,xq,'spline');
end
t2=toc;
tic;
for n=1:N
F=griddedInterpolant(x,y,'spline');yq=F(xq);
end
t3=toc;
t1=t1/N;t2=t2/N;t3=t3/N;
[t1 t2 t3]
ans =
0.00075666 4.3467e-05 5.4321e-05
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
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!

Sign in to comment.

More Answers (1)

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 Comments

Can you think of any reason why
spline(x,y,xq)
doesn't just call interp1 or that low-level implementation directly?
Thanks Bruno for your kind explanation!
spline is designed as two-step methods : construct the ppform then evalute using pp-form to query value, a little bit like griddedInterpolant. interp1 is designed to return directly query values.
IIRC interp1 uses to be slow. TMW optimizes it att some point.
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) ?
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.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!