43
Lecture 2: Interpolation J.K. [email protected] WI3097TU Delft Institute of Applied Mathematics Delft University of Technology 10 September 2012 () Interpolation // J.K. Ryan 10 September 2012 1 / 43

Lecture2 Interpolaticsdson

Embed Size (px)

DESCRIPTION

dskdjskj

Citation preview

Page 1: Lecture2 Interpolaticsdson

Lecture 2:Interpolation

J.K. [email protected]

WI3097TUDelft Institute of Applied Mathematics

Delft University of Technology

10 September 2012

() Interpolation // J.K. Ryan 10 September 2012 1 / 43

Page 2: Lecture2 Interpolaticsdson

Outline

1 Review

2 Lagrange InterpolationDefinitionForming the approximationApproximation propertiesApproximation error

3 Spline Interpolation

() Interpolation // J.K. Ryan 10 September 2012 2 / 43

Page 3: Lecture2 Interpolaticsdson

Last lectureImportant Concepts

Important tools: Taylor’s theorem, Triangle inequalityHow numbers are storedErrors

I Roundoff error: = fl(x)− xI Absolute error: |e| = |uexact − uapproximate |I Relative error:

absolute error|uexact |

=|uexact − uapproximate |

|uexact |

Landau’s O symbol

() Interpolation // J.K. Ryan 10 September 2012 3 / 43

Page 4: Lecture2 Interpolaticsdson

Beginning of NumericsOverall Goal: to solve

ddx u(x) = f (u(x))

.Approximate a function (today).Approximate the derivative (next time).Approximate an integral (later).

Today’s Goal: Given a finite set of data points that represent afunction,y = f (x),

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

we want to form an approximation to y = f (x).

() Interpolation // J.K. Ryan 10 September 2012 4 / 43

Page 5: Lecture2 Interpolaticsdson

Beginning of Numerics

To approximate a function, one method is throughinterpolation.

Lagrange interpolation.Cubic spline interpolation.· · ·

() Interpolation // J.K. Ryan 10 September 2012 5 / 43

Page 6: Lecture2 Interpolaticsdson

Interpolation/ExtrapolationGiven a set of data points,

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

where yn = f (xn).

Definition (Interpolation)For interpolation, we try to approximate a value in between thegiven data points. That is, an approximation, yh ≈ f (x), wherex0 ≤ x ≤ xn.

Definition (Extrapolation)For extrapolation, we try to approximate a value outside therange of the given data points. That is, an approximation,yh ≈ f (x), where either x < x0 or x > xn.

() Interpolation // J.K. Ryan 10 September 2012 6 / 43

Page 7: Lecture2 Interpolaticsdson

Interpolation

ExampleGiven a set of data points,(

2, 12

),

(52 ,

25

),

(4, 14

),

we want to find the value at

x = 3,⇒ interpolate

x = 5,⇒ extrapolate.

Simplest method: use piecewise constants.

() Interpolation // J.K. Ryan 10 September 2012 7 / 43

Page 8: Lecture2 Interpolaticsdson

Linear interpolation

Linear interpolation is astraight line between 2 points:

(x0, y0), (x1, y1) wherey0 = f (x0) and y1 = f (x1).

Left: We use the endpointsfor the approximation.

() Interpolation // J.K. Ryan 10 September 2012 8 / 43

Page 9: Lecture2 Interpolaticsdson

Linear interpolation

The approximation to f (x) is given by the linear interpolationformula

p(x) =x − x1x0 − x1

y0 +x − x0x1 − x0

y1.

Note: Quite similar to the point-slope formula,y = mx + b, where m =slope and b = y intercept.

() Interpolation // J.K. Ryan 10 September 2012 9 / 43

Page 10: Lecture2 Interpolaticsdson

Linear interpolation

We can right this in another way:

p(x) =x − x1x0 − x1

y0 +x − x0x1 − x0

y1

= L0,1(x)y0 + L1,1(x)y1

where

L0,1(x) =x − x1x0 − x1

and L1,1(x) =x − x0x1 − x0

() Interpolation // J.K. Ryan 10 September 2012 10 / 43

Page 11: Lecture2 Interpolaticsdson

Linear interpolation

Notice:Polynomial of degree 1 (with order 2)Constructed using 2 pointsCan relate to Linearization:

L(x) = f (x0) + f ′(x0)(x − x0)

At x0 : p(x0) = y0, L0,1(x0) = 1, L1,1(x1) = 0At x1 : p(x1) = y1, L0,1(x0) = 0, L1,1(x1) = 1

() Interpolation // J.K. Ryan 10 September 2012 11 / 43

Page 12: Lecture2 Interpolaticsdson

InterpolationGeneralization

Generalization to higher order:

Given n + 1 points:

(x0, f (x0)), (x1, f (x1)), . . . , (xn, f (xn))

Is it possible to construct a polynomial of degree n thatinterpolates f (x) at those points?

() Interpolation // J.K. Ryan 10 September 2012 12 / 43

Page 13: Lecture2 Interpolaticsdson

InterpolationGeneralization

Definition (Lagrange interpolating polynomial)Given n + 1 data points,

(x0, f (x0)), (x1, f (x1)), . . . , (xn, f (xn)),

we can construct a polynomial of degree n at those points,

p(x) =n∑

k=0f (xk)Lk,n(x),

where p(x) is the Lagrange interpolating polynomial andLk,n(x) are the Lagrange coefficients which are given by

Lk,n(x) =(x − x0)(x − x1) · · · (x − xk−1)(x − xk+1) · · · (x − xn)

(xk − x0)(xk − x1) · · · (xk − xk−1)(xk − xk+1) · · · (xk − xn).

() Interpolation // J.K. Ryan 10 September 2012 13 / 43

Page 14: Lecture2 Interpolaticsdson

InterpolationTheorem (Lagrange interpolation)If points x0, x1, . . . , xn are n + 1 distinct points and f (x) isa function whose values are given at these points, then thereexists a unique polynomial, p(x), of degree at most n such that

f (xk) = p(xk), k = 0, 1, . . . , n.

The polynomial is given by

p(x) =n∑

k=0f (xk)Lk,n(x),

where

Lk,n(x) =n∏

jj 6=k

=0

x − xjxk − xj

, k = 0, 1, . . . , n.

() Interpolation // J.K. Ryan 10 September 2012 14 / 43

Page 15: Lecture2 Interpolaticsdson

Interpolation

Notice:p(x) is a polynomial of degree n.Constructed using n + 1 points.

Lk,n(xj) =

{0, j 6= k1, j = k

Polynomial is unique.p(xj) = f (xj) for j = 0, 1, . . . , n.

() Interpolation // J.K. Ryan 10 September 2012 15 / 43

Page 16: Lecture2 Interpolaticsdson

Interpolation

ExampleGiven x0 = 2, x1 = 2.5, x2 = 4, find an interpolatingpolynomial of degree 2 for f (x) = 1

x .

This means our given data is(2, 12

),

(52 ,

25

),

(4, 14

).

We have 3 = n + 1 points. We should be able to construct apolynomial of degree 2 = n.

() Interpolation // J.K. Ryan 10 September 2012 16 / 43

Page 17: Lecture2 Interpolaticsdson

InterpolationExample (continued)

In this case,

p(x) =n∑

k=0f (xk)Lk,n(x) =

2∑k=0

f (xk)Lk,2(x),

where

Lk,2(x) =2∏

jj 6=k

=0

x − xjxk − xj

, k = 0, 1, 2.

More specifically,

L0,2(x) =(x − x1)(x − x2)

(x0 − x1)(x0 − x2), L1,2(x) =

(x − x0)(x − x2)

(x1 − x0)(x1 − x2),

L2,2(x) =(x − x0)(x − x1)

(x2 − x0)(x2 − x1)

() Interpolation // J.K. Ryan 10 September 2012 17 / 43

Page 18: Lecture2 Interpolaticsdson

InterpolationExample (continued)

Plugging in our data:

p(x) =2∑

k=0f (xk)Lk,2(x)

= f (x0)L0,2(x) + f (x1)L1,2(x) + f (x2)L2,2(x)

=12L0,2(x) +

25L1,2(x) +

14L2,2(x)

where

L0,2(x) =

(x − 5

2

)(x − 4)(

2− 52

)(2− 4)

L1,2(x) =(x − 2)(x − 4)(52 − 2

) (52 − 4

) ,

L2,2(x) =(x − 2)

(x − 5

2

)(4− 2)

(4− 5

2

)() Interpolation // J.K. Ryan 10 September 2012 18 / 43

Page 19: Lecture2 Interpolaticsdson

InterpolationExample (continued)

Simplifying, we have that

p(x) = (0.05x − 0.425)x + 1.15

interpolates f (x) = 1x for

2 ≤ x ≤ 4.

() Interpolation // J.K. Ryan 10 September 2012 19 / 43

Page 20: Lecture2 Interpolaticsdson

InterpolationError

Once we have constructed the approximation, we should alwaysask:

How good is this approximation?

Absolute Error = |Exact − Approximate|= |f (x)− p(x)|

=

∣∣∣∣1x − ((0.05x − 0.425)x + 1.15)

∣∣∣∣

() Interpolation // J.K. Ryan 10 September 2012 20 / 43

Page 21: Lecture2 Interpolaticsdson

InterpolationError

At x = 3 (interpolation):

Absolute Error =

∣∣∣∣13 − ((0.05(3)− 0.425)3 + 1.15)

∣∣∣∣ = 0.00833̄

At x = 5 (extrapolation):

Absolute Error =

∣∣∣∣15 − ((0.05(5)− 0.425)5 + 1.15)

∣∣∣∣ = 0.075

() Interpolation // J.K. Ryan 10 September 2012 21 / 43

Page 22: Lecture2 Interpolaticsdson

InterpolationError

Interpolation, x = 3 :

|f (x)− p(x)| = 0.00833̄

Extrapolation, x = 5 :

|f (x)− p(x)| = 0.075

() Interpolation // J.K. Ryan 10 September 2012 22 / 43

Page 23: Lecture2 Interpolaticsdson

Interpolation

Theorem (Interpolation error)Given n + 1 distinct points in [a, b], and a function with n + 1continuous derivatives, f ∈ Cn+1(a, b), then for each x ∈ [a, b]there exists a ξ ∈ (a, b) with

|f (x)− p(x)| =

∣∣∣∣(x − x0)(x − x1) · · · (x − xn)1

(n + 1)!f (n+1)(ξ(x))

∣∣∣∣ ,where p(x) is the Lagrange interpolating polynomial of degreen.

() Interpolation // J.K. Ryan 10 September 2012 23 / 43

Page 24: Lecture2 Interpolaticsdson

InterpolationError

From the previous example: f (x) = 1x ⇒ f (3) = − 6

x4

The error formula would then be:

|f (x)− p(x)| =

∣∣∣∣(x − x0)(x − x1)(x − x2)13!f (3)(ξ)

∣∣∣∣=

∣∣∣∣(x − 2)

(x − 5

2

)(x − 4)

13!

(− 6ξ4

)∣∣∣∣≤ 1

3!maxξ∈[2,4]

∣∣∣∣ 6ξ4

∣∣∣∣ ∣∣∣∣(x − 2)

(x − 5

2

)(x − 4)

∣∣∣∣

() Interpolation // J.K. Ryan 10 September 2012 24 / 43

Page 25: Lecture2 Interpolaticsdson

Interpolation

Sometimes, it is better to use a lower order polynomialapproximation, but use smaller intervals.

ExampleLet f (x) = ex and x ∈ [0, 1]. Suppose we want to use apiecewise linear interpolation using points x0, x1, . . . , xn.

Find a piecewise linear interpolant for f (x).What should the spacing be between data to keep the errorless than 10−6?

() Interpolation // J.K. Ryan 10 September 2012 25 / 43

Page 26: Lecture2 Interpolaticsdson

InterpolationExample

Define xj = 0 + jh, j = 0, . . . , n. h then represents the spacingbetween the data,

h = xj+1 − xj .

For linear interpolation, the Lagrange interpolant forx ∈ [xj , xj+1] is given by

p(x) = f (xj)Lj,1(x) + f (xj+1)Lj+1,1(x), x ∈ (xj , xj+1)

= f (xj)x − xj

xj − xj+1+ f (xj+1)

x − xj+1xj+1 − xj

() Interpolation // J.K. Ryan 10 September 2012 26 / 43

Page 27: Lecture2 Interpolaticsdson

InterpolationExample

This gives

p(x) = exj x − xjxj − xj+1

+ exj+1x − xj+1xj+1 − xj

But h = xj+1 − xj

⇒ p(x) =1h ((xj+1 − x)exj + (x − xj)exj+1)

() Interpolation // J.K. Ryan 10 September 2012 27 / 43

Page 28: Lecture2 Interpolaticsdson

InterpolationExample

We want|f (x)− p(x)| ≤ 10−6

For which h does this hold?

Using our interpolation error theorem

|f (x)− p(x)| ≤∣∣∣∣(x − xj)(x − xj+1)

12!f (2)(ξ)

∣∣∣∣≤ 1

2!| f (2)(ξ)︸ ︷︷ ︸

d2dx2 ex=ex

||(x − xj)(x − xj+1)|

≤ 12 maxξ∈(0,1)

eξ | (x − jh)︸ ︷︷ ︸h2

(x − (j + 1)h)︸ ︷︷ ︸h2

| ≤ e2h2

4

() Interpolation // J.K. Ryan 10 September 2012 28 / 43

Page 29: Lecture2 Interpolaticsdson

InterpolationExample

We want|f (x)− p(x)| ≤ 10−6

where

|f (x)− p(x)| ≤ e8h

2 ≤ 10−6

⇒ h2 ≤ 8e−110−6

⇒ h ≤(8e−110−6

)1/2

() Interpolation // J.K. Ryan 10 September 2012 29 / 43

Page 30: Lecture2 Interpolaticsdson

Lagrange InterpolationSummary

One method of constructing an approximation to a function, f (x) is1 Take n + 1 data points: x0, x1, . . . , xn.2 Define the Lagrange coefficients:

Lk,n(x) =n∏

jj 6=k

=0

x − xjxk − xj

, k = 0, 1, . . . , n.

3 Then the approximating polynomial is given by

p(x) =n∑

k=0f (xk)Lk,n(x)

4 The error is given by

|f (x)− p(x)| ≤ 1(n + 1)!

∣∣∣∣∣n∏

k=0(x − xk)

∣∣∣∣∣ maxξ∈(x0,xn)

∣∣∣f (n+1)(ξ)∣∣∣

() Interpolation // J.K. Ryan 10 September 2012 30 / 43

Page 31: Lecture2 Interpolaticsdson

Spline Interpolation

Another way to form a piecewise polynomial approximation isthrough Spline interpolation.

Break the intervals into subintervals

{(x0, y0), (x1, y1)}, {(x1, y1), (x2, y2)}, . . . {(xn−1, yn−1), (xn, yn)}

Use piecewise polynomial approximationUse differentiability at endpoints of submitervals

() Interpolation // J.K. Ryan 10 September 2012 31 / 43

Page 32: Lecture2 Interpolaticsdson

Spline InterpolationLinear interpolation

We could use piecewise linear polynomials:

DefinitionGiven a function f on [a, b] and a set of nodesa = x0 < x1 < · · · < xn = b, a linear spline interpolant is afunction s(x) ∈ C[a, b] such that

si (x) is a linear polynomial on the interval[xi , xi+1], i = 0, . . . , n − 1si (xi ) = f (xi ), i = 0, . . . , n

But this gives something like piecewise Lagrange interpolation.

() Interpolation // J.K. Ryan 10 September 2012 32 / 43

Page 33: Lecture2 Interpolaticsdson

Spline InterpolationCubic interpolation

What about a higher order piecewise interpolant?

DefinitionGiven a function f on [a, b] and a set of nodesa = x0 < x1 < · · · < xn = b, a cubic spline interpolant is afunction s(x) ∈ C2[a, b] such that

1 si (x) is a polynomial of degree 3 on each subinterval[xi , xi+1], i = 0, . . . , n − 1

2 si (xi ) = f (xi ), i = 0, . . . , n − 1 and si (xi+1) = f (xi+1)

3 dk

dxk si (xi+1) = dk

dxk si+1(xi+1),i = 0, . . . , n − 2, k = 0, 1, 2

4 (One possible condition) s”0 (x0) = s”

n−1(xn) = 0

() Interpolation // J.K. Ryan 10 September 2012 33 / 43

Page 34: Lecture2 Interpolaticsdson

Cubic Spline Interpolation

This means that given equally spaced data,

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

we can define

si (x) = ai (x − xi )3 + bi (x − xi )

2 + ci (x − xi ) + di

on [xi , xi+1], where h = xi+1 − xi .

We must solve for the coefficients, ai , bi , ci , di .

() Interpolation // J.K. Ryan 10 September 2012 34 / 43

Page 35: Lecture2 Interpolaticsdson

Cubic Spline InterpolationStep 1

Plug in x = xi into

si (x) = ai (x−xi )3+bi (x−xi )

2+ci (x−xi )+di , i = 0, . . . , n−1

si (xi ) = f (xi )

= ai (xi − xi )3 + bi (xi − xi )

2 + ci (xi − xi ) + di ,

= di

⇒ di = f (xi ), i = 0, . . . , n − 1

() Interpolation // J.K. Ryan 10 September 2012 35 / 43

Page 36: Lecture2 Interpolaticsdson

Cubic Spline InterpolationStep 2

Use s”i (xi+1) = s”

i+1(xi+1) and s”0 (x0) = s”

n−1(xn) = 0 where

s”i (x) = 6ai (x − xi ) + 2bi

This gives

s”i (xi+1) = 6ai (xi+1 − xi ) + 2bi = 6aih + 2bi

s”i+1(xi+1) = 2bi+1 (1)

⇒ ai =13h (bi+1 − bi ), i = 0, . . . , n − 2

and

⇒ b0 = 0, an−1 = − 13hbn−1 i = 0, . . . , n − 2

() Interpolation // J.K. Ryan 10 September 2012 36 / 43

Page 37: Lecture2 Interpolaticsdson

Cubic Spline InterpolationStep 3

Use si (xi+1) = si+1(xi+1) and sn−1(xn) = yn Which gives,

si (xi+1) = ai (xi+1 − xi )3 + bi (xi+1 − xi )

2 + ci (xi+1 − xi ) + yi

= aih3 + bih2 + xih + yi

si+1(xi+1) = yi+1

So that we have

⇒ ci =1h (yi+1 − yi )−

h3 (bi+1 + 2bi ), i = 0, . . . , n − 2

and

⇒ cn−1 =1h (yn − yn−1)− 2h

3 bn,

() Interpolation // J.K. Ryan 10 September 2012 37 / 43

Page 38: Lecture2 Interpolaticsdson

Cubic Spline InterpolationStep 4

From s ′i (xi+1) = s ′i+1(xi+1) we obtain a linear system:

s ′i (xi+1) =23hbi+1 +

13hbi +

1h (yi+1 − yi )

s ′i (xi+1) = −13(bi+2 + 2bi+1) + 1h(bi+1 + 2bi+1)

Which gives

⇒ bi+2 + 4bi+1 + bi =3h2 (yi+2 − 2yi+1 + yi ), i = 0, . . . , n − 3

() Interpolation // J.K. Ryan 10 September 2012 38 / 43

Page 39: Lecture2 Interpolaticsdson

Cubic Spline InterpolationAlgorithm

Given data,(x0, y0), (x1, y1), . . . , (xn, yn)

we define

si (x) = ai (x − xi )3 + bi (x − xi )

2 + ci (x − xi ) + di

on [xi , xi+1], where hi = xi+1 − xi . To find the coefficients:1 Solve linear system Mb = g for b = [b0, b1, . . . , bn−1]T

2 Calculate ci .

3 Calculate ai .

4 Form si (x) = ai (x − xi )3 + bi (x − xi )

2 + ci (x − xi ) + di forx ∈ [xi , xi+1].

() Interpolation // J.K. Ryan 10 September 2012 39 / 43

Page 40: Lecture2 Interpolaticsdson

Cubic Spline Interpolation

ExampleGiven data

(1, 1),

(2, 12

),

(3, 13

),

(4, 14

),

The cubic spline interpolant that approximation f (x) = 1x using

this data is given by

S(x) =

112(x3 − 3x2 − 4x + 18), 1 ≤ x ≤ 2,112(−x3 + 9x2 − 28x + 34), 2 < x ≤ 3,112(−x + 7), 3 < x ≤ 4.

() Interpolation // J.K. Ryan 10 September 2012 40 / 43

Page 41: Lecture2 Interpolaticsdson

Cubic Spline Interpolation

How did we find this Spline interpolant?Piecewise polynomial intervals: [x0, x1], [x1, x2], [x2, x3].This means we have 3 spline interpolants s0(x), s1(x),and s2(x).We know di = f (xi ) = 1

xi, i = 0, . . . , n − 1.

Boundaries: s”0 (x0) = s”

2 (x3) = 0→ s”0 (1) = s”

2 (4) = 0

⇒ b0 = 0 a2 = −13b2

Solve the system for bi , i = 1, 2 :[4 11 4

] [b1b2

]=

[114

]⇒[b1b2

]=

[140

]

() Interpolation // J.K. Ryan 10 September 2012 41 / 43

Page 42: Lecture2 Interpolaticsdson

Cubic Spline InterpolationUsing ci = 1

hi(yi+1 − yi )− hi

3 (bi+1 + 2bi ) :

c0 = − 712 , c1 = −1

3 , c2 = − 112

Using ai = 12hi

(bi+1 − bi ) :

a0 =112 , a1 = − 1

12 , a2 = 0

Gives

s(x) =

s0(x) = 1

12 (x − 1)3 − 712 (x − 1) + 1, ≤ x ≤ 2,

s1(x) = − 112 (x − 2)3 + 1

4 (x − 2)2 − 13 (x − 2) + 1

2 , 2 < x ≤ 3,s2(x) = − 1

12 (x − 3) + 13 , 3 < x ≤ 4.

() Interpolation // J.K. Ryan 10 September 2012 42 / 43

Page 43: Lecture2 Interpolaticsdson

Material addressed

1 Review2 Lagrange Interpolation

DefinitionForming theapproximationApproximation propertiesApproximation error

3 Spline Interpolation

Material in book:Chapter 2, Sections 1-3, 5-6

HIGHLY Recommendedexercises:

All from Ch. 2.

() Interpolation // J.K. Ryan 10 September 2012 43 / 43