0
votes

I am trying to calculate an integral using spline interpolation with matlab (version R2014a on windows 8).

I have the 3 values of the function (for x=0,0.5,1).

so I have 2 vectors - x and y that contain the values of the function, and I'm executing

cube_spline = spline(x,y);
coefficients = qube_spline.coefs

And I'm expecting to get 2 polynomials, each of degree 3, i.e I'm expecting coefficients to be a matrix of size 2*4, but somewhy I'm getting a matrix that is 1*4, which means only 1 polynomial for 2 panels.

On the other hand, if for example I'm using 4 dots, (i.e 3 panels) then I'm getting that coefficients's size is 3*4 as expected, which means 3 polynomials for 3 panels.

My question is Why does matlab return only 1 polynomial for 2 panels spline, and 3 polynomial for 3 panels spline (or any number that is greater then 2)?

1
spline is cubic spline interpolation, i.e. degree 3. I guess it's a typo that you expect a degree 4 polynomial (which would have 5 coefficients). - knedlsepp
If you only have three points there is a quadratic polynomial, that will interpolate your points. So the space of cubic curves that will interpolate your points will be one dimensional (infinitely many of those curves!). The function spline will chose one of those infinitely many curves by using some extra internal condition, like minimizing the second derivative or vanishing boundary conditions or something similar. Expecting two polynomials from three data points is just too much, as it can already be described by a single polynomial. - knedlsepp
@knedlsepp Your'e right, I meant 4 coefficient -> 3rd degree, thanks! (fixed the typo) - Hagai
@knedlsepp So Does cubic spline actually requires at least 4 points? I thought that the fact that we are requiring that the derivatives will be equal in the intersection points creates the required equations isn't it? - Hagai
Well it isn't "required to have 4 points" in the sense that with 3 points it won't work. But you will have to specify 4 points to get more than one piecewise polynomial. With 3 points you will only get a single polynomial, but that isn't a bad thing. The single polynomial will describe your points just fine. With more than 3 points certainly those required equations will come into play. - knedlsepp

1 Answers

1
votes

There are multiple possible boundary conditions for splines, e.g.:

  • second derivatives equals zero on the boundary
  • given first derivatives on the boundary
  • periodic conditions, i.e. same first and second derivatives on the boundary
  • not-a-knot: take the outermost three points to specify the boundary conditions.

It seems spline is using the not-a-knot condition by default. So for three points only a single cubic polynomial is necessary to interpolate your data (a quadratic one would be enough too if it weren't for the not-a-knot condition), so there's no reason for spline to return one spline for each of the two intervals. This is however not a bad thing.

By the way: If all you want is to interpolate the values and don't need the polynomial coefficients, you could go with interp1 instead. You can specify in a simpler way which kind of discontinuities you want. You have the options to go with:

'pchip': C^1 continuity.

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.

integral(@(xs) interp1(x, y, xs, 'pchip'),  xmin, xmax)

'spline': C^2 continuity. (Seems to be using the same not-a-knot end conditions as 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.

integral(@(xs) interp1(x, y, xs, 'spline'), xmin, xmax)