Main Content

Cubic spline interpolation

**Note**

For a simpler but less flexible method to interpolate cubic splines,
try the Curve Fitting app or the `fit`

function and see
About Smoothing Splines.

returns the ppform of a cubic spline `pp`

=csapi(`x`

,`y`

)*s* with knot sequence
`x`

that takes the values `y(:,j)`

at
`x(j)`

for `j=1:length(x)`

. The values
`y(:,j)`

can be scalars, vectors, matrices, and ND-arrays.
The function averages the data points with the same data site and then sorts
them by their sites. With `x`

the resulting sorted data sites,
the spline `s`

satisfies the not-a-knot end conditions, such as

$$jum{p}_{x(2)}{D}_{s}^{3}=0=jum{p}_{x(end-1)}{D}^{3}(s)$$

where
*D*^{3}*s* is the
third derivative of *s*.

If `x`

is a cell array of sequences `x1`

,
..., `xm`

of lengths `n1`

, ...,
`nm`

, then `y`

is an array of size
`[n1,...,nm]`

(or of size `[d,n1,...,nm]`

if the interpolant is `d`

-valued). In that case,
`pp`

is the ppform of an `m`

-cubic spline
interpolant *s* to such data. In particular,

$$s\left(x({i}_{1}),\cdots ,x({i}_{m})\right)=y\left(:,{i}_{1},\dots ,{i}_{m}\right)$$

with $${i}_{1}=1:nl$$ and $${i}_{m}=1:nm$$.

To perform operations on this interpolating cubic spline, such as evaluation,
differentiation, plotting, use the pp structure. For more information, see the
`fnval`

, `fnder`

, `fnplt`

functions.

returns the values of the smoothing spline evaluated at the points
`values`

= csapi(`x`

,`y`

,`xx`

)`xx`

. This syntax is the same as
`fnval(csapi(x,y),xx)`

.

This command is essentially the MATLAB^{®} function `spline`

, which, in turn, is a
stripped-down version of the Fortran routine `CUBSPL`

in
*PGS*, except that `csapi`

(and now also
`spline`

) accepts vector-valued data and can handle gridded
data.

`csapi`

is an implementation of the Fortran routine
`CUBSPL`

from *PGS*.

The algorithm constructs and solves the relevant tridiagonal linear system using the MATLAB sparse matrix capability.

The algorithm also uses the not-a-knot end condition, forcing the first and second polynomial piece of the interpolant to coincide, as well as the second-to-last and the last polynomial piece.