Sei sulla pagina 1di 3

Chapter 2 Cubic Splines In mathematics, a spline is a sufficiently smooth polynomial function that is pi ecewise-defined, and possesses a high degree

of smoothness at the places where t he polynomial pieces connect (which are known as knots). A cubic spline is a spline constructed of piecewise third-order polynomials whic h pass through a set of m control points. The second derivative of each polynomi al is commonly set to zero at the endpoints, since this provides a boundary cond ition that completes the system of m-2 equations. This produces a so-called "nat ural" cubic spline and leads to a simple tridiagonal system which can be solved easily to give the coefficients of the polynomials. However, this choice is not the only one possible, and other boundary conditions can be used instead. The Spline Toolbox provided in MATLAB can be used to better solve the interpolation problems. Moreover, numerical differentiation and integration problems can be solved easily using this toolbox. Syntax: yy=spline(x,Y,xx) pp = spline(x,Y) Description: 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 interpolant xx. For the interpola tion, the independent variable is assumed to be the final dimension of Y with th e breakpoints defined by x. 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]. pp = spline(x,Y) returns the piecewise polynomial form of the cubic spline inter polant for later use with ppval and the spline utility unmkpp. x must be a vecto r. Y can be a scalar, a vector, or an array of any dimension, subject to the fol lowing conditions: If x and Y are vectors of the same size, the not-a-knot end conditions are used. If x or Y is a scalar, it is expanded to have the same length as the other and t he not-a-knot end conditions are used. (See Exceptions (1) below). 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. (See Exceptions (2) below.) Excemptions: 1. If Y is a vector that contains two more values than x has entries, the f irst 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) 2. If Y is a matrix or an N-dimensional array with size(Y,N) equal to lengt h(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 interpola tion on rows of an input matrix, interp1 performs interpolation on columns of an input matrix. Example 1: This generates a sine curve, and 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 s lopes 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.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: spline(t,p,2000) uses the cubic spline to extrapolate and predict the population in the year 2000 . The result is ans = 270.6060 Example 4: The statements , 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) marke d with o's. Note that this y contains two more values (i.e., two more columns) t han does x, hence y(:,1) and y(:,end) are used as endslopes. Example 5: The following code generates sine and cosine curves, and then samples the spline s 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;

Potrebbero piacerti anche