Computers in Civil Engineering 53:081 Spring 2003 Lecture #14 Interpolation Interpolation: Overview Objective: estimate intermediate values between precise data points using simple functions y Interpolation y Curve Fitting x Curve goes through data points single value Solutions – Newton Polynomials – Lagrange Polynomials – Spline Interpolation x Curve need not go through data points multiple values Example y f ( x) ? y ( xi ) ? High-precision data points xi x Quad Cities Dresden LaSalle Braidwood Quad-Cities Nuke Station Diffuser Curve Jain's TMC-1 9 8 DT 7 Poly. (DT) Poly. (DT) Delta T (deg F) 6 5 4 3 y = -5.594406E-18x4 - 1.100233E-13x3 + 3.470629E-08x2 - 1.251748E-03x + 1.691667E+01 R2 = 0.9999 2 1 0 0 5000 10000 15000 20000 Miss. R. Discharge (cfs) 25000 30000 35000 Examples of Simple Polynomials Fist-order (linear) Second-order (quadratic) Third-order (cubic) Newton’s Divided-Difference Interpolating Polynomials General comments Linear Interpolation Quadratic Interpolation General Form Linear Interpolation Formula By similar triangles: f ( x1 ) f1 ( x ) f ( x 0 ) f1 ( x ) Rearrange: x x0 f1 ( x ) f ( x 0 ) f ( x0 ) x0 The notation: x x1 f 1 ( x ) means f ( x1 ) f ( x 0 ) x1 x 0 f ( x1 ) f ( x 0 ) x1 x 0 ( x x0 ) f ( x0 ) m ( x x0 ) the first order interpolating polynomial Example Problem: Estimate ln(2) (the true value is 0.69) Solution: We know that: at x = 1 ln(x) =0 at x = e ln(x) =1 (e=2.718...) Thus , f1 ( x ) f ( x 0 ) f (1) 0 f ( x1 ) f ( x 0 ) x1 x 0 f ( e ) f (1) e 1 1 0 2 . 178 1 ( x x0 ) ( 2 1) ( 2 1) 0 . 58 Quadratic Interpolation General form: f 2 ( x ) a 0 a1 x a 2 x 2 Equivalent form: f 2 ( x ) b 0 b1 ( x x 0 ) b 2 ( x x 0 )( x x1 ) (f2(x) means second-order interpolating polynomial) To solve for b 0 , b1 , and b 2 ,three points are needed: ( x 0 , f ( x 0 )), ( x1 , f ( x1 ), ( x 2 , f ( x 2 )) Quadratic Interpolation f 2 ( x ) b 0 b1 ( x x 0 ) b 2 ( x x 0 )( x x1 ) (1) Set x x 0 in (1) to find b 0 f ( x 0 ) Substitute b 0 in (1) and evaluate at x x1 to find: b1 f ( x1 ) f ( x 0 ) x1 x 0 Substitute b 0 , b1 in (1) and evaluate at x x 2 to find: Note: this looks like a second derivative… f ( x 2 ) f ( x1 ) b2 x 2 x1 f ( x1 ) f ( x 0 ) x2 x0 x1 x 0 Example Problem Estimate ln(2) (the true value is 0.69) Solution We know that: at x = x0 = 1 ln(x) =0 at x = x1 = e ln(x) =1 (e=2.718...) at x = x2 = e2 ln(x) = 2 b1 b 0 f ( x 0 ) ln( 1) 0 f ( x 2 ) f ( x1 ) b2 x 2 x1 ln( e ) ln( 1) e 1 1 0 2 . 7183 1 f ( x1 ) f ( x 0 ) x2 x0 x1 x 0 0 . 05 f 2 ( 2 ) b 0 b1 ( x x 0 ) b 2 ( x x 0 )( x x1 ) 0 . 62 0 . 58 How to Generalize This? It would get pretty tedious to do this for third, fourth, fifth, sixth, etc order polynominal We need a plan: Newton’s Interpolating Polynomials General form of Newton’s Interpolating Polynomials f n ( x ) b 0 b1 ( x x 0 ) b n ( x x 0 )( x x1 ) ( x x n 1 ) To solve for b 0 , b1 , b n , n+1 points are needed: ( x 0 , f ( x 0 )), ( x1 , f ( x1 ), , ( x n , f ( x n )) Solution b f ( x ) 0 0 b1 f [ x1 , x 0 ] b 2 f [ x 2 , x1 , x 0 ] What does this [ ] notation mean? b n f [ x n , x n 1 , , x1 , x 0 ] Finite Divided Differences First finite divided difference: f [ xi , x j ] f ( xi ) f ( x j ) xi x j Second finite divided difference: f [ xi , x j , x k ] f [ xi , x j ] f [ x j , x k ] xi xk nth finite divided difference: f [ x n , x n 1 , , x1 , x 0 ] f [ x n , x n 1 , , x1 ] f [ x n 1 , x n 2 , , x 0 ] xn x0 f n ( x ) b 0 ( x x 0 ) f [ x1 , x 0 ] ( x x 0 )( x x1 ) f [ x 2 , x1 , x 0 ] ( x x 0 )( x x1 ) ( x x n 1 ) f [ x n , x n 1 , , x 0 ] Finite Divided Differences f n ( x ) b 0 ( x x 0 ) f [ x1 , x 0 ] ( x x 0 )( x x1 ) f [ x 2 , x1 , x 0 ] ( x x 0 )( x x1 ) ( x x n 1 ) f [ x n , x n 1 , , x 0 ] Finite divided difference table, case n = 3: i xi f ( xi ) first second 0 x0 f ( x0 ) f [ x1 , x 0 ] f [ x 2 , x1 , x 0 ] 1 x1 f ( x1 ) f [ x 2 , x1 ] f [ x 3 , x 2 , x1 ] 2 x2 f ( x2 ) f [ x3 , x2 ] 3 x3 f ( x3 ) third f [ x 3 , x 2 , x1 , x 0 ] b 0 , b1 , b 2 , b 3 Divided Differences Pseudo Code i xi f ( xi ) first second 0 x0 f ( x0 ) f [ x1 , x 0 ] f [ x 2 , x1 , x 0 ] 1 x1 f ( x1 ) f [ x 2 , x1 ] f [ x 3 , x 2 , x1 ] 2 x2 f ( x2 ) f [ x3 , x2 ] 3 x3 f ( x3 ) third f [ x 3 , x 2 , x1 , x 0 ] b 0 , b1 , b 2 , b 3 do i=0,n-1 fdd(i,1)=f(i) enddo do j=2,n do i=1,n-j+1 fdd(i,j)=(fdd(i+1,j-1)-fdd(i,j-1))/ & (x(i+j-1)-x(i)) enddo enddo Example – ln(2) again i xi f ( xi ) first second third 0 x0 f ( x0 ) f [ x1 , x 0 ] f [ x 2 , x1 , x 0 ] 1 x1 f ( x1 ) f [ x 2 , x1 ] f [ x 3 , x 2 , x1 ] 2 x2 f ( x2 ) f [ x3 , x2 ] 3 x3 f ( x3 ) f [ x 3 , x 2 , x1 , x 0 ] b 0 , b1 , b 2 , b 3 f [ x1 , x 0 ] ln( 4 ) ln( 1) 4 1 0 . 4621 i xi f ( xi ) first second third 0 1 0 0 . 4621 0 . 0518 0 . 0079 1 4 1 . 3863 0 . 2027 0 . 0204 2 6 1 . 7918 0 . 1823 3 5 1 . 6094 f [ x 3 , x 2 , x1 , x 0 ] 0 . 204 ( 0 . 0518 ) 5 1 0 . 0079 f n ( x ) b0 ( x x 0 ) f [ x 1 , x 0 ] ( x x 0 )( x x 1 ) f [ x 2 , x 1 , x 0 ] ( x x 0 )( x x 1 )( x x 2 ) f [ x 3 , x 2 , x1 , x 0 ] i xi f ( xi ) first second third 0 1 0 0 . 4621 0 . 0518 0 . 0079 1 4 1 . 3863 0 . 2027 0 . 0204 2 6 1 . 7918 0 . 1823 3 5 1 . 6094 f n ( x ) 0 . 4621 ( x 1) 0 . 0518 ( x 1)( x 4 ) 0 . 0079 ( x 1)( x 4 )( x 6 ) f n ( 2 ) 0 . 4621 ( 2 1) 0 . 0518 ( 2 1)( 2 4 ) 0 . 0079 ( 2 1)( 2 4 )( 2 6 ) 0 . 6289 Newton Interpolation Pseudo Code See the textbook! Features of Newton Divided-Differences to get Interpolating Polynomial Data need not be equally spaced Arrangement of data does not have to be ascending or descending, but it does influence error of interpolation Best case is when the base points are close to the unknown value Estimate of relative error: R n f n 1 ( x ) f n ( x ) Error estimate for nth-order polynomial is the difference between the (n+1)th and nth-order prediction. Relative Error As a Function of Order Example 18.5 in text Error 0.5 Determine ln(2) using the following table True Error Estimated error 0 5 Order Estimated error (reversed) -0.5 x f(x )= ln (x ) 1 4 6 5 3 1 .5 2 .5 3 .5 0 1 .3 8 6 3 1 .7 9 1 8 1 .6 0 9 4 1 .0 9 8 6 0 .4 0 5 5 0 .9 1 6 3 1 .2 5 2 8 MATLAB function interp1 is very useful for this Midterm 2 Tuesday 15 April