Search code examples
matlabsplineintegral

Calculate cubic spline integral in matlab with 3 values


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)?


Solution

  • 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)