exercise on newton polynomials and mechanical ...lcaretto/me309/13-interpolation.pdfpolynomial...

12
Polynomial Interpolation March 12-17, 2014 ME 309 Numerical Analysis of Engineering Systems 1 Polynomial Interpolation Larry Caretto Mechanical Engineering 309 Numerical Analysis of Engineering Systems March 12-17, 2014 Outline Review numerical differentiation Unique n th order polynomial for n+1 data points, regardless of form Exercise on Newton Polynomials and Divided Difference Table Lagrange Interpolation Cubic splines APPENDIX: Thomas algorithm for solving tridiagonal matrix equations 2 Review Numerical Differentiation Finite difference expressions for derivatives derived from Taylor series Derivative = (Finite difference expression) + (Truncation Error) Use uniform grid with points x i = x 0 + ih f i = f(x i ) and h = step size = (x N x 0 )/N Have N + 1 points from x 0 to x N Can derive expressions for first, second, third, etc. derivatives 3 Use this Information only Review Numerical Differentiation II Classify finite-difference expressions as forward, backwards and central Forward difference: data for derivative at x = x i come from points at x x i Backwards difference: data for derivative at x = x i come from points at x x i Central difference: data for derivative at x = x i come from points at x i-k x x i+k Order of the error: truncation error proportional to h n written as O(h n ) 4 Review Derivative Expressions 5 h O h f f f i i i 1 ' h O h f f f i i i 1 ' 2 1 1 ' 2 h O h f f f i i i 2 1 2 ' 2 3 4 h O h f f f f i i i i 2 1 2 ' 2 3 4 h O h f f f f i i i i 2 2 1 1 ' ' 2 h O h f f f f i i i i What are (1) order of derivative, (2) order of error, and (3) direction (forward, central, or backward)? Central differences easiest to use for a given order of error (if data available) In-Class Exercise What is order of derivative, order of error and direction of expression below? 6 4 2 2 1 1 2 ' ' 12 16 30 16 h O h f f f f f f i i i i i i t(s) s(m) 0.7 0.644 0.8 0.717 0.9 0.783 1.0 0.841 1.1 0.891 1.2 0.932 1.3 0.964 1.4 0.985 1.5 0.997 Find acceleration at t = 1 s for position (s in meters) data 2 ' ' 1 . 0 12 717 . 0 ) 783 . 0 ( 16 ) 841 . 0 ( 30 ) 891 . 0 ( 16 932 . 0 i s s’’ = –0.792 m/s 2 Central difference expression for the second derivative, with fourth-order error

Upload: others

Post on 28-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Exercise on Newton Polynomials and Mechanical ...lcaretto/me309/13-interpolation.pdfPolynomial Interpolation March 12-17, 2014 ME 309 –Numerical Analysis of Engineering Systems 4

Polynomial Interpolation March 12-17, 2014

ME 309 – Numerical Analysis of Engineering Systems 1

Polynomial Interpolation

Larry Caretto

Mechanical Engineering 309

Numerical Analysis of

Engineering Systems

March 12-17, 2014

Outline• Review numerical differentiation

• Unique nth order polynomial for n+1

data points, regardless of form

• Exercise on Newton Polynomials and

Divided Difference Table

• Lagrange Interpolation

• Cubic splines

• APPENDIX: Thomas algorithm for

solving tridiagonal matrix equations

2

Review Numerical Differentiation• Finite difference expressions for

derivatives derived from Taylor series

• Derivative = (Finite difference

expression) + (Truncation Error)

• Use uniform grid with points xi = x0 + ih

– fi = f(xi) and h = step size = (xN – x0)/N

– Have N + 1 points from x0 to xN

• Can derive expressions for first, second,

third, etc. derivatives

3

Use this

Information only

Review Numerical Differentiation II• Classify finite-difference expressions as

forward, backwards and central

– Forward difference: data for derivative at x

= xi come from points at x xi

– Backwards difference: data for derivative at

x = xi come from points at x xi

– Central difference: data for derivative at x =

xi come from points at xi-k x xi+k

• Order of the error: truncation error

proportional to hn written as O(hn)

4

Review Derivative Expressions

5

hOh

fff iii

1' hO

h

fff iii

1'

211'

2hO

h

fff iii

212'

2

34hO

h

ffff iiii

212'

2

34hO

h

ffff iiii

2

2

11'' 2hO

h

ffff iiii

What are (1) order of

derivative, (2) order of

error, and (3) direction

(forward, central, or

backward)?

Central differences easiest to use for a

given order of error (if data available)

In-Class Exercise• What is order of derivative, order of

error and direction of expression below?

6

4

2

2112''

12

163016hO

h

ffffff iiiiii

t(s) s(m)

0.7 0.644

0.8 0.717

0.9 0.783

1.0 0.841

1.1 0.891

1.2 0.932

1.3 0.964

1.4 0.985

1.5 0.997

• Find acceleration at t = 1 s for

position (s in meters) data

2

''

1.012

717.0)783.0(16)841.0(30)891.0(16932.0 is

s’’ = –0.792 m/s2

– Central difference expression for the

second derivative, with fourth-order error

Page 2: Exercise on Newton Polynomials and Mechanical ...lcaretto/me309/13-interpolation.pdfPolynomial Interpolation March 12-17, 2014 ME 309 –Numerical Analysis of Engineering Systems 4

Polynomial Interpolation March 12-17, 2014

ME 309 – Numerical Analysis of Engineering Systems 2

Sum of Coefficients• Why is the sum of the coefficients in

each derivative expression is zero?

7

hOh

fff iii

1'

hOh

fff iii

1'

211'

2hO

h

fff iii

212'

2

34hO

h

ffff iiii

212'

2

34hO

h

ffff iiii

2

2

11'' 2hO

h

ffff iiii

4

2

2112''

12

163016hO

h

ffffff iiiiii

• If all fi are the same any derivative is zero

8

Figure 2-1. Effect of Step Size on Error

1.E-11

1.E-10

1.E-09

1.E-08

1.E-07

1.E-06

1.E-05

1.E-04

1.E-03

1.E-02

1.E-01

1.E+00

1.E+01

1.E-17 1.E-15 1.E-13 1.E-11 1.E-09 1.E-07 1.E-05 1.E-03 1.E-01

Step Size

Err

or

Truncation error: linear

d log(error) / d log (h)

for large step sizes (slope

= order of error)

Roundoff error at

smaller step sizes

Numerical derivative zero

at smallest step sizes

Review Interpolation

9

xi yi

0 0

10 10

20 30

30 60

40 100

50 150

• Example problem: What is

the value of y when x = 34?

• Can use different numbers of

x-y data pairs from 2 to 6

• For polynomial p(x), the basic

rule is that p(xi) = yi

– E.g. p(20) = 30

– What is p(40)? p(40) = 100

Uniqueness of Polynomial

10

• With n+1 data points we can fit a polynomial of order n

• This polynomial is unique

• It can take different forms, second order polynomials as an example

– y = a + bx + cx2 = (cx + b)x + a

– Newton: y = a0 + a1(x – x0) + a2(x – x0)(x – x1)

– Lagrange:

1323

213

3212

312

3121

321

xxxx

xxxxy

xxxx

xxxxy

xxxx

xxxxyxp

Example

11

• y = 3 + 2x + x2 has values y = 3 at x = 0, y = 6 at x = 1 and y = 11 at x = 2

• y = 3 + 3(x – 0) + 1(x – 0)(x – 1) has same values as does polynomial below

0212

1011

2101

206

2010

213

xxxxxxy

• Regardless of the form we use there is only one (unique) nth order polynomial between n + 1 data points

Review Newton Polynomials

12

• p0(x) = a0

• p1(x) = a0 + a1(x – x0)

• p2(x) = a0 + a1(x – x0) + a2(x – x0)(x – x1)

• p3(x) = a0 + a1(x – x0) + a2(x – x0)(x – x1) + a3(x – x0)(x – x1)(x – x2)

• p4(x) = a0 + a1(x – x0) + a2(x – x0)(x – x1) + a3(x – x0)(x – x1)(x – x2) + a4(x – x0)(x – x1)(x – x2)(x – x3)(x – x4)

• pn(x) = pn-1(x) + an k=0n−1 x − xk

Page 3: Exercise on Newton Polynomials and Mechanical ...lcaretto/me309/13-interpolation.pdfPolynomial Interpolation March 12-17, 2014 ME 309 –Numerical Analysis of Engineering Systems 4

Polynomial Interpolation March 12-17, 2014

ME 309 – Numerical Analysis of Engineering Systems 3

Newton Polynomial VBA Code

13

Function newtonPoly(a() As Double, x() As _Double, xFit As Double, n As Long) As Double

'Evaluates Newton polynomial of order nDim product As Double 'Ongoing productDim sum As Double 'Ongoing sumDim k As Long 'Loop counter

sum = a(0)product = 1For k = 1 To nproduct = product * (xFit - x(k-1))sum = sum + a(k) * product

Next knewtonPoly = sum

End Function

Review Divided Difference Table0 y0=10 ←a0

←a1

10 y1=20 ←a2

←a3

25 y2=150

↑a

50 y3=320

90 y4=790

01

010

xx

yyF

12

121

xx

yyF

23

232

xx

yyF

02

010

xx

FFS

13

121

xx

FFS

03

010

xx

SST

34

343

xx

yyF

24

232

xx

FFS

14

121

xx

SST

04

010

xx

TTR

• Start with x and

y data in first two

columns

• Take differences

of previous

columns divided

by delta-x terms

• Shaded cells

show

coefficients in

Newton

polynomials

15

Beware of Extrapolation!

16

Sample Divided Difference Table0 y0=10 ←a0

←a1

10 y1=20 ←a2

←a3

25 y2=150

↑a4

50 y3=320

90 y4=790

01

010

xx

yyF

12

121

xx

yyF

23

232

xx

yyF

02

010

xx

FFS

13

121

xx

FFS

03

010

xx

SST

34

343

xx

yyF

24

232

xx

FFS

14

121

xx

SST

04

010

xx

TTR

Divided difference

table gives polynomial

coefficients (next

page)

Follow same ak pattern

for other starting points

Newton Polynomials from xstart

17

• p0(x) = a0 and p1(x) = a0 + a1(x – xstart)

• p2(x) = a0 + a1(x – xstart) + a2(x – xstart) (x – xstart+1)

• p3(x) = a0 + a1(x – xstart) + a2(x – xstart) (x – xstart+1) + a3(x – xstart) (x – xstart+1)(x –xstart+2)

• a0 = ystart, a1 = Fstart = (ystart+1 – ystart) / (xstart+1 – xstart). a2 = Sstart = (Fstart+1 – …

1

0

1

0

n

m

m

k

kstartmn xxaxp

1

0

1

n

k

kstartnnn xxaxpxp

General Use of Table

18

• Start at any point in divided-difference table to get different interpolations

– Corresponds to using different set of data points and different order for fit

• Generally want to get interpolated value at a point that is in the middle of n+1 data points for nth-order polynomial

• What x points would you use for second order (quadratic) interpolation at x = 30 if you had data at x = [0 10 25 50 90]?

10 25 50

Page 4: Exercise on Newton Polynomials and Mechanical ...lcaretto/me309/13-interpolation.pdfPolynomial Interpolation March 12-17, 2014 ME 309 –Numerical Analysis of Engineering Systems 4

Polynomial Interpolation March 12-17, 2014

ME 309 – Numerical Analysis of Engineering Systems 4

19

Divided Difference Exercise0 y0=10

10 y1=20

25 y2=150

50 y3=320

90 y4=790

01

010

xx

yyF

12

121

xx

yyF

23

232

xx

yyF

02

010

xx

FFS

13

121

xx

FFS

03

010

xx

SST

34

343

xx

yyF

24

232

xx

FFS

14

121

xx

SST

04

010

xx

TTR

Compute divided-

difference table for

quadratic polynomial

through points at x =

10, 25, 50

Use divided-difference

table to generate the

quadratic polynomial 20

Divided Difference Table for Solution0 y0=10

10 y1=20 ←a0

←a1

25 y2=150 ←a2

50 y3=320

90 y4=790

01

010

xx

yyF

12

121

xx

yyF

23

232

xx

yyF

02

010

xx

FFS

13

121

xx

FFS

03

010

xx

SST

34

343

xx

yyF

24

232

xx

FFS

14

121

xx

SST

04

010

xx

TTR

3

26

15

130

1025

201501

F

5

34

25

170

2550

1503202

F

150

7

4015

28

1050

15

130

15

102

1050

3

26

5

34

1

S

Quadratic Polynomial

21

• Usual form for initial point at x = x0

– p2(x) = a0 + a1(x – x0) + a2(x – x0)(x – x1)

• For starting at x = x1

– p2(x) = a0 + a1(x – x1) + a2(x – x1)(x – x2)

– Using data from previous chart gives

– This gives p(xk) = yk for xk = 10, 25, and 50

– Interpolated value for x = 30 is 189

2510150

710

3

26202 xxxxp

22

-100

0

100

200

300

400

500

600

700

800

0 10 20 30 40 50 60 70 80 90

x

Quadratic Interpolation versus Data

QuadraticInterpolation

Data

Lagrange Interpolation• Alternative form to write interpolation

polynomial without need for detailed

coefficient computations

• Linear polynomial with two points

23

1

1

1

1

ii

ii

ii

ii

xx

xxy

xx

xxyxp

• Can you see that this is a linear

polynomial with p(xi-1) = yi-1 and p(xi) = yi

Lagrange Interpolation II• Quadratic polynomial with three points

24

111

11

11

11

111

11

iiii

iii

iiii

iii

iiii

iii

xxxx

xxxxy

xxxx

xxxxy

xxxx

xxxxyxp

• Can you see that this is a quadratic

polynomial with p(xi-1) = yi-1, p(xi) = yi,

and p(xi+1) = yi+1

Page 5: Exercise on Newton Polynomials and Mechanical ...lcaretto/me309/13-interpolation.pdfPolynomial Interpolation March 12-17, 2014 ME 309 –Numerical Analysis of Engineering Systems 4

Polynomial Interpolation March 12-17, 2014

ME 309 – Numerical Analysis of Engineering Systems 5

General Lagrange Polynomial• For n+1 data points from (x0, y0) to (xn,

yn) we have the following nth order

polynomial

25

n

in

ijj

ji

n

ijj

ji

xx

xxy

xp0

,1

,1

• At any data point, x = xk, p(xk) = yk

Splines• Definition: a piecewise polynomial

where each piece is a low order

polynomial

• Continuity: Depending on the order of

the spline one can maintain continuity of

the piecewise polynomial and one or

more of its derivatives

– Linear spline: continuity of polynomials

– Quadratic spline: continuity of first derivative

– Cubic spline: continuity of second derivative26

Cubic Splines• Have n+1 data points from (x0, y0) to (xn,

yn) sometimes called “knots”

• Between each pair of knots, (xk, yk) to (xk+1, yk+1), we pass a cubic polynomial

– pk(x) = ak + bkx + ckx2 + dkx

3 k = 0,…,n – 1

• This set of n polynomials has a total of 4n coefficients (a0 to an-1, b0 to bn-1, c0 to cn-1, and d0 to dn-1) that we have to determine

– We need 4n equations to determine these coefficients

27

Knots and Polynomials• Diagram of n+1 knots and n polynomials

p0 p1 p2 p3 pn-3 pn-2 pn-1

●----●------●---------●---~ ~----●-------●----●

x0 x1 x2 x3 xn-2 xn-1 xn

• Each point (knot) has known xk and yk

– At end points p0(x0) = y0 and pn-1(xn) = yn

– At each interior point pk(xk) = pk+1(xk) = yk

– Have continuity of first and second derivatives

p’k(xk) = p’k+1(xk) and p”k(xk) = p”k+1(xk) at knots

28

Conditions at Knots• Piecewise polynomial must be contin-

uous at each internal knot from x1 to xn-1

and must match the y value at the knot

– This means pk(xk) = pk+1(xk) = yk

29

3

1

2

1111

32

kkkkkkkkkk

kkkkkkkkkk

xdxcxbaxpy

xdxcxbaxpy

• This applies from k = 1 to k = n – 1

giving us 2n – 2 equations

Conditions at Knots II• At the ends of the interval, the

polynomial values must match the data

– This gives two more equations for a total of

2n – 2 + 2 = 2n

30

32

3

01

2

01011010

nnnnnnnnnn xdxcxbaxpy

xdxcxbaxpy

• At the interior knots we want to have

continuity of first and second derivatives

– We have no data to match, just continuity

Page 6: Exercise on Newton Polynomials and Mechanical ...lcaretto/me309/13-interpolation.pdfPolynomial Interpolation March 12-17, 2014 ME 309 –Numerical Analysis of Engineering Systems 4

Polynomial Interpolation March 12-17, 2014

ME 309 – Numerical Analysis of Engineering Systems 6

Continuity at Knots III• n – 1 first derivative continuity conditions

at each interior knot (i = 1, …, n – 1)

– dpk/dx = bk + 2ckx + 3dkx2 at any x

31

2

111

2 3232 kkkkkkkkkk xdxcbxdxcb

• n – 1 second derivative continuity cond-

itions at internal knots (i = 1, …, n – 1)

– d2pk/dx2 = 2ck + 6dkx at any x

kkkkkk xdcxdc 11 6262

Counting Equations• Have 2n + (n – 1) + (n – 1) = 4n – 2

equations to determine 4n coefficients

• Need some approximations to get an

additional two equations

• Need to develop a solution procedure to

solve for all coefficients necessary to

have n splines for our n – 1 data points

– See Rao, pp 394-395 for derivation of

equations to solve for spline equations in

terms of second derivatives32

Overview of Solution Process• Know that each polynomial has second

derivative given by d2pk/dx2 = 2ck + 6dkx

• Shows that second derivative varies

linearly with x between knots

• Use linear interpolation for second

derivative to derive a set of equations for

unknown second derivatives, pk’’ = sk

– Solve these equations for sk then use these

values to reformulate the cubic spline

equations33

Solving for the sk

• Define hk = xk+1 – xk as the variable step

size between x values (k = 0, … n – 1)

• Define Dyk = (yk+1 – yk)/hk

• Derived equations for sk

34

• This is known as a tridiagonal set of

equations because, when written as a

matrix only three diagonals have

nonzero terms

11111 62 DD kkkkkkkkk yyhhhh sss

Tridiagonal Equations for sk

35

DD

DD

DD

DD

DD

DD

56

45

34

23

12

01

7

6

5

4

3

2

1

0

665

5

5

544

4433

3322

2211

1100

6

6

6

6

6

6

2

0

00

00

00

00

00000

20000

2000

0200

0020

0002

yy

yy

yy

yy

yy

yy

hhh

h

h

hhh

hhhh

hhhh

hhhh

hhhh

s

ss

s

s

s

s

s

• Example of sk matrix equations (for n+1

= 8) before adding two other equations

• Have six equations in eight unknowns

– Assume some end conditions to get

equations for s0 = f(s0,s1) and s7 = f(s5,s6)

Assuming Extra Equations

36

• Use assumptions about s0 and sn

– Assumptions for s0 and sn can be different

1. Assume a value (typically s = 0, called

the “natural” boundary condition)

2. Assume constant (s0 = s1; sn-1 = sn)

3. Assume linear behavior resulting in

following equations at either end

21112

2

20110

1

0

1

1

nnnnn

n

n hhhh

hhhh

sss

sss

Page 7: Exercise on Newton Polynomials and Mechanical ...lcaretto/me309/13-interpolation.pdfPolynomial Interpolation March 12-17, 2014 ME 309 –Numerical Analysis of Engineering Systems 4

Polynomial Interpolation March 12-17, 2014

ME 309 – Numerical Analysis of Engineering Systems 7

Assuming Extra Equations II

37

4. Specify the value of p’(x) at the endpoint

which gives the following results

2

'1

2'

3

111

1

100

0

0

D

D

nnnassumedn

n

n

assumed

yxph

xpyh

sss

ss

• Assumptions gives equations for s0 and

sn that can be used to replace these

variables in tridiagonal matrix equations

• Reduces solution for sk to n – 1

equations in n – 1 unknowns

Assuming Extra Equations III3. Not-a-knot condition assumes continuity

of third derivative at second and second-

from-last data point: 𝑝1′′′ 𝑥1 =

𝑝2′′′ 𝑥1 𝑝𝑛

′′′ 𝑥𝑛−1 = 𝑝𝑛−1′′′ 𝑥𝑛−1

38

– Same as linear s vs. x behavior

– Gives same equations for s0 and sn

−𝜎0 + 1 +ℎ0ℎ1

𝜎1 −ℎ0ℎ1𝜎2 = 0

ℎ𝑛−1ℎ𝑛−2

𝜎𝑛−2 − 1+ℎ𝑛−1ℎ𝑛−2

𝜎𝑛−1 + 𝜎𝑛 = 0

Assuming Extra Equations IV

• Not-a-knot

Equations

39

−𝜎0 + 1+ℎ0ℎ1

𝜎1 −ℎ0ℎ1𝜎2 = 0

ℎ𝑛−1ℎ𝑛−2

𝜎𝑛−2 − 1+ℎ𝑛−1ℎ𝑛−2

𝜎𝑛−1 + 𝜎𝑛 = 0

• Linear s vs.

x Equations

21112

2

20110

1

0

1

1

nnnnn

n

n hhhh

hhhh

sss

sss

−ℎ1𝜎0 + ℎ1 + ℎ0 𝜎1 − ℎ0𝜎2 = 0

ℎ𝑛−1𝜎𝑛−2 − ℎ𝑛−2 + ℎ𝑛−1 𝜎𝑛−1 + ℎ𝑛−2𝜎𝑛 = 0

• Common

Form

Modifying Equations I

40

012111000 62 yyhhhh DD sss

– If s0 = s0,Assumed first equation becomes

Assumedhyyhhh ,000121110 62 sss DD

– Similarly with sn = sn,Assumed the last

equation becomes

Assumednnnnnnnnn hyyhhh ,12111222 62 sss DD

1. Assume a value for the second

derivative, sAssumed (often sAssumed = 0)– First equation with s0

Modifying Equations II2. Assume second derivative is constant

at endpoints so that s0 = s1 or sn = sn-1

– First equation before and after substituting

s0 = s1 into first equation

41

012111000 62 yyhhhh DD sss

– Similarly with sn = sn-1 last equation is

2111222 632 DD nnnnnnn yyhhh ss

0121110 623 yyhhh DD ss

Modifying Equations III3. Not-a-knot: second derivative is linear

near the endpoints

42

012111000 62 yyhhhh DD sss

– Last equation with same assumption is

21112

2

12

2

2

12 62

DD

nnnnn

n

nn

n

nn yyhh

h

h

h

hh ss

20110

1

0

1sss hhh

h

012

1

2

01110

1

0 62 yyh

hhhh

h

hDD

ss

– First equation before and after substituting

s0 = s1 into this equation

Page 8: Exercise on Newton Polynomials and Mechanical ...lcaretto/me309/13-interpolation.pdfPolynomial Interpolation March 12-17, 2014 ME 309 –Numerical Analysis of Engineering Systems 4

Polynomial Interpolation March 12-17, 2014

ME 309 – Numerical Analysis of Engineering Systems 8

Modifying Equations IV4. Specify first

derivative

43

012111000 62 yyhhhh DD sss

– First equation shown below before and

after substitution

2

3 1'

,00

0

0

ss D assumedpy

h

'

,00121110 39622

3assumedpyyhhh DD

ss

– Similar operation on final equation gives

'

,2111

222 3692

32 assumednnnn

nnnn pyy

hhh DD

ss

Tridiagonal Equations for sk

44

DD

DD

DD

DD

6

45

34

23

12

1

6

5

4

3

2

1

66

5544

4433

3322

2211

11

6

6

6

6

0000

2000

0200

0020

0002

0000

D

yy

yy

yy

yy

D

BA

hhhh

hhhh

hhhh

hhhh

CB

s

s

s

s

s

s

• Example of sk matrix equations after

adding two missing equations

• B1, C1, D1, A6 = An-1, B6 = Bn-1, and D6 = Dn-1

depend on end condition assumptions

B1, C1, D1, An-1, Bn-1, Dn-1 Part I

VariableAssume Second

Derivative at Ends

Assume Constant Se-

cond Derivative at Ends

B1 2(h0 + h1) 3h0 + 2h1

C1 h1 h1

D1

6[Dy1 – Dy0] –

h0s0,Assumed

6[Dy1 – Dy0]

An-1 hn-2 hn-2

Bn-1 2(hn-2 + hn-1) 2hn-2 + 3hn-1

Dn-1

6[Dyn-1 – Dyn-2] –

hn-1sn,Assumed

6[Dyn-1 – Dyn-2]

45

B1, C1, D1, An-2, Bn-2, Dn-2 Part II

VariableNot-a-knot: Linear

Second Derivatives

Assumed first

derivative at ends

B1 h0 + 2h1 3h0/2 + 2h1

C1 h1 – h0 h1

D1

6[Dy1 – Dy0]h1

/ (h1 + h0)

6Dy1 – 9Dy0 +

3p’0,Assumed

An-1 hn-2 – hn-1 hn-2

Bn-1 2hn-2 + hn-1 2hn-2 + 3hn-1/2

Dn-1

6[Dyn-1 – Dyn-2]hn-2

/ (hn-2 + hn-1)

9Dyn-1 – 6Dyn-2 –

3p’n,Assumed

46

Tridiagonal Matrix Equation

• Solve this with Thomas algorithm

– See appendix for derivation47

NNNN D

D

D

D

D

x

x

x

x

x

BA

BA

CBA

CBA

CB

4

3

2

1

4

3

2

1

44

333

222

11

0000

000

000

000

0000

Tridiagonal (Thomas) Algorithm• Loop over all rows from k = 1 to k = N-1;

for each k value compute the following

• Compute

• Loop over all rows from k = N – 1 to k =

1 in reverse order. For each row, k,

compute xk from the following equation

48

kkkkkkkkkk BDADDBCABB 111111

NNN BDx

k

kkkk

B

xCDx 1

Page 9: Exercise on Newton Polynomials and Mechanical ...lcaretto/me309/13-interpolation.pdfPolynomial Interpolation March 12-17, 2014 ME 309 –Numerical Analysis of Engineering Systems 4

Polynomial Interpolation March 12-17, 2014

ME 309 – Numerical Analysis of Engineering Systems 9

The Spline Polynomial• Once the sk values are known we have

the following equations for pk(x) to be

applied between xk and xk+1

49

k

kk

k

kk

kk

k

kk

kk

k

kkk

h

xxy

h

xxy

xxhh

xx

xxhh

xxxp

11

3

1

1

3

1

6

6

s

sCubic splines

used to program

computer driven

machines

because there is

no discontinuity in

force

Spline vs. High Order Newton

50

x y

805 0.71

825 0.763

845 0.907

865 1.336

885 2.169

905 1.598

925 0.916

945 0.672

965 0.615

985 0.606

• Two fits for ten data

points at left

– Ninth order Newton

polynomial

– Cubic spline with nine

different cubic formulae

• Results compared on

following plot

• Which do you think is

better fit?

51

-1

-0.5

0

0.5

1

1.5

2

2.5

800 850 900 950 1000

Comparing Newton Polynomial with Cubic Spline

NewtonPolynomialData

Cubic Spline

52

0.5

0.7

0.9

1.1

1.3

1.5

1.7

1.9

2.1

2.3

800 820 840 860 880 900 920 940 960 980 1000

Effect of End Conditions on Cubic Spline

Second derivative zero

Constant second derivative

Linear second derivative

First derivative zero

Data

APPENDIX: Thomas Algorithm• Application of Gaussian elimination to

matrix with only three diagonals

– Called a tridiagonal matrix

– Algorithm is sometimes called TDMA for

Tridiagonal Matrix Algorithm

– Actually applies Gaussian elimination, but

results are much simpler due to simple

structure of the tridiagonal matrix

– Basic equation for one row of matrix

53

iiiiiii DxCxBxA 11

Tridiagonal Matrix Equation

• First and last equation have only two

terms54

NNNN D

D

D

D

D

x

x

x

x

x

BA

BA

CBA

CBA

CB

4

3

2

1

4

3

2

1

44

333

222

11

0000

000

000

000

0000

Page 10: Exercise on Newton Polynomials and Mechanical ...lcaretto/me309/13-interpolation.pdfPolynomial Interpolation March 12-17, 2014 ME 309 –Numerical Analysis of Engineering Systems 4

Polynomial Interpolation March 12-17, 2014

ME 309 – Numerical Analysis of Engineering Systems 10

Data Storage• Tridiagonal matrix zeros are not stored

• Instead of usual A(i,j) matrix structure,

only the three diagonal terms are stored

• These are stored as one-dimensional

arrays: A(i), B(i), C(i)

• The right-hand-side column is stored as

the D(i) array

• Apply the usual Gaussian elimination to

get all zeroes below the main diagonal

55

Zeroes in Column One

• Changes required only for B2 and D2

when row 1 is the pivot row

– Factor A2/B1 gives A2 A2 – (A2/B1)B1 = 056

NN

NN D

D

D

DB

AD

D

x

x

x

x

x

BA

BA

CBA

CCB

AB

CB

4

3

1

1

22

1

4

3

2

1

44

333

21

1

22

11

0000

000

000

0000

0000

Already zero

Zeroes in Column Two

• Changes required only for B3 and D3

when row 2 is the pivot row

– B2 and D2 changed from original values in

previous step with row 1 as pivot row57

NN

NN D

D

DB

AD

D

D

x

x

x

x

x

BA

BA

CCB

AB

CB

CB

4

2

2

33

2

1

4

3

2

1

44

32

2

33

22

11

0000

000

0000

0000

0000

Already zero

General Formula

• Tridiagonal structure changes only Bk+1

and Dk+1 when row k is pivot row

– Formulas: 58

NN

NN D

D

DB

AD

D

D

x

x

x

x

x

BA

BA

CCB

AB

CB

CB

4

2

2

33

2

1

4

3

2

1

44

32

2

33

22

11

0000

000

0000

0000

0000

k

k

kkkk

k

kkk D

B

ADDC

B

ABB 1

111

11

Back Substitution• Matrix at end of elimination steps

59

NN

N

N

NN

D

D

D

D

D

x

x

x

x

x

B

CB

CB

CB

CB

4

3

2

1

1

3

2

1

11

33

22

11

00000

0000

0000

0000

0000

– Back-substitu-

tion formulas 1,2,2,11

NNkforB

xCDx

BDx

k

kkkk

NNN

Thomas Algorithm• Loop over all rows from k = 1 to k = N-1;

for each k value compute the following

• Compute

• Loop over all rows from k = N – 1 to k =

1 in reverse order. For each row, k,

compute xk from the following equation

60

kkkkkkkkkk BDADDBCABB 111111

NNN BDx

k

kkkk

B

xCDx 1

Page 11: Exercise on Newton Polynomials and Mechanical ...lcaretto/me309/13-interpolation.pdfPolynomial Interpolation March 12-17, 2014 ME 309 –Numerical Analysis of Engineering Systems 4

Polynomial Interpolation March 12-17, 2014

ME 309 – Numerical Analysis of Engineering Systems 11

Assignment Three Grades• Number of students: 20

• Maximum possible score: 40

• Mean score: 28.2

• Median score: 28.5

• Standard deviation: 7.67

• Distribution of scores:

13 13 19 22 22 26 2727 27 28 29 29 2931 34 34 37 38 38 40

61

Assignment 3 Comments• Functions to solve f(x) = 0 should have

the name of the functions to compute

f(x) as an input arguments to the

function not as part of the function

– Need f’ function name for Newton’s Method

– See solutions for correct way to do this

– You will have to do this in later assign-

ments on numerical integration and

numerical solution of ordinary differential

equations

62

Assignment 3 Comments II• Codes to solve f(x) = 0 should check

both error for convergence and excess

iterations to avoid infinite loop

– Return answer if required tolerance met

before iterations exceeded

– Return error message if tolerance not met

• Can always save workbook as macro-

enabled workbook to avoid losing

macros: File | Options | Save | “Save

Files in this Format” (first item in menu)63

Assignment 3 Comments III• Do not place unneeded worksheets in

your Excel workbooks

– Use File | Options | General to set initial

number of worksheets to one

• The fewer-less Rule– Important in formal writing, not in typical

conversation

– Use less for items that you cannot count• Example: I drink less coffee now.

– Use fewer for items that you can count• I drank fewer cups of coffee today

64

Assignment 3 Comments IV• Basic ideas

– fzero and Goal Seek are like finding a root

of f(x) = 0 on your calculator. They are

easy to implement once you know how

– Normally do not have to program solution

of f(x) = 0 unless part of a larger program

• Could find VBA code for doing so on line and

could use fzero as part of MATLAB code

• False position method is slow, but sure, once

initial guesses are made

• Newton’s method fast, but may not converge to

correct root, and requires derivative65

Assignment 3 Comments IV• Modify or eliminate dummy text in

MATLAB functions

– Original dummy text

– Possible modification

• Set error in Goal Seek in Excel Options

• Name files <lastName>_<assmt no.>

66

%UNTITLED2 Summary of this function goes here% Detailed explanation goes here

%FALSEPOS False Position solution of f(x) = 0% This function solves f(x) = 0 using the% false position method; inputs are …

Page 12: Exercise on Newton Polynomials and Mechanical ...lcaretto/me309/13-interpolation.pdfPolynomial Interpolation March 12-17, 2014 ME 309 –Numerical Analysis of Engineering Systems 4

Polynomial Interpolation March 12-17, 2014

ME 309 – Numerical Analysis of Engineering Systems 12

Interpolation Exercise

x y1 1.2212 1.4923 1.8224 2.2265 2.7186 3.3207 4.0558 4.9539 6.050

10 7.389

67

• For the data table at the left,

find an estimate of the value

of y at x = 6.6 using a cubic

polynomial

• You may use notes, ask

questions, consult with fellow

students, etc.

• You should be prepared to

solve this problem on a quiz

given the form of the Newton

polynomial

Exercise Solution

68

5 2.7180.6018

6 3.320 0.066620.7351 -0.004917

7 4.055 0.081370.8978

8 4.953

← a0

← a1

← a2

a3

• Divided

difference

table for cubic

polynomial

about x = 6.6

• p3(x) = a0 + a1(x – xstart) + a2(x – xstart) (x – xstart+1) + a3(x – xstart) (x – xstart+1)(x –xstart+2) = 2.718 + 0.6018(x – 5) + 0.06662(x – 5)(x – 6) – 0.004917(x –5)(x – 6)(x – 7) = 3.747 for x = 6.6