I am trying to watch difference between cubic interpolation and spline interpolation using matlab plot but i am getting same plots in both cases using interp1 command

My code is below. In below code ,if i use spline in place of cubic, i still exactly get same plot,why?

clear all
close all
x = 0:pi/4:2*pi; 
v = sin(x);
xq = 0:pi/16:2*pi;
vq2 = interp1(x,v,xq,'cubic');
  • 2
    $\begingroup$ The purpose of splines is to fit multiple splines for a given set of points.And the spline used is typically cubic. While cubic interpolation just means that you have fitted a cubic curve over some data points. $\endgroup$ – Ben Jun 27 at 19:47
  • $\begingroup$ Hi Engr! What have you researched so far. interp1 has, like every matlab function, excellent documentation, and it tells you what cubic (==pchip) and spline do. You'll notice that you can infer directly under which condition of your data these two methods are identical. However, you don't reference any research into what is done for interpolation here, so I'm not sure what the actual question is. $\endgroup$ – Marcus Müller Jun 27 at 20:53
  • $\begingroup$ Do you mean they look identical, or they are identical? What do you see when you generate cubic and spline interpolations and plot their difference? $\endgroup$ – TimWescott Jun 28 at 4:19
  • $\begingroup$ @TimWescott yes,you are right. The both plots appear to be identical but they aren't exactly ,when we see their vq matrix value, but question is how we can make them look different in plots? $\endgroup$ – engr Jun 28 at 16:57

To emphasize how two point sets differ, using dot and circle markers can be useful, because uneven centering is quite visible (from the comments):

Dots and Circles

and the code is here:

x = 0:pi/4:2*pi; 
v = sin(x);
xq = 0:pi/16:2*pi;
vq1 = interp1(x,v,xq,'spline');
vq2 = interp1(x,v,xq,'pchip');
axis tight; grid on

The cubic option will be renamed as pchip, and reuse for other purposes in Matlab, because of the potential misinterpretations:

  • pchip: Shape-preserving piecewise cubic interpolation. The interpolated value at a query point is based on a shape-preserving piecewise cubic interpolation of the values at neighboring grid points.
  • spline: Spline interpolation using not-a-knot end conditions. The interpolated value at a query point is based on a cubic interpolation of the values at neighboring grid points in each respective dimension.
  • cubic: Note The behavior of interp1(...,'cubic') will change in a future release. In a future release, this method will perform cubic convolution.
| improve this answer | |

The best way to understand cubic splines (under whatever name or how specified) is to derive and code them yourself.

What you are looking for is a parametric equation with these properties:

$$ \begin{aligned} f(0) &= y[n] = y_0\\ f(1) &= y[n+1] = y_1\\ f(2) &= y[n+2] = y_2\\ f(3) &= y[n+2] = y_3\\ \end{aligned} $$

Your candidate function is a cubic polynomial, of course.

$$ f(t) = a t^3 + b t^2 + c t + d $$

Plug 'em in. You get four equations with four unknowns ($a,b,c,d$)

Once you have solved the equation, use it to interpolate between the middle two points.

Extra credit:

Do the vector equivalent in N-space with the conditions given as two points in space and the velocity vectors at each point with one unit of time to get there.

Then code your solutions. Print the results so you can see some precision. And compare your answer to Matlab's.

Then post your results here.

| improve this answer | |
  • $\begingroup$ uhm, Ced, cubic splines don't need to be Lagrange. in fact i would recommend against it. "splines" mean a lotta things to different people, but i think usually cubic splines are Hermite polynomials. $\endgroup$ – robert bristow-johnson Jun 27 at 23:40
  • $\begingroup$ @robertbristow-johnson I'm not understanding your point (pun alert). A cubic interpolation function can be parameterized or specified in a whole matter of ways, but in the end, it is the same set of points. Would you like a trig formulation? I don't use Matlab, so I can't help with the differnent versions there. But MM's advice stands well there. $\endgroup$ – Cedron Dawg Jun 27 at 23:56
  • $\begingroup$ @robertbristow-johnson Remember this? dsp.stackexchange.com/questions/58032/… $\endgroup$ – Cedron Dawg Jun 28 at 0:04
  • $\begingroup$ @CedronDawg uff! I didn't even remember that! :D $\endgroup$ – Marcus Müller Jun 28 at 0:16
  • 1
    $\begingroup$ @MarcusMüller Thanks. Truthfully, I tend to rederive it rather than look it up in the rare occassions I use it. (Pssst. Let engr figure out for himself/herself that it is a lot easier to solve and use on the domain of -1,0,1,2) $\endgroup$ – Cedron Dawg Jun 28 at 0:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.