MATLAB's Cubic Spline Interpolation Function: Syntax, Description, and Examples - Prof. Er, Study notes of Mathematical Methods for Numerical Analysis and Optimization

An overview of matlab's spline function, which is used for cubic spline data interpolation. The syntax, description, exceptions, examples, and algorithm of the function. It also mentions related functions such as interp1, ppval, mkpp, and unmkpp.

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-eb0
koofers-user-eb0 🇺🇸

5

(1)

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
spline :: Functions (MATLAB Function Reference) text://0
1 of 4 10/18/2007 2:52 PM
MATLAB Function Reference
spline
Cubic spline data interpolation
Syntax
pp = spline(x,Y)
yy = spline(x,Y,xx)
Description
pp = spline(x,Y) returns the piecewise polynomial form of the cubic spline interpolant for later use with ppval and
the spline utility unmkpp. x must be a vector. Y can be a scalar, a vector, or an array of any dimension, subject to the
following conditions:
If Y is a scalar or vector, it must have the same length as x. A scalar value for x or Y is expanded to have the same
length as the other. See Exceptions (1) for an exception to this rule, in which the not-a-knot end conditions are
used.
If Y is an array that is not a vector, the size of Y must have the form [d1,d2,...,dk,n], where n is the length of
x. The interpolation is performed for each d1-by-d2-by-...-dk value in Y. See Exceptions (2) for an exception to
this rule.
yy = spline(x,Y,xx) is the same as yy = ppval(spline(x,Y),xx), thus providing, in yy, the values of the
interpolant at xx. xx can be a scalar, a vector, or a multidimensional array. The sizes of xx and yy are related as follows:
If Y is a scalar or vector, yy has the same size as xx.
If Y is an array that is not a vector,
If xx is a scalar or vector, size(yy) equals [d1, d2, ..., dk, length(xx)].
If xx is an array of size [m1,m2,...,mj], size(yy) equals [d1,d2,...,dk,m1,m2,...,mj].
Exceptions
If Y is a vector that contains two more values than x has entries, the first and last value in Y are used as the
endslopes for the cubic spline. If Y is a vector, this means
f(x) = Y(2:end-1)
df(min(x)) = Y(1)
df(max(x)) = Y(end)
1.
If Y is a matrix or an N-dimensional array with size(Y,N) equal to length(x)+2, the following hold:
f(x(j)) matches the value Y(:,...,:,j+1) for j=1:length(x)
Df(min(x)) matches Y(:,:,...:,1)
Df(max(x)) matches Y(:,:,...:,end)
2.
Note You can also perform spline interpolation using the interp1 function with the command
interp1(x,y,xx,'spline'). Note that while spline performs interpolation on rows of an input matrix,
interp1 performs interpolation on columns of an input matrix.
Examples
Example 1
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)
pf3
pf4

Partial preview of the text

Download MATLAB's Cubic Spline Interpolation Function: Syntax, Description, and Examples - Prof. Er and more Study notes Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

MATLAB Function Reference

spline

Cubic spline data interpolation

Syntax

pp = spline(x,Y) yy = spline(x,Y,xx)

Description

pp = spline(x,Y) returns the piecewise polynomial form of the cubic spline interpolant for later use with ppval and the spline utility unmkpp. x must be a vector. Y can be a scalar, a vector, or an array of any dimension, subject to the following conditions:

If Y is a scalar or vector, it must have the same length as x. A scalar value for x or Y is expanded to have the same length as the other. See Exceptions (1) for an exception to this rule, in which the not-a-knot end conditions are used. If Y is an array that is not a vector, the size of Y must have the form [d1,d2,...,dk,n], where n is the length of x. The interpolation is performed for each d1-by-d2-by-...-dk value in Y. See Exceptions (2) for an exception to this rule.

yy = spline(x,Y,xx) is the same as yy = ppval(spline(x,Y),xx), thus providing, in yy, the values of the interpolant at xx. xx can be a scalar, a vector, or a multidimensional array. The sizes of xx and yy are related as follows:

If Y is a scalar or vector, yy has the same size as xx. If Y is an array that is not a vector, If xx is a scalar or vector, size(yy) equals [d1, d2, ..., dk, length(xx)]. If xx is an array of size [m1,m2,...,mj], size(yy) equals [d1,d2,...,dk,m1,m2,...,mj].

Exceptions

If Y is a vector that contains two more values than x has entries, the first and last value in Y are used as the endslopes for the cubic spline. If Y is a vector, this means f(x) = Y(2:end-1) df(min(x)) = Y(1) df(max(x)) = Y(end)

If Y is a matrix or an N-dimensional array with size(Y,N) equal to length(x)+2, the following hold: f(x(j)) matches the value Y(:,...,:,j+1) for j=1:length(x) Df(min(x)) matches Y(:,:,...:,1) Df(max(x)) matches Y(:,:,...:,end)

Note You can also perform spline interpolation using the interp1 function with the command interp1(x,y,xx,'spline'). Note that while spline performs interpolation on rows of an input matrix, interp1 performs interpolation on columns of an input matrix.

Examples

Example 1

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)

Example 2

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),'-');

Example 3

The two vectors

t = 1900:10:1990; p = [ 75.995 91.972 105.711 123.203 131. ... 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

spline(t,p,2000)

uses the cubic spline to extrapolate and predict the population in the year 2000. The result is

ans =

Algorithm

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. spline uses the functions ppval, mkpp, and unmkpp. These routines form a small suite of functions for working with piecewise polynomials. For access to more advanced features, see the M-file help for these functions and the Spline Toolbox.

See Also

interp1, ppval, mkpp, pchip, unmkpp

References

[1] de Boor, C., A Practical Guide to Splines , Springer-Verlag, 1978.

spinmap spones

© 1984-2007 The MathWorks, Inc. • Terms of Use • Patents • Trademarks • Acknowledgments