23
Numerical Computation Lecture 11: Polynomial Interpolation United International College

Numerical Computation

  • Upload
    plato

  • View
    88

  • Download
    2

Embed Size (px)

DESCRIPTION

Numerical Computation. Lecture 11: Polynomial Interpolation United International College. Review. Polynomial Interpolation : Given data {(y k ,x k )}, find polynomial P(x) such that P(x k )= y k Lagrange Interpolation Newton Interpolation 1. P 0 (x) = y 0 - PowerPoint PPT Presentation

Citation preview

Page 1: Numerical Computation

Numerical Computation

Lecture 11: Polynomial Interpolation

United International College

Page 2: Numerical Computation

Review• Polynomial Interpolation: Given data {(yk ,xk )}, find

polynomial P(x) such that P(xk)= yk

– Lagrange Interpolation

– Newton Interpolation 1. P0(x) = y0

2. Pk+1 (x) = Pk (x) + ck (x – x0 )(x – x1 ) … (x – xk ) Set Pk+1 (xk+1) = yk+1 solve for ck (k=1,2,…,n) 3. P(x) = Pn (x)

Page 3: Numerical Computation

Today

• We will cover:– More on Polynomial Interpolation– Readings:

• Pav, section 5.1.3, 5.1.4• Moler, section 3.1

– Reading Quiz!!

Page 4: Numerical Computation

Interpolation

• Definition: Given a set of (x,y) data points, Interpolation is the process of finding a function (usually a polynomial) that passes through these data points.

Page 5: Numerical Computation

Newton’s Nested Formula

• Recall: Newton’s method is iterative with Pk+1 (x) = Pk (x) + ck+1 (x – x0)(x – x1) … (x – xk)• Now, Pk (x) satisfies a similar formula: Pk (x) = Pk-1 (x) + ck (x – x0)(x – x1) … (x – xk-1)• So, Pk+1 (x) = Pk-1 (x) + [ck (x – x0)…(x – xk-1)] + + [ck+1 (x – x0)…(x – xk)]• Thus, Pn (x) = c0 + [c1 (x – x0)] + … + [cn (x – x0 )…(x – xn-1 )]

Page 6: Numerical Computation

Newton Nested Formula

• Pn (x) = c0 + [c1 (x – x0)] + … + [cn (x – x0 )…(x – xn-1 )]• 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)[…]]]

Page 7: Numerical Computation

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 (t) • This is called Newton’s Nested formula for Pn

Page 8: Numerical Computation

Efficiency

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

Page 9: Numerical Computation

Newton Divided Differences

• vn = Pn (t) • This is efficient, but is there a fast way to calculate the ck ‘s?

• Let us suppose that yk = f(xk ) for some function f.

Page 10: Numerical Computation

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: Numerical Computation

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: Numerical Computation

Newton Divided Differences

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

Page 13: Numerical Computation

Newton Divided Differences

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

Page 14: Numerical Computation

Newton Divided Differences

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

Page 15: Numerical Computation

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: Numerical Computation

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: Numerical Computation

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: Numerical Computation

Matlab - Newton Divided Differencesfunction 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: Numerical Computation

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: Numerical Computation

Matlab - Newton Nested Formula

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

Page 21: Numerical Computation

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: Numerical Computation

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: Numerical Computation

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 DepthT (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