Sunteți pe pagina 1din 15

Cubic Splines

MACM 316

1/15

Cubic Splines
Given the following list of points: x : y : a = x0 < x1 < < xn = b y0 y1 yn

Basic idea: piecewise polynomial interpolation Use lower order polynomials that interpolate on each subinterval [xi, xi+1]. Force the polynomials to join up as smoothly as possible. Simplest example: a linear spline just connects the dots Denition: A cubic spline S (x) is a piecewise-dened function that satises the following conditions: 1. S (x) = Si (x) is a cubic polynomial on each subinterval [xi, xi+1] for i = 0, 1, . . . , n 1. 2. S (xi) = yi for i = 0, 1, . . . , n (S interpolates all the points). 3. S (x), S (x), and S (x) are continuous on [a, b] (S is smooth). So we write the n cubic polynomial pieces as Si (x) = ai + bi (x xi ) + ci(x xi)2 + di (x xi)3, for i = 0, 1, . . . , n 1, where ai, bi , ci, and di represent 4n unknown coefcients.
November 1, 2012 c Steven Rauch and John Stockie

Cubic Splines

MACM 316

2/15

Derivation
Using the denition, lets determine equations relating the coefcients and keep a count of the number of equations: # eqns. Interpolation and continuity: Si (xi) = yi for i = 0, 1, . . . , n 1 Si (xi+1) = yi+1 for i = 0, 1, . . . , n 1
(based on both of these, S (x) is continuous)

n n

Derivative continuity: (xi+1) = Si Si +1 (xi+1 ) for i = 0, 1, . . . , n 2


(xi+1) = Si Si +1 (xi+1 ) for i = 0, 1, . . . , n 2

n1 n1 4n 2

Total # of equations: There are still 2 equations missing! (later)

November 1, 2012

c Steven Rauch and John Stockie

Cubic Splines

MACM 316

3/15

Derivation (contd)
We need expressions for the derivatives of Si : Si(x) = ai + bi(x xi) + ci(x xi)2 + di (x xi)3
(x) = bi + 2ci(x xi) + 3di(x xi)2 Si (x) = 2ci + 6di(x xi) Si

It is very helpful to introduce the hi = xi+1 xi. Then the spline conditions can be written as follows: Si(xi) = yi for i = 0, 1, . . . , n 1: ai = y i Si(xi+1) = yi+1 for i = 0, 1, . . . , n 1:
3 ai + hi b i + h2 i ci + hi di = yi+1 (xi+1) = Si Si +1 (xi+1 ) for i = 0, 1, . . . , n 2:

b i + 2 hi c i + 3 h2 i di bi+1 = 0
(xi+1) = Si Si +1 (xi+1 ) for i = 0, 1, . . . , n 2:

2ci + 6hidi 2ci+1 = 0 The boxed equations above can be written as a large linear system for the 4n unknowns [a0, b0, c0, d0, a1, b1, c1, d1 , . . . , an1, bn1, cn1, dn1 ]T
November 1, 2012 c Steven Rauch and John Stockie

Cubic Splines

MACM 316

4/15

Alternate formulation
This linear system can be simplied considerably by dening
(x i ) = 2c i mi = Si

or

ci =

mi 2

and thinking of the mi as unknowns instead. Then:


(xi+1) = Si Si +1 (xi+1 ) for i = 0, 1, . . . , n 2:

= = =

2ci + 6hidi 2ci+1 = 0 mi + 6hidi mi+1 = 0 di =


mi+1 mi 6hi

Si(xi) = yi and Si (xi+1) = yi+1 for i = 0, 1, . . . , n 1: =


3 y i + hi b i + h2 i ci + hi di = yi+1

Substitute ci and di from above: = bi =


yi+1 yi hi

hi mi 2

hi (mi+1 6

mi)

(xi+1) = Si Si +1 (xi+1 ) for i = 0, 1, . . . , n 2:

b i + 2 hi c i + 3 h2 i di = bi+1

Substitute bi, ci and di from above and simplify: himi + 2(hi + hi+1 )mi+1 + hi+1 mi+2 = 6
November 1, 2012

yi+2 yi+1 hi+1

yi+1 yi hi

c Steven Rauch and John Stockie

Cubic Splines

MACM 316

5/15

Notice: These are n 1 linear equations for n + 1 unknowns [m0, m1, m2, . . . , mn]T where
mi = gi (x i ).

November 1, 2012

c Steven Rauch and John Stockie

Cubic Splines

MACM 316

6/15

Endpoint Conditions: Natural Spline


Now, well deal with the two missing equations . . . Note: Derivative matching conditions are applied only at interior points, which suggests applying some constraints on the derivative(s) at x0 and xn. There is no unique choice, but several common ones are 1. Natural or Free Spline (zero curvature): m0 = 0 and mn = 0

for taken together gives an (n + 1) (n + 1) system:


1 0 0 0 m0 h0 2(h0 + h1 ) h1 0 m 1 0 h1 2(h1 + h2 ) h2 0 m 2 . . . 0 0 h2 2(h2 + h3 ) h3 m3 . . . . . . . . . . . . . . . 0 hn2 2(hn2 + hn1 ) hn1 mn 0 0 0 1 0 y2 y1 y0 y1h h1 0 y2 y1 y3 y2 h h 2 1 y4 y3 y3 y2 = 6 h3 h2 . . . yn yn1 yn1 yn2 hn1 hn2 0

. . . or you could just drop the equations for m0 and mn and write it as an (n 1) (n 1) system.
November 1, 2012 c Steven Rauch and John Stockie

Cubic Splines

MACM 316

7/15

Interpretation of a Natural Spline

The word spline comes from the draftsmans spline, which is shown here constrained by 3 pegs. There is no force on either end to bend the spline beyond the last pegs, which results in a at shape (zero curvature). This is why the S = 0 end conditions are called natural.

November 1, 2012

c Steven Rauch and John Stockie

Cubic Splines

MACM 316

8/15

Endpoint Conditions: Clamped Spline


2. Clamped Spline, sometimes called a complete spline (derivative is specied):
S0 (x 0 ) = A

= = =

b0 = A A= y1 y0 h0 h0 2 m0 h0 6 (m1 m0) A

2h0m0 + h0m1 = 6

y1 y0 h0

and
Sn 1 (xn ) = B

bn1 = B yn yn1 hn1

hn1mn1 + 2hn1mn = 6 B

These two equations are placed in the rst/last rows


2h0 h0 0 0 . . h0 2(h0 + h1) h1 0 . . . 0 h1 2(h1 + h2) h2 0 . . ... ... ... . 0 . 0 0 0 hn2 2(hn2 + hn1 ) hn1 0 0 hn1 2hn1

and the rst/last RHS entries are modied accordingly.

November 1, 2012

c Steven Rauch and John Stockie

Cubic Splines

MACM 316

9/15

Endpoint Conditions: Not-A-Knot


3. Not-A-Knot Spline (third derivative matching):
S0 (x1) = S1 (x 1 )

and

Sn 2 (xn1 ) = Sn1 (xn1) mi+1 mi , 6hi

(x) = 6di and di = Using Si become

these conditions

h1(m1 m0 ) = h0(m2 m1) and hn1(mn1 mn2) = hn2(mn mn1) The matrix in this case is
h1 h0 + h1 h0 0 . . h0 2(h0 + h1 ) h1 0 . . . 0 h1 2(h1 + h2 ) h2 0 . . . . . . .. .. .. 0 . 0 0 0 hn2 2(hn2 + hn1 ) hn1 0 hn1 hn2 + hn1 hn2

and the rst/last RHS entries are zero. Note: Matlabs spline function implements the not-a-knot condition (by default) as well as the clamped spline, but not the natural spline. Why not? (see Homework #4).

November 1, 2012

c Steven Rauch and John Stockie

Cubic Splines

MACM 316

10/15

Summary of the Algorithm


Starting with a set of n + 1 data points (x 0 , y 0 ), (x 1 , y 1 ), (x 2 , y 2 ), . . . , (x n , y n ) 1. Calculate the values hi = xi+1 xi for i = 0, 1, 2, . . . , n 1. 2. Set up the matrix A and right hand side vector r as on page 7 for the natural spline. If clamped or not-a-knot conditions are used, then modify A and r appropriately. 3. Solve the (n + 1) (n + 1) linear system Am = r for the second derivative values mi. 4. Calculate the spline coefcients for i = 0, 1, 2, . . . , n 1 using: ai = y i bi = ci = di = yi+1 yi hi mi 2 mi+1 mi 6 hi hi 2 mi hi 6 (mi+1 mi )

5. On each subinterval xi x xi+1, construct the function gi (x) = ai + bi(x xi) + ci(x xi)2 + di (x xi)3

November 1, 2012

c Steven Rauch and John Stockie

Cubic Splines

MACM 316

11/15

Natural Spline Example


xi 4.00 4.35 4.57 4.76 5.26 5.88 yi 4.19 5.77 6.57 6.23 4.90 4.77 hi 0.35 0.22 0.19 0.50 0.62 Solve Am = r when
A= = 1 0 0 0 0 0.35 1.14 0.22 0 0 0 0 0.22 0.82 0.19 0 0 0 0 0.19 1.38 0.50 0 0 0 0 0.50 2.24 0.62 0 0 0 0 1 and r= 0 5.2674 32.5548 5.2230 14.7018 0

m = [0, 3.1762, 40.4021, 0.6531, 6.7092, 0]T

Calculate the spline coefcients:


xi y i = a i S0(x) 4.00 4.19 S1(x) 4.35 S2(x) 4.57 S3(x) 4.76 S4(x) 5.26 5.77 6.57 6.23 4.90 bi 4.3290 4.8848 ci 0 di Interval 1.5125 [4.00, 4.35]

1.5881 33.0139 [4.35, 4.57] 34.8675 [4.57, 4.76] 2.4541 [4.76, 5.26] 1.8035 [5.26, 5.88]

0.7900 20.2010 3.1102 1.5962 0.3266 3.3546

Then the spline functions are easy to read off, for example:
S2 (x) = 6.57 + 0.7900(x 4.57) 20.2010(x 4.57)2 + 34.8675(x 4.57)3 S3 (x) = 6.23 3.1102(x 4.76) 0.3266(x 4.76)2 + 2.4541(x 4.76)3

Exercise: Verify that S2 and S3 satisfy the conditions dening a cubic spline.
November 1, 2012 c Steven Rauch and John Stockie

Cubic Splines

MACM 316

12/15

Splines vs. Interpolation


7 6.5 6 5.5 5 4.5 4 4 y

Polynomial Natural Spline 4.5 x 5 5.5

The Newton divided difference table for the data points is


yi xi 4.00 4.19 4.35 5.77 3.6364 4.57 6.57 -1.7895 4.76 6.23 -2.6600 5.26 4.90 -0.2097 5.88 4.77 = P5 (x) = 4.19 + 4.5143(x 4) 1.5402(x 4)(x 4.35) . . .
c Steven Rauch and John Stockie

a1 4.5143

a2

a3

a4

a5

-1.5402 -15.3862 -13.2337 13.1562 -1.2616 2.6331 2.1878 -6.8778 22.6527 -15.7077

November 1, 2012

Cubic Splines

MACM 316

13/15

Clamped Spline Example


xi 4.00 4.35 4.57 4.76 5.26 5.88 yi 4.19 5.77 6.57 6.23 4.90 4.77 hi 0.35 0.22 0.19 0.50 0.62
(same data)

Assume S (4.00) = 1.0 and S (5.88) = 2.0 (clamped). Solve Am = r where


A= = 0.70 0.35 0 0 0 0 0.35 1.14 0.22 0 0 0 0 0.22 0.82 0.19 0 0 0 0 0.19 1.38 0.50 0 0 0 0 0.50 2.24 0.62 0 0 0 0 0.62 1.24 and r= 33.0858 5.2674 32.5548 5.2230 14.7018 10.7418

m = [54.5664, 14.6022, 35.0875, 3.0043, 11.1788, 14.2522]T

xi y i = a i bi S0(x) 4.00 4.19 1.0000 S1(x) 4.35 S2(x) 4.57 S3(x) 4.76 S4(x) 5.26 5.77 6.57 6.23 4.90 5.9937

ci di Interval 27.2832 32.9375 [4.00, 4.35] 7.3011 15.5191 [4.35, 4.57] 28.1431 [4.57, 4.76] 4.7277 [4.76, 5.26] 6.8363 [5.26, 5.88]

0.5279 17.5437 3.0908 1.0472 1.5021 5.5894

Exercise: Verify that the spline satises the clamped end conditions:
S0 (4.00) = 1.0 and S4 (5.88) = 2.0.
November 1, 2012 c Steven Rauch and John Stockie

Cubic Splines

MACM 316

14/15

Comparison
Below is a comparison of the splines with various endpoint conditions:
7 6.5 6 5.5 5 4.5 4 4 y Natural Clamped NotAKnot

4.5 x

5.5

November 1, 2012

c Steven Rauch and John Stockie

Cubic Splines

MACM 316

15/15

Second Example
Consider the following data representing the thrust of a model rocket versus time.
t 0.00 0.05 0.10 0.15 0.20 0.30 0.40 0.50 T 0.00 1.00 5.00 15.00 33.50 33.00 16.50 16.00 t 0.60 0.70 0.80 0.85 0.90 0.95 1.00 T 16.00 16.00 16.00 16.00 6.00 2.00 0.00

Below is a plot of the natural spline:


40 35 30 25 Thrust 20 15 10 5 0 0 0.2 0.4 time 0.6 0.8 1

Notice how wiggles are introduced near the ends of the at region non-smooth data cause some problems for cubic splines.
November 1, 2012 c Steven Rauch and John Stockie

S-ar putea să vă placă și