MATH-415 Numerical Analysis INTERPOLATION. INTERPOLATING POLYNOMIALS 1 What is Interpolation ? Given (x0,y0), (x1,y1), …… (xn,yn), find the value of y at a value of x that is not given. Linear Interpolation 3 Interpolants Polynomials are the most common choice of interpolants because they are easy to: Evaluate Differentiate Integrate 4 Interpolation • Thus interpolation is the estimation of intermediate values between precise data points. The most common method is: f ( x) a0 a1 x a2 x 2 an x n • Although there is one and only one nth-order polynomial that fits n+1 points, there are different mathematical formats in which this polynomial can be expressed. The most popular are: The Newton polynomial The Lagrange polynomial 5 Direct Polynomial Interpolation Given n+1 data points (x0,y0), (x1,y1),………….. (xn,yn), pass a polynomial of order n through the data as given below: y a0 a1 x .................... an x . n where a0, a1,………………. an are real constants. • Set up n+1 equations to find n+1 constants. • To find the value y at a given value of x, simply substitute the value of x in the above polynomial. http://numericalmethods.eng.usf. edu 6 Coefficients of an Interpolating Polynomial • Since n+1 data points are required to determine n+1 coefficients, simultaneous linear systems of equations can be used to calculate “a”s: f ( x0 ) a0 a1 x0 a2 x02 an x0n f ( x1 ) a0 a1 x1 a2 x12 an x1n f ( xn ) a0 a1 xn a2 xn2 an xnn • where “x”s are the knowns and “a”s are the unknowns by Lale Yurttas, Texas A&M University 7 Example 1 The upward velocity of a rocket is given as a function of time in Table 1. Find the velocity at t=16 seconds using the direct method for linear interpolation. Table 1 Velocity as a function of time. t , s vt , m/s 0 0 10 227.04 15 362.78 20 517.35 22.5 602.97 30 901.67 Figure 2 Velocity vs. time data for the rocket example http://numericalmethods.eng.usf. edu 8 Example 1: Solution vt a0 a1t y v15 a0 a1 15 362.78 v20 a0 a1 20 517.35 x1 , y1 x0 , y0 f1 x Solving the above two equations gives, a0 100.93 a1 30.914 x Figure 3 Linear interpolation. Hence vt 100.93 30.914t, 15 t 20. v16 100.93 30.91416 393.7 m/s http://numericalmethods.eng.usf. edu 9 Newton’s Divided-Difference Interpolating Polynomials BC DE AB AD BC f1 ( x) f ( x0 ) ABC E ADE AB x x0 DE f ( x1 ) f ( x0 ) C AD x1 x0 A B D f1 ( x) f ( x0 ) f ( x1 ) f ( x0 ) x x0 x1 x0 10 Newton’s Divided-Difference Interpolating Polynomials Linear Interpolation • Is the simplest form of interpolation, connecting two data points with a straight line. f1 ( x) f ( x0 ) f ( x1 ) f ( x0 ) x x0 x1 x0 Slope and a finite divided difference approximation to 1st derivative f ( x1 ) f ( x0 ) f1 ( x) f ( x0 ) ( x x0 ) Linear-interpolation x1 x0 formula • f1(x) designates that this is a first-order interpolating polynomial 11 Newton’s Divided-Difference Interpolating Polynomials Quadratic Interpolation • If three data points are available, the estimate is improved by introducing some curvature into the line connecting the points: f 2 ( x) b0 b1 ( x x0 ) b2 ( x x0 )( x x1 ) • A simple procedure can be used to determine the values of the coefficients: x x0 b0 f x0 f x1 f x0 xx b 1 x x2 1 x1 x0 f x2 f x1 f x1 f x0 x2 x1 x1 x0 b2 x2 x0 12 General Form of Newton’s Interpolating Polynomials f n ( x) f ( x0 ) ( x x0 ) f [ x1 , x0 ] ( x x0 )( x x1 ) f [ x2 , x1 , x0 ] ( x x0 )( x x1 ) ( x xn 1 ) f [ xn , xn 1 , , x0 ] b0 f ( x0 ) b1 f [ x1 , x0 ] b2 f [ x2 , x1 , x0 ] bn f [ xn , xn 1 , , x1 , x0 ] f [ xi , x j ] f ( xi ) f ( x j ) f [ xi , x j , xk ] xi x j f [ xi , x j ] f [ x j , xk ] Bracketed function evaluations are finite divided differences xi xk f [ xn , xn 1 , , x1 , x0 ] f [ xn , xn 1 , , x1 ] f [ xn 1 , xn 2 , , x0 ] xn x0 13 Lagrange Interpolating Polynomials • The Lagrange interpolating polynomial is simply a reformulation of the Newton’s polynomial that avoids the computation of divided differences: n f n ( x) Li ( x) f ( xi ) i 0 n Li ( x) j 0 j i by Lale Yurttas, Texas A&M University x xj xi x j 14 Lagrange Interpolating Polynomials Lagrangian interpolating polynomial is given by n f n ( x) Li ( x) f ( xi ) i 0 where ‘ n ’ in f n (x) stands for the n th order polynomial that approximates the function y f (x) given at (n 1) data points as x0 , y 0 , x1 , y1 ,......, x n 1 , y n 1 , x n , y n , and n Li ( x) j 0 j i x xj xi x j Li (x) is a weighting function that includes a product of (n 1) terms with terms of j i omitted. http://numericalmethods.eng.usf. edu 15 Lagrange Interpolating Polynomials x x0 x x1 f1 ( x) f ( x0 ) f ( x1 ) x0 x1 x1 x0 L0 x x1 x x2 x x0 x x2 f 2 ( x) f ( x0 ) f ( x1 ) x0 x1 x0 x 2 x1 x0 x1 x 2 x x0 x x1 f ( x2 ) x2 x0 x2 x 1 L 1 L2 16 Lagrange Interpolating Polynomials 17 Error of Interpolation • For an nth-order interpolating polynomial, an analogous relationship for the error is: f ( n1) (x ) Rn ( x x0 )( x x1 ) ( x xn ) (n 1)! • x is located in some interval containing the unknown and the data: x, x0 , x1 ,..., xn 18 Example The upward velocity of a rocket is given as a function of time in Table 1. Find the velocity at t=16 seconds using the Lagrangian method for linear interpolation. Table Velocity as a function of time t (s) v(t ) (m/s) 0 10 15 20 22.5 30 0 227.04 362.78 517.35 602.97 901.67 Figure. Velocity vs. time data for the rocket example http://numericalmethods.eng.usf. edu 19 Linear Interpolation 517.35 550 1 v(t ) Li (t )v(ti ) 500 i 0 L0 (t )v (t 0 ) L1 (t )v (t1 ) ys f ( range) f x desired t 0 15, t 0 362.78 t1 20, t1 517.35 450 400 362.78 350 10 12 x s 10 0 http://numericalmethods.eng.usf. edu 14 16 18 20 22 24 x s range x desired x s 10 1 20 Linear Interpolation (contd) 1 L0 (t ) j 0 j 0 1 L1 (t ) j 0 j 1 v(t ) v(16) t tj t0 t j t tj t1 t j t t1 t 0 t1 t t0 t1 t 0 t t0 t t1 t 20 t 15 v(t 0 ) v(t1 ) (362.78) (517.35) t 0 t1 t1 t 0 15 20 20 15 16 20 16 15 (362.78) (517.35) 15 20 20 15 0.8(362.78) 0.2(517.35) 393.7 m/s. http://numericalmethods.eng.usf. edu 21 Quadratic Interpolation For the second order polynomial interpolatio n (also called quadratic interpolation), we choose the velocity given by 2 v (t ) Li ( t ) v(t i ) i 0 L0 (t )v (t 0 ) L1 (t ) v( t1 ) L2 (t ) v( t 2 ) http://numericalmethods.eng.usf. edu 22 Example The upward velocity of a rocket is given as a function of time in Table 1. Find the velocity at t=16 seconds using the Lagrangian method for quadratic interpolation. Table Velocity as a function of time t (s) v(t ) (m/s) 0 10 15 20 22.5 30 0 227.04 362.78 517.35 602.97 901.67 Figure. Velocity vs. time data for the rocket example http://numericalmethods.eng.usf. edu 23 Quadratic Interpolation (contd) t 0 10, v(t 0 ) 227.04 517.35 t1 15, v(t1 ) 362.78 550 500 t 2 20, v(t 2 ) 517.35 450 ys 400 2 L0 (t ) j 0 j 0 2 L1 (t ) j 0 j 1 2 L2 (t ) j 0 j 2 t tj t0 t j t t j t1 t j t tj t2 t j t t1 t t 2 t t t t 0 1 0 2 t t0 t1 t 0 t t 2 t1 t 2 f ( range) f x desired 300 t t 0 t t1 t t t t 2 0 2 1 350 250 227.04 200 10 12 10 http://numericalmethods.eng.usf. edu 14 16 18 20 x s range x desired 20 24 Quadratic Interpolation (contd) t t1 t t2 t t0 t t 2 t t0 t t1 v t v t v t 0 1 v t2 t t t t t t t t t t t t 0 1 0 2 1 0 1 2 2 0 2 1 16 15 16 20 16 10 16 20 16 10 16 15 v 16 227.04 362.78 517.35 10 15 10 20 15 10 15 20 20 10 20 15 0.08 227.04 0.96 362.78 0.12 527.35 392.19 m/s The absolute relative approximate error a obtained between the results from the first and second order polynomial is 392.19 393.70 100 392.19 0.38410% a http://numericalmethods.eng.usf. edu 25 Cubic Interpolation For the third order polynomial (also called cubic interpolation), we choose the velocity given by 3 v (t ) Li ( t ) v(t i ) i 0 L0 (t ) v( t 0 ) L1 ( t ) v(t 1 ) L2 ( t ) v(t 2 ) L3 ( t ) v(t 3 ) 602.97 700 600 ys 500 f ( range) f x desired 400 300 227.04 200 10 10 12 14 16 18 20 22 x s range x desired http://numericalmethods.eng.usf. edu 24 22.5 26 Cubic Interpolation (contd) t o 10, v t o 227.04 t1 15, v t1 362.78 t 2 20, v t 2 517.35 t 3 22.5, v t 3 602.97 3 L0 (t ) j 0 j 0 t tj t0 t j t t 1 t t 2 t t 3 ; t t t t t t 0 1 0 2 0 3 602.97 700 600 3 L1 (t ) j 0 j 1 3 L2 (t ) j 0 j 2 3 L3 ( t ) j 0 j 3 t t j t1 t j t t0 t1 t 0 t t 2 t t 3 t t 1 2 t1 t 3 ys 500 f ( range) t tj t2 t j t t 0 t t1 t t 3 t t t t 2 0 2 1 t 2 t 3 ; f x desired 400 300 t tj t3 t j t t 0 t t1 t t 2 t t t t t t 3 0 3 1 3 2 227.04 200 10 12 10 http://numericalmethods.eng.usf. edu 14 16 18 x s range x desired 20 22 24 22.5 27 Cubic Interpolation (contd) t t1 t t 2 t t3 t t0 t t 2 t t3 vt1 vt 2 vt t t t t t t t t t t t t 0 1 0 2 0 3 1 0 1 2 1 3 t t0 t t1 t t3 t t1 t t1 t t 2 vt 2 vt3 t t t t t t t t t t t t 2 0 2 1 2 3 3 1 3 1 3 2 16 15 16 20 16 22.5 16 10 16 20 16 22.5 v16 227.04 362.78 10 15 10 20 10 22.5 15 10 15 20 15 22.5 16 10 16 15 16 22.5 16 10 16 15 16 20 517.35 602.97 20 10 20 15 20 22 . 5 22 . 5 10 22 . 5 15 22 . 5 20 0.0416 227.04 0.832 362.78 0.312 517.35 0.1024602.97 392.06 m/s The absolute relative approximate error a obtained between the results from the second and third order polynomial is 392.06 392.19 a 100 392.06 0.033269% http://numericalmethods.eng.usf. edu 28 Comparison Table Order of Polynomial 1 2 3 v(t=16) m/s 393.69 392.19 392.06 Absolute Relative Approximate Error -------- 0.38410% 0.033269% http://numericalmethods.eng.usf. edu 29 Lagrangian Interpolation: Algorithm Function Lagrange(z,x,f) % Returns an interpolated value at z % x is an (n+1)-dimensional array of the given data points % f is an (n+1)-dimensional array of the given function values Interpolated_Value=0 for i=1:n+1 Lagrangian=1 for j=1:n+1 if(j~=i) then Lagrangian=Lagrangian*(z-x(j))/(x(i)-x(j)) end if end for j Interpolated_Value=Interpolated_Value+Lagrangian*f(i) end for I Return Interpolated_Value 30 Spline Interpolation • There are cases where polynomials can lead to erroneous results because of round-off errors or significant distinction between an interpolating polynomial and an interpolated function • Alternative approach is to apply lower-order polynomials to subsets of data points. Such connecting polynomials are called spline functions 31 Why Splines ? 1 f ( x) 1 25 x 2 Table : Six equidistantly spaced points in [-1, 1] x y 1 1 25 x 2 -1.0 0.038461 -0.6 0.1 -0.2 0.5 0.2 0.5 0.6 0.1 1.0 0.038461 Figure : 5th order polynomial vs. exact function http://numericalmethods.eng.usf. edu 32 Why Splines ? 1.2 0.8 y 0.4 0 -1 -0.5 0 0.5 1 -0.4 -0.8 x 19th Order Polynomial f (x) 5th Order Polynomial Figure : Higher order polynomial interpolation is a bad idea http://numericalmethods.eng.usf. edu 33 Linear Spline Interpolation Given x0 , y0 , x1 , y1 ,......, x n 1 , y n1 x n , y n , fit linear splines to the data. This simply involves forming the consecutive data through straight lines. So if the above data is given in an ascending order, the linear splines are given by yi f ( xi ) Figure : Linear splines http://numericalmethods.eng.usf. edu 34 Quadratic Spline Interpolation Given x0 , y0 , x1 , y1 ,......, x n 1 , y n 1 , x n , y n , fit quadratic splines through the data. The splines are given by f ( x ) a1 x 2 b1 x c1 , a 2 x 2 b2 x c2 , x 0 x x1 x1 x x 2 . . . a n x 2 bn x cn , x n 1 x x n Find a i , bi , ci , i 1, 2, …, n http://numericalmethods.eng.usf. edu 35 Spline Interpolation 36 Spline Interpolation 37 38