Cubic spline data interpolation
yy = spline(x,Y,xx)
pp = spline(x,Y)
yy = spline(x,Y,xx) uses a
cubic spline interpolation to find
yy, the values
of the underlying function
Y at the values of the
xx. For the interpolation, the independent
variable is assumed to be the final dimension of
the breakpoints defined by
x. The values in
The sizes of
related as follows:
Y is a scalar or vector,
the same size as
Y is an array that is not a
xx is a scalar or vector,
d2, ..., dk, length(xx)].
xx is an array of size
pp = spline(x,Y) returns
the piecewise polynomial form of the cubic spline interpolant for
later use with
ppval and the spline utility
be a vector with distinct values.
Y can be a scalar,
a vector, or an array of any dimension, subject to the following conditions:
vectors of the same size, the not-a-knot end conditions are used.
a vector that contains two more values than
entries, the first and last value in
Y are used
as the endslopes for the cubic spline. If
a vector, this means
f(x) = Y(2:end-1)
df(min(x)) = Y(1)
df(max(x)) = Y(end)
a matrix or an
N-dimensional array with
length(x)+2, the following hold:
f(x(j)) matches the value
You can also perform spline interpolation using the
This generates a sine curve, then samples the spline over a finer mesh.
x = 0:10; y = sin(x); xx = 0:.25:10; yy = spline(x,y,xx); plot(x,y,'o',xx,yy)
This illustrates the use of clamped or complete spline interpolation where end slopes are prescribed. Zero slopes at the ends of an interpolant to the values of a certain distribution are enforced.
x = -4:4; y = [0 .15 1.12 2.36 2.36 1.46 .49 .06 0]; cs = spline(x,[0 y 0]); xx = linspace(-4,4,101); plot(x,y,'o',xx,ppval(cs,xx),'-');
The two vectors
t = 1900:10:1990; p = [ 75.995 91.972 105.711 123.203 131.669 ... 150.697 179.323 203.212 226.505 249.633 ];
represent the census years from 1900 to 1990 and the corresponding United States population in millions of people. The expression
uses the cubic spline to extrapolate and predict the population in the year 2000. The result is
ans = 270.6060
x = pi*[0:.5:2]; y = [0 1 0 -1 0 1 0; 1 0 1 0 -1 0 1]; pp = spline(x,y); yy = ppval(pp, linspace(0,2*pi,101)); plot(yy(1,:),yy(2,:),'-b',y(1,2:5),y(2,2:5),'or'), axis equal
generate the plot of a circle, with the five data points
y(:,2),...,y(:,6) marked with o's. Note that this
y contains two more values (i.e., two more columns) than does
y(:,end) are used as endslopes.
The following code generates sine and cosine curves, then samples the splines over a finer mesh.
x = 0:.25:1; Y = [sin(x); cos(x)]; xx = 0:.1:1; YY = spline(x,Y,xx); plot(x,Y(1,:),'o',xx,YY(1,:),'-') hold on plot(x,Y(2,:),'o',xx,YY(2,:),':') hold off
A tridiagonal linear system (with, possibly, several right sides)
is being solved for the information needed to describe the coefficients
of the various cubic polynomials which make up the interpolating spline.
unmkpp. These routines form a small suite
of functions for working with piecewise polynomials. For access to
more advanced features, see the
page, the command-line help for these functions, and the Curve Fitting Toolbox™ spline
 de Boor, C., A Practical Guide to Splines, Springer-Verlag, 1978.