Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis.

Similar presentations


Presentation on theme: "Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis."— Presentation transcript:

1 Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis

2 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.

3 Newton’s Nested Formula Recall: Newton’s method is iterative with P k+1 (x) = P k (x) + c k (x – x 0 )(x – x 1 ) … (x – x k ) Now, P k (x) satisfies a similar formula: P k (x) = P k-1 (x) + c k-1 (x – x 0 )(x – x 1 ) … (x – x k-1 ) So, P k+1 (x) = P k-1 (x) + [c k-1 (x – x 0 )…(x – x k-1 )] + + [c k (x – x 0 )…(x – x k )] Thus, P n (x) = c 0 + [c 1 (x – x 0 )] + … + [c n (x – x 0 )…(x – x n )]

4 Newton Nested Formula P n (x) = c 0 + [c 1 (x – x 0 )] + … + [c n (x – x 0 )…(x – x n )] 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 c 0, as it should be. We can rewrite this formula as (verify this!!) P n (x) = c 0 + (x – x 0 )[c 1 + (x – x 1 )[c 2 + (x – x 2 )[…]]]

5 Newton Nested Formula P n (x) = c 0 + (x – x 0 )[c 1 + (x – x 1 )[c 2 + (x – x 2 )[…]]] This allows a better way of calculating P n (t) for a value of t (or x): Then, v n = P n (x) This is called Newton’s Nested formula for P n

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

7 Newton Divided Differences v n = P n (x) This is efficient, but is there a fast way to calculate the c k ‘s?

8 Newton Divided Differences P n (x) = c 0 + c 1 (x – x 0 ) + … + c n (x – x 0 )…(x – x n )] 1) Substituting x 0 for x and y 0 for y=P n (x), we get c 0 = y 0 =f[x 0 ] 2) Now, substituting x 1 for x and y 1 for y, we get 3) Substitute once more (x 2,y 2 ) to get

9 Newton Divided Differences 3) (cont’d) Simplifying, we get So, P n (x) = f[x] + f[x 0, x 1 ](x – x 0 ) + f[x 0, x 1, x 2 ](x – x 0 )(x -x 1 ) + … + c n (x – x 0 )…(x – x n )]

10 Newton Divided Differences Definition (Divided Differences). For a given collection of data { (x i,f(x i ) ) } a kth order divided difference is a function of k + 1 (not necessarily distinct) data values written as f[x i, x i+1, …, x i+k ]. 0 th Order: f[x i ] = f(x i ) = y i kth Order:

11 Newton Divided Differences Theorem For Newton’s Interpolation formula P n (x) = c 0 + [c 1 (x – x 0 )] + … + [c n (x – x 0 )…(x – x n )] the coefficients c k can be calculated as c k = f[x 0, x 1, …, x k ]

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

13 Newton Divided Differences Example: Data (1,3), (1/2, -10), (3,2)

14 Newton Divided Differences Example: Data (1,3), (1/2, -10), (3,2)

15 Newton Divided Differences Example: Data (1,3), (1/2, -10), (3,2) Top row of the pyramid contains coefficients, c 0 = 3, c 1 = 26, c 2 = -53/5 Recall: Earlier work gave p(x) = 3+ 26 (x-1) – (53/5) (x-1)(x-1/2)

16 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).

17 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))

18 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

19 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 c k.

20 Matlab - Newton Nested Formula Need loop running “Backwards” For evaluation, we replace x by a value u.

21 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

22 Matlab - Newton Nested Formula Test: >> newtonInterp(x,y,1) ans = 3 >> newtonInterp(x,y,0.5) ans = -10 >> newtonInterp(x,y,3) ans = 2

23 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.

24 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?


Download ppt "Scientific Computing Interpolation – Divided Differences Efficiency and Error Analysis."

Similar presentations


Ads by Google