# Different commands to do spline interpolation, are they the same?

16 views (last 30 days)
Mohammad Shojaei Arani on 13 Aug 2022
Edited: Bruno Luong on 14 Aug 2022
Hello,
I have a question about (cubic) spline interpolation in MATLAB. For cubic spline interpolation I see several commands which 'seem' to do the same job but I am not 100% sure and this is why I am asking you. To be more clear consider the two commands pp = spapi(4,x,y) and pp = spline(x,y) (there are also the commands pp = csapi(x,y) and pp = interp1(x,v,'spline','pp') but let's skip these for simplicity). My question is : "Are these two commands equivalent?" If I trust my eyes, the answer is 'Yes' to me but, are they really the same?
One issue that makes me to doubt is that I see that the mentioned commands use rather different knot sequesnces. For instance, consider the following example:
x=linspace(-2*pi,2*pi,10);y=sin(x);
pp1 = spline(x,y);
pp2=spapi(4,x,y);
pp1.breaks
pp2.knots
ans =
-6.2832 -4.8869 -3.4907 -2.0944 -0.6981 0.6981 2.0944 3.4907 4.8869 6.2832
ans =
-6.2832 -6.2832 -6.2832 -6.2832 -3.4907 -2.0944 -0.6981 0.6981 2.0944 3.4907 6.2832 6.2832 6.2832 6.2832
Babak

John D'Errico on 13 Aug 2022
Edited: John D'Errico on 13 Aug 2022
Can two different functions exist that happen to do the same thing? Of course. MATLAB is a complicated language, that was created over the course of MANY years. For many of its users, MATLAB was around before they were born. Yet at the same time, MATLAB needs to support legacy code. So functions I wrote 35 years ago will often still work.
Consider these two examples, ones that you gave yourself:
x = 0:6;
y = sin(x);
spl1 = spline(x,y)
spl1 = struct with fields:
form: 'pp' breaks: [0 1 2 3 4 5 6] coefs: [6×4 double] pieces: 6 order: 4 dim: 1
spl2 = spapi(4,x,y)
spl2 = struct with fields:
form: 'B-' knots: [0 0 0 0 2 3 4 6 6 6 6] coefs: [0 0.7637 1.3839 0.1669 -1.2562 -1.0307 -0.2794] number: 7 order: 4 dim: 1
Yes, both create an interpolating cubic spline. But are they the same? HUH? Are you kidding? Did you look at what they created? Did you read the help?
Spline produces a pp form, so the piecewise polynomial form, whereas spapi produces a B-form. You can convert the B-form into a pp form.
pp2 = fn2fm(spl2,'pp')
pp2 = struct with fields:
form: 'pp' breaks: [0 2 3 4 6] coefs: [4×4 double] pieces: 4 order: 4 dim: 1
Still an interpolating cubic spline, but do you see here they are on a different set of breaks? Breaks were actually not needed at locations 1 and 5, here because the not-a-knot end conditions were chosen by default. This can get a bit deep, so I'll stop at that point before I write 10 pages of explanation. Oh wait, see my comment below, and read the book. Actually a pretty good book. I have a copy on my shelves.
Even so, spapi is a more complex code, that can do more than just a simple cubic spline. For example, we can create a piecewise linear interpolating spline. A bit of overkill to do that, but we can.
spl0 = spapi(2,x,y)
spl0 = struct with fields:
form: 'B-' knots: [0 0 1 2 3 4 5 6 6] coefs: [0 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794] number: 7 order: 2 dim: 1
fnplt(spl0) And spapi is not limited to problems in one dimension. Spline is. Those things are outside of the scope of the function spline. spline is essentially a much simpler function.
As far as the functions csapi and interp1 go, they also have different uses, different behaviors, different abilities. Is there some overlap between them? Yes. But teaching the complete differences between them all would require writing a complete book on splines here. Oh. Wait. There are already books written on splines, one of which was written by de Boor, who was the source of many of these spline codes, at least long ago.
Remember that as MATLAB has grown over the years, new functions were introduced with more capability, that in some cases overlaps with the older ones. Yet they cannot just drop the older codes on a whim, as now too many legacy codes will fail to run for no good reason other than that someone wanted to make an improvement. And it is often the case that one cannot reasonably expand the functionality of an older tool, as that would make the interface and documentation far too complicated. So we end up with some degree of overlap. Such is life.
Mohammad Shojaei Arani on 14 Aug 2022
ok. Thanks!