Why the plots of spline and cubic interpolation are exactly same? [closed]

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?

clc
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');
plot(x,v,'o',xq,vq2,':.');

• 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. – Ben Jun 27 '20 at 19:47
• 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. – Marcus Müller Jun 27 '20 at 20:53
• 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? – TimWescott Jun 28 '20 at 4:19
• @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? – engr Jun 28 '20 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):

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');
plot(x,v,':.k',xq,vq1,'.r',xq,vq2,'ob');
legend('Original','Spline','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.

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.