15
Cubic Splines MACM 316 1/15 Cubic Splines Given the following list of points: x : a = x 0 <x 1 < ··· <x n = b y : y 0 y 1 ··· y n Basic idea: “piecewise polynomial interpolation” Use lower order polynomials that interpolate on each subin- terval [x i ,x i+1 ]. Force the polynomials to join up as smoothly as possible. Simplest example: a linear spline just “connects the dots” Definition: A cubic spline S (x) is a piecewise-defined function that satisfies the following conditions: 1. S (x)= S i (x) is a cubic polynomial on each subinterval [x i ,x i+1 ] for i =0, 1,...,n 1. 2. S (x i )= y i 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 S i (x)= a i + b i (x x i )+ c i (x x i ) 2 + d i (x x i ) 3 , for i =0, 1,...,n 1, where a i ,b i ,c i , and d i represent 4n un- known coefficients. November 1, 2012 c Steven Rauch and John Stockie

Cubic Spline 1

  • Upload
    rex-liu

  • View
    59

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Cubic Spline 1

Cubic Splines MACM 316 1/15

Cubic SplinesGiven the following list of points:

x : a = x0 < x1 < · · · < xn = b

y : y0 y1 · · · yn

➤ Basic idea: “piecewise polynomial interpolation”

• Use lower order polynomials that interpolate on each subin-terval [xi, xi+1].

• Force the polynomials to join up as smoothly as possible.

➤ Simplest example: a linear spline just “connects the dots”

Definition: A cubic spline S(x) is a piecewise-defined functionthat satisfies 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 un-known coefficients.

November 1, 2012 c© Steven Rauch and John Stockie

Page 2: Cubic Spline 1

Cubic Splines MACM 316 2/15

DerivationUsing the definition, let’s determine equations relating th e coef-ficients and keep a count of the number of equations:

# eqns.Interpolation and continuity:

Si(xi) = yi for i = 0, 1, . . . , n − 1 n

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

(based on both of these, S(x) is continuous)

Derivative continuity:S′

i(xi+1) = S′i+1(xi+1) for i = 0, 1, . . . , n − 2 n − 1

S′′i (xi+1) = S′′

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

Total # of equations: 4n − 2

There are still 2 equations missing! (later)

November 1, 2012 c© Steven Rauch and John Stockie

Page 3: Cubic Spline 1

Cubic Splines MACM 316 3/15

Derivation (cont’d)We need expressions for the derivatives of Si:

Si(x) = ai + bi(x − xi) + ci(x − xi)2 + di(x − xi)

3

S′i(x) = bi + 2ci(x − xi) + 3di(x − xi)

2

S′′i (x) = 2ci + 6di(x − xi)

It is very helpful to introduce the hi = xi+1 − xi. Then the splineconditions can be written as follows:

• Si(xi) = yi for i = 0, 1, . . . , n − 1:

ai = yi

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

ai + hibi + h2ici + h3

idi = yi+1

• S′i(xi+1) = S′

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

bi + 2hici + 3h2idi − bi+1 = 0

• S′′i (xi+1) = S′′

i+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 sys -tem for the 4n unknowns

[a0, b0, c0, d0, a1, b1, c1, d1, . . . , an−1, bn−1, cn−1, dn−1]T

November 1, 2012 c© Steven Rauch and John Stockie

Page 4: Cubic Spline 1

Cubic Splines MACM 316 4/15

Alternate formulationThis linear system can be simplified considerably by defining

mi = S′′i (xi) = 2ci or ci = mi

2

and thinking of the mi as unknowns instead. Then:

• S′′i (xi+1) = S′′

i+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:

=⇒ yi + hibi + h2ici + h3

idi = yi+1

Substitute ci and di from above:

=⇒ bi =yi+1−yi

hi−

hi

2mi −

hi

6(mi+1 − mi)

• S′i(xi+1) = S′

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

=⇒ bi + 2hici + 3h2idi = bi+1

Substitute bi, ci and di from above and simplify:

himi + 2(hi + hi+1)mi+1 + hi+1mi+2 = 6[

yi+2−yi+1

hi+1−

yi+1−yi

hi

]

November 1, 2012 c© Steven Rauch and John Stockie

Page 5: Cubic Spline 1

Cubic Splines MACM 316 5/15

Notice: These are n − 1 linear equations for n + 1 unknowns

[m0, m1, m2, . . . , mn]T where mi = g′′

i (xi).

November 1, 2012 c© Steven Rauch and John Stockie

Page 6: Cubic Spline 1

Cubic Splines MACM 316 6/15

Endpoint Conditions: Natural Spline➤ Now, we’ll deal with the two missing equations . . .

➤ Note: Derivative matching conditions are applied only at in-terior points, which suggests applying some constraints onthe 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 · · · 0h0 2(h0 + h1) h1 0

0 h1 2(h1 + h2) h2 0

0 0 h2 2(h2 + h3) h3

...... . . . . . . . . .

0 hn−2 2(hn−2 + hn−1) hn−1

0 · · · 0 0 1

m0

m1

m2

m3

...mn

= 6

0y2−y1

h1

− y1−y0

h0

y3−y2

h2

− y2−y1

h1

y4−y3

h3

− y3−y2

h2...yn−yn−1

hn−1

−yn−1−yn−2

hn−2

0

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

Page 7: Cubic Spline 1

Cubic Splines MACM 316 7/15

Interpretation of a Natural Spline

• The word “spline” comes from the draftsman’s spline, whichis shown here constrained by 3 pegs.

• There is no force on either end to bend the spline beyond thelast pegs, which results in a flat shape (zero curvature).

• This is why the S′′ = 0 end conditions are called natural .

November 1, 2012 c© Steven Rauch and John Stockie

Page 8: Cubic Spline 1

Cubic Splines MACM 316 8/15

Endpoint Conditions: Clamped Spline2. Clamped Spline , sometimes called a “complete spline” (deriva-

tive is specified):

S′0(x0) = A =⇒ b0 = A

=⇒ A =y1 − y0

h0

−h0

2m0 −

h0

6(m1 − m0)

=⇒ 2h0m0 + h0m1 = 6

[

y1 − y0

h0

− A

]

and

S′n−1(xn) = B =⇒ bn−1 = B

=⇒ hn−1mn−1 + 2hn−1mn = 6

[

B −yn − yn−1

hn−1

]

These two equations are placed in the first/last rows

2h0 h0 0 · · · · · · 0

h0 2(h0 + h1) h1 0...

0 h1 2(h1 + h2) h2 0...

... 0 . . . . . . . . . 00 · · · 0 hn−2 2(hn−2 + hn−1) hn−1

0 · · · · · · 0 hn−1 2hn−1

and the first/last RHS entries are modified accordingly.

November 1, 2012 c© Steven Rauch and John Stockie

Page 9: Cubic Spline 1

Cubic Splines MACM 316 9/15

Endpoint Conditions: Not-A-Knot3. Not-A-Knot Spline (third derivative matching):

S′′′0 (x1) = S′′′

1 (x1) and S′′′n−2(xn−1) = S′′′

n−1(xn−1)

Using S′′′i (x) = 6di and di =

mi+1−mi

6hi, these conditions

become

h1(m1 − m0) = h0(m2 − m1)

and

hn−1(mn−1 − mn−2) = hn−2(mn − mn−1)

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 hn−2 2(hn−2 + hn−1) hn−1

0 · · · · · · −hn−1 hn−2 + hn−1 −hn−2

and the first/last RHS entries are zero.

Note: Matlab’s spline function implements the not-a-knotcondition (by default) as well as the clamped spline, butnot the natural spline. Why not? (see Homework #4).

November 1, 2012 c© Steven Rauch and John Stockie

Page 10: Cubic Spline 1

Cubic Splines MACM 316 10/15

Summary of the AlgorithmStarting with a set of n + 1 data points

(x0, y0), (x1, y1), (x2, y2), . . . , (xn, yn)

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 7for the natural spline. If clamped or not-a-knot conditions areused, then modify A and r appropriately.

3. Solve the (n + 1) × (n + 1) linear system Am = r for thesecond derivative values mi.

4. Calculate the spline coefficients for i = 0, 1, 2, . . . , n − 1 us-ing:

ai = yi

bi =yi+1 − yi

hi

−hi

2mi −

hi

6(mi+1 − mi)

ci =mi

2

di =mi+1 − mi

6hi

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

Page 11: Cubic Spline 1

Cubic Splines MACM 316 11/15

Natural Spline Examplexi 4.00 4.35 4.57 4.76 5.26 5.88yi 4.19 5.77 6.57 6.23 4.90 4.77hi 0.35 0.22 0.19 0.50 0.62

Solve Am = r when

A =

1 0 0 0 00.35 1.14 0.22 0 0 0

0 0.22 0.82 0.19 0 00 0 0.19 1.38 0.50 0

0 0 0 0.50 2.24 0.620 0 0 0 1

and r =

0−5.2674

−32.5548−5.2230

14.70180

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

Calculate the spline coefficients:xi yi = ai bi ci di Interval

S0(x) 4.00 4.19 4.3290 0 1.5125 [4.00, 4.35]

S1(x) 4.35 5.77 4.8848 1.5881 −33.0139 [4.35, 4.57]

S2(x) 4.57 6.57 0.7900 −20.2010 34.8675 [4.57, 4.76]

S3(x) 4.76 6.23 −3.1102 −0.3266 2.4541 [4.76, 5.26]

S4(x) 5.26 4.90 −1.5962 3.3546 −1.8035 [5.26, 5.88]

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 defining acubic spline.November 1, 2012 c© Steven Rauch and John Stockie

Page 12: Cubic Spline 1

Cubic Splines MACM 316 12/15

Splines vs. Interpolation

4 4.5 5 5.54

4.5

5

5.5

6

6.5

7

x

y

PolynomialNatural Spline

The Newton divided difference table for the data points isxi yi a1 a2 a3 a4 a5

4.00 4.194.5143

4.35 5.77 -1.54023.6364 -15.3862

4.57 6.57 -13.2337 22.6527-1.7895 13.1562 -15.7077

4.76 6.23 -1.2616 -6.8778-2.6600 2.6331

5.26 4.90 2.1878-0.2097

5.88 4.77

=⇒ P5(x) = 4.19 + 4.5143(x − 4) − 1.5402(x − 4)(x − 4.35) − . . .

November 1, 2012 c© Steven Rauch and John Stockie

Page 13: Cubic Spline 1

Cubic Splines MACM 316 13/15

Clamped Spline Examplexi 4.00 4.35 4.57 4.76 5.26 5.88yi 4.19 5.77 6.57 6.23 4.90 4.77hi 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 00.35 1.14 0.22 0 0 00 0.22 0.82 0.19 0 0

0 0 0.19 1.38 0.50 00 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.223014.7018

−10.7418

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

xi yi = ai bi ci di IntervalS0(x) 4.00 4.19 −1.0000 27.2832 −32.9375 [4.00, 4.35]

S1(x) 4.35 5.77 5.9937 −7.3011 −15.5191 [4.35, 4.57]

S2(x) 4.57 6.57 0.5279 −17.5437 28.1431 [4.57, 4.76]

S3(x) 4.76 6.23 −3.0908 −1.5021 4.7277 [4.76, 5.26]

S4(x) 5.26 4.90 −1.0472 5.5894 −6.8363 [5.26, 5.88]

Exercise: Verify that the spline satisfies the clamped end condi-tions:

S′0(4.00) = −1.0 and S′

4(5.88) = −2.0.

November 1, 2012 c© Steven Rauch and John Stockie

Page 14: Cubic Spline 1

Cubic Splines MACM 316 14/15

ComparisonBelow is a comparison of the splines with various endpoint co n-ditions:

4 4.5 5 5.54

4.5

5

5.5

6

6.5

7

x

y

NaturalClampedNot−A−Knot

November 1, 2012 c© Steven Rauch and John Stockie

Page 15: Cubic Spline 1

Cubic Splines MACM 316 15/15

Second ExampleConsider the following data representing the thrust of a mod elrocket versus time.

t T

0.00 0.000.05 1.000.10 5.000.15 15.000.20 33.500.30 33.000.40 16.500.50 16.00

t T

0.60 16.000.70 16.000.80 16.000.85 16.000.90 6.000.95 2.001.00 0.00

Below is a plot of the natural spline:

0 0.2 0.4 0.6 0.8 10

5

10

15

20

25

30

35

40

time

Thr

ust

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