scientific computing interpolation – divided differences efficiency and error analysis

24
Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Upload: lindsey-green

Post on 02-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Scientific Computing

Interpolation – Divided Differences Efficiency and Error Analysis

Page 2: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

How is Interpolation used?

• Here is a table of values for the temperature at a given depth for a lake. We want a good estimate of the temperature at a depth of 7.5 meters.

Temperature Depth

T (oC) z (m)19.1 019.1 -119 -2

18.8 -318.7 -418.3 -518.2 -617.6 -711.7 -89.9 -99.1 -10

Page 3: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Newton’s Nested Formula

• Recall: Newton’s method is iterative with Pk+1 (x) = Pk (x) + ck (x – x0)(x – x1) … (x – xk)

• Now, Pk (x) satisfies a similar formula:

Pk (x) = Pk-1 (x) + ck-1 (x – x0)(x – x1) … (x – xk-1)

• So, Pk+1 (x) = Pk-1 (x) + [ck-1 (x – x0)…(x – xk-1)] +

+ [ck (x – x0)…(x – xk)]

• Thus, Pn (x) = c0 + [c1 (x – x0)] + … + [cn (x – x0 )…(x – xn )]

Page 4: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Newton Nested Formula

• Pn (x) = c0 + [c1 (x – x0)] + … + [cn (x – x0 )…(x – xn )]

• We can write this in a compact form as

• Here we define the product for k =0 to be 1, so the first term is c0 , as it should be.

• We can rewrite this formula as (verify this!!) Pn (x) = c0 + (x – x0)[c1 + (x – x1)[c2 + (x – x2)[…]]]

pn (x) ck (x x j )0jk

k0

n

Page 5: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Newton Nested Formula

• Pn (x) = c0 + (x – x0)[c1 + (x – x1)[c2 + (x – x2)[…]]]

• This allows a better way of calculating Pn (t) for a value of t (or x):

• Then, vn = Pn (x) • This is called Newton’s Nested formula for Pn

100

1222

0111

0

)(

)(

)(

nn

nn

nn

n

vxxcv

vxxcv

vxxcv

cv

Page 6: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Efficiency

• Newton’s Nested formula Requires 2n additions, n multiplications O(n) • Lagrange Interpolation Requires at least n2 additions O(n2)

100

1222

0111

0

)(

)(

)(

nn

nn

nn

n

vxxcv

vxxcv

vxxcv

cv

P(x) x x jxk x jjk

k

yk

Page 7: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Newton Divided Differences

• vn = Pn (x)

• This is efficient, but is there a fast way to calculate the ck ‘s?

100

1222

0111

0

)(

)(

)(

nn

nn

nn

n

vxxcv

vxxcv

vxxcv

cv

Page 8: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Newton Divided Differences

• Pn (x) = c0 + c1 (x – x0) + … + cn (x – x0 )…(x – xn )]

1) Substituting x0 for x and y0 for y=Pn(x), we get

c0 = y0=f[x0]

2) Now, substituting x1 for x and y1 for y, we get

3) Substitute once more (x2 ,y2) to get

)],[)( 10001

01101101 xxfDy

xx

yycxxcyy

)())((

)()(

))(()(

02

12

02

01

01

12

02

1202

0201

0102

2

120220201

0102

xx

xx

xx

xx

yy

xx

yy

xxxx

xxxx

yyyy

c

xxxxcxxxx

yyyy

Page 9: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Newton Divided Differences

3) (cont’d) Simplifying, we get

So, Pn (x) = f[x] + f[x0, x1](x – x0) + f[x0, x1, x2](x – x0)(x -x1) + …

+ cn (x – x0 )…(x – xn )]

],,[)(

)()(

1

)(

)()(

)(

21002

02

01

02

01

01

12

12

02

01

01

12

01

01

01

12

02

02

12

01

01

01

12

02

02

12

0112

01

01

12

02

02

12

02

01

01

12

02

2

xxxfyDxx

DyDy

xx

xx

yy

xx

yy

xx

xx

yy

xx

xx

xx

yy

xx

yy

xx

xx

xx

xx

yy

xx

yy

xx

xx

xxxx

xx

yy

xx

yy

xx

xx

xx

xx

yy

xx

yy

c

Page 10: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Newton Divided Differences

• Definition (Divided Differences). For a given collection of data { (xi ,f(xi) ) } a kth order divided difference is a function of k + 1 (not necessarily distinct) data values written as f[xi , xi+1 , … , xi+k].

0th Order: f[xi ] = f(xi) = yi

kth Order:

Page 11: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Newton Divided Differences

• Theorem For Newton’s Interpolation formula Pn (x) = c0 + [c1 (x – x0)] + … + [cn (x – x0 )…(x – xn )]

the coefficients ck can be calculated as

ck = f[x0 , x1 , … , xk]

Page 12: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Newton Divided Differences

• Algorithm for Calculating Divided Differences: Calculate the differences in “pyramid” fashion, until you reach the last one.

Page 13: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Newton Divided Differences

• Example: Data (1,3), (1/2, -10), (3,2)

Page 14: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Newton Divided Differences

• Example: Data (1,3), (1/2, -10), (3,2)

Page 15: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Newton Divided Differences

• Example: Data (1,3), (1/2, -10), (3,2)

Top row of the pyramid contains coefficients, c0 = 3, c1 = 26 , c2 = -53/5Recall: Earlier work gave p(x) = 3+ 26 (x-1) – (53/5) (x-1)(x-1/2)

Page 16: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Matlab - Newton Divided Differences

• Notes: We want to store the values in the “pyramid”. These can be stored in a matrix. We let F(i,k) = kth divided difference

• Differences are indexed from 0 to n, so F will have indexes from 1 to n+1.

• Vectors: x[], y[] for initial data, y = f(x).

Page 17: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Matlab - Newton Divided Differences

• Notes: F(i,k) = kth divided difference

• Get F(i,k+1) = (F(i+1,k)-F(i,k))/(x(i+k)-x(i))

Page 18: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Matlab - Newton Divided Differences

function F = div_diff(x, y)% div_diff function computes all divided differences for % the data stored in x and y = f(x)n = length(x);m = length(y);if m~=n; error('x and y must be same size');else F = zeros(n, n); for i = 1:n % define 0th divide difference F(i,1) = y(i); end for k = 1:n-1 % Get kth divided difference for i = 1:n-k F(i,k+1) = (F(i+1,k)-F(i,k))/(x(i+k)-x(i)); end end

Page 19: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Matlab - Newton Divided Differences

• Example: x=[1 0.5 3]‘; y=[3 -10 2]'; div_diff(x,y) ans = 3.0000 26.0000 -10.6000 -10.0000 4.8000 0 2.0000 0 0

• Top row (F(1,*)) holds coefficients ck .

Page 20: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Matlab - Newton Nested Formula

• Need loop running “Backwards” • For evaluation, we replace x by a value u. 100

1222

0111

0

)(

)(

)(

nn

nn

nn

n

vxxcv

vxxcv

vxxcv

cv

Page 21: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Matlab - Newton Nested Formula

function v = newtonInterp(x,y,u)% v = newtonInterp(x,y,u) computes the Newton method % interpolation value p(u) for the given data x and yn = length(x);m = length(y);if m~=n; error('x and y must be same size');else F = div_diff(x,y); % Compute Newton polynomial using nested format v = F(1,n); % v0 = cn for i = n-1:-1:1 v = F(1,i) + (u-x(i))*v; % nested formula endend

Page 22: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Matlab - Newton Nested Formula

• Test:>> newtonInterp(x,y,1)ans = 3>> newtonInterp(x,y,0.5)ans = -10>> newtonInterp(x,y,3)ans = 2

Page 23: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Class Exercise

• Here is a table of values for the temperature at a given depth for a lake. Find a good estimate of the temperature at a depth of 7.5 meters. Use a cubic interpolating polynomial to help solve this problem.

Temperature Depth

T (oC) z (m)19.1 019.1 -119 -2

18.8 -318.7 -418.3 -518.2 -617.6 -711.7 -89.9 -99.1 -10

Page 24: Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

Error Analysis

1) If we want to interpolate a given f(x) at n+1 nodes, how should we pick nodes?

2) How accurate can we make a polynomial interpolant over an interval?