250 likes | 519 Views
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
E N D
Numerical Computation Lecture 11: Polynomial Interpolation United International College
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)
Today • We will cover: • More on Polynomial Interpolation • Readings: • Pav, section 5.1.3, 5.1.4 • Moler, section 3.1 • Reading Quiz!!
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.
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 )]
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)[…]]]
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
Efficiency Newton’s Nested formula Requires 2n additions, n multiplications O(n) Lagrange Interpolation Requires at least n2 additions O(n2)
Newton Divided Differences vn = Pn (t) This is efficient, but is there a fast way to calculate theck ‘s? Let us suppose that yk = f(xk ) for some function f.
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:
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]
Newton Divided Differences Algorithm for Calculating Divided Differences: Calculate the differences in “pyramid” fashion, until you reach the last one.
Newton Divided Differences Example: Data (1,3), (1/2, -10), (3,2)
Newton Divided Differences Example: Data (1,3), (1/2, -10), (3,2)
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/5 Recall: Earlier work gave p(x) = 3+ 26 (x-1) – (53/5) (x-1)(x-1/2)
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).
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))
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
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 .
Matlab - Newton Nested Formula Need loop running “Backwards” For evaluation, we replace t by a value u.
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 y n = 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 end end
Matlab - Newton Nested Formula Test: >> newtonInterp(x,y,1) ans = 3 >> newtonInterp(x,y,0.5) ans = -10 >> newtonInterp(x,y,3) ans = 2
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.