Different commands to do spline interpolation, are they the same?
23 views (last 30 days)
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:
pp1 = spline(x,y);
-6.2832 -4.8869 -3.4907 -2.0944 -0.6981 0.6981 2.0944 3.4907 4.8869 6.2832
-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
Thanks in advance!
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)
spl2 = spapi(4,x,y)
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')
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)
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.