Interpolation Assoc. Prof. Dr. Siridech Boonsang Electrical Engineering xi x xi+1 Spline Interpolation • Polynomials are the most common choice of interpolants. • There are cases where polynomials can lead to erroneous results because of round off error and overshoot. • Alternative approach is to apply lower-order polynomials to subsets of data points. Such connecting polynomials are called spline functions. Spline provides a superior approximation of the behavior of functions that have local, abrupt changes (d). Linear Spline The first order splines for a group of ordered data points can be defined as a set of linear functions: f ( x) f ( x0 ) m0 ( x x0 ) f ( x) f ( x1 ) m1 ( x x1 ) x0 x x1 x1 x x2 f ( x) f ( xn1 ) mn1 ( x xn1 ) xn1 x xn f ( xi 1 ) f ( xi ) mi xi 1 xi Linear spline - Example Fit the following data with first order splines. Evaluate the function at x = 5. x f(x) 3.0 4.5 7.0 9.0 2.5 1.0 2.5 0.5 2.5 1 m 0.6 7 4.5 f (5) f (4.5) m(5 4.5) 1.0 0.6 0.5 1.3 Parametric Continuity Conditions • Zero order parametric (C0): Simply the curves meet, C1(1) = C2(0) . • First order parametric (C1): The first parametric derivations for two successive curve sections are equal at their joining point, C´1(1)= C´2(0) Parametric Continuity Conditions • Second order parametric (C2): Both the first and second parametric derivatives of the two curve sections are the same at the intersection, C˝1(1)= C˝2(0) Linear Spline • The main disadvantage of linear spline is that they are not smooth. The data points where 2 splines meets called (a knot), the changes abruptly. • The first derivative of the function is discontinuous at these points. • Using higher order polynomial splines ensure smoothness at the knots by equating derivatives at these points. Quadric Splines • Objective: to derive a second order polynomial for each 2 interval between data points. f i ( x) ai x bi x ci • Terms: Interior knots and end points For n+1 data points: • i = (0, 1, 2, …n), • n intervals, • 3n unknown constants (a’s, b’s and c’s) Quadric Splines • The function values of adjacent polynomial must be equal at the interior knots 2(n-1). ai 1 xi 1 bi 1 xi 1 ci 1 f i ( xi 1 ) i 2, 3, 4,..., n 2 ai xi 1 bi xi 1 ci f i ( xi 1 ) 2 i 2, 3, 4,..., n • The first and last functions must pass through the end points (2). a1 x0 b1 x0 c1 f ( x0 ) 2 an xn bn xn cn f ( xn ) 2 Quadric Splines • The first derivatives at the interior knots must be equal (n-1). f i ( x) 2ai x bi ' 2ai 1 xi 1 bi 1 2ai xi 1 bi • Assume that the second derivate is zero at the first point (1) a1 0 (The first two points will be connected by a straight line) Quadric Splines - Example Fit the following data with quadratic splines. Estimate the value at x = 5. x f(x) 3.0 4.5 7.0 9.0 2.5 1.0 2.5 0.5 Solutions: There are 3 intervals (n=3), 9 unknowns. Quadric Splines - Example 1. Equal interior points: For first interior point (4.5, 1.0) The 1st equation: x12 a1 x1b1 c1 f ( x1 ) (4.5) 2 a1 4.5b1 c1 f (4.5) The 2nd equation: 20.25 a1 4.5 b1 c1 1.0 x12 a2 x1b2 c2 f ( x1 ) (4.5) 2 a2 4.5b2 c2 f (4.5) 20.25a2 4.5b2 c2 1.0 Quadric Splines - Example For second interior point (7.0, 2.5) The 3rd equation: x22 a2 x2b2 c2 f ( x2 ) (7) 2 a2 7b2 c2 f (7) 49a2 7b2 c2 2.5 The 4th equation: x22 a3 x2b3 c3 f ( x2 ) (7) a3 7b3 c3 f (7) 2 49a3 7b3 c3 2.5 Quadric Splines - Example First and last functions pass the end points For the start point (3.0, 2.5) x a1 x0 b1 c1 f ( x0 ) 2 0 9a1 3b1 c1 2.5 For the end point (9, 0.5) x a x3b3 c3 f ( x3 ) 2 3 1 81a3 9b3 c3 0.5 Quadric Splines - Example Equal derivatives at the interior knots. For first interior point (4.5, 1.0) 2 x1 a1 b1 2 x1 a2 b2 9a1 b1 9a2 b2 For second interior point (7.0, 2.5) 2 x2 a2 b2 2 x3 a3 b3 14a2 b2 14a3 b3 Second derivative at the first point is 0 f '' ( x0 ) a1 0 Quadric Splines - Example 4.5 1 0 0 0 0 0 0 3 1 0 0 1 0 0 0 0 0 0 0 0 20.25 4.5 1 0 0 49 7 1 0 0 0 0 0 49 7 0 0 0 0 0 0 0 0 81 9 0 0 9 1 0 14 0 1 14 1 0 b1 1 c 0 1 1 0 a 2 2 .5 1 b2 2.5 2 .5 0 c2 1 a3 0.5 0 b3 0 0 c3 0 Quadric Splines - Example Solving these 8 equations with 8 unknowns a1 0, b1 1, c1 5.5 a2 0.64, b2 6.76, c2 18.46 a3 1.6, b3 24.6, c3 91.3 f1 ( x) x 5.5, 3.0 x 4.5 f 2 ( x) 0.46 x 2 6.76 x 18.46, 4.5 x 7.0 f3 ( x) 1.6 x 2 24.6 x 91.3, 7.0 x 9.0 Cubic Splines Objective: to derive a third order polynomial for each interval between data points. Terms: Interior knots and end points f i ( x) ai x bi x ci x d i 3 2 For n+1 data points: • i = (0, 1, 2, …n), • n intervals, • 4n unknown constants (a’s, b’s ,c’s and d’s) Cubic Splines • The function values must be equal at the interior knots (2n-2). • The first and last functions must pass through the end points (2). • The first derivatives at the interior knots must be equal (n-1). • The second derivatives at the interior knots must be equal (n-1). • The second derivatives at the end knots are zero (2), (the 2nd derivative function becomes a straight line at the end points) Alternative technique to get Cubic Splines • The second derivative within each interval [xi-1, xi ] is a straight line. (the 2nd derivatives can be represented by first order Lagrange interpolating polynomials. x xi x xi 1 '' f i ( x) f i ( xi 1 ) f i ( xi ) xi 1 xi xi xi 1 '' '' A straight line connecting the first knot f’’(xi-1) and the second knot f’’(xi) The second derivative at any point x within the interval Cubic Splines • The last equation can be integrated twice 2 unknown constants of integration can be evaluated by applying the boundary conditions: 1. f(x) = f (xi-1) at xi-1 2. f(x) = f (xi) at xi '' f i ( x) '' f i ( xi 1 ) xi x 3 f i ( xi ) x xi 1 3 6xi xi 1 6xi xi 1 f i ( xi 1 ) f i ( xi 1 )xi xi 1 xi x 6 xi xi 1 '' '' f i ( xi ) f i ( xi )xi xi 1 x xi 1 6 xi xi 1 Unknowns: f ' ' ( xi ) f ' ' ( xi 1 ) i = 0, 1,…, n Cubic Splines •For each interior point xi (n-1): f i1 ( xi ) f i ( xi ) ' ' ( xi xi 1 ) f '' ( xi 1 ) 2( xi 1 xi 1 ) f '' ( xi ) ( xi 1 xi ) f '' ( xi 1 ) 6 f ( xi 1 ) f ( xi ) xi 1 xi 6 f ( xi 1 ) f ( xi ) xi xi 1 This equation result with n-1 unknown second derivatives where, for boundary points: f˝(xo) = f˝(xn) = 0 Cubic Splines - Example Fit the following data with cubic splines Use the results to estimate the value at x=5. x f(x) 3.0 2.5 4.5 1.0 7.0 2.5 9.0 0.5 Solution: Natural Spline: f '' ( x0 ) f '' (3) 0, f '' ( x3 ) f '' (9) 0 Cubic Splines - Example For 1st interior point (x1 = 4.5) x f(x) 3.0 2.5 4.5 1.0 7.0 2.5 9.0 0.5 - xi xi 1 x1 x0 4.5 3.0 1.5 - xi 1 xi 1 x2 x0 7 3.0 4 - xi 1 xi x2 x1 7 4.5 2.5 Apply the following equation: ( xi xi 1 ) f '' ( xi 1 ) 2( xi 1 xi 1 ) f '' ( xi ) ( xi 1 xi ) f '' ( xi 1 ) 6 6 f ( xi 1 ) f ( xi ) f ( xi 1 ) f ( xi ) xi 1 xi xi xi 1 Cubic Splines - Example 6 6 1.5 f (3) 2 4 f (4.5) 2.5 f (7) (2.5 1) (2.5 1) 2.5 1.5 '' f (3) 0 Since '' '' '' 8 f '' (4.5) 2.5 f '' (7) 9.6 .............. (eq.1) For 2nd interior point (x2 = 7 ) x 3.0 4.5 7.0 9.0 f(x) 2.5 1.0 2.5 0.5 xi xi 1 x2 x1 7 4.5 2.5 xi 1 xi 1 x3 x1 9 4.5 4.5 xi 1 xi x3 x2 9 7 2 Cubic Splines - Example Apply the following equation: ( xi xi 1 ) f '' ( xi 1 ) 2( xi 1 xi 1 ) f '' ( xi ) ( xi 1 xi ) f '' ( xi 1 ) 6 6 f ( xi 1 ) f ( xi ) f ( xi 1 ) f ( xi ) xi 1 xi xi xi 1 2.5 f '' (4.5) 2 4.5 f '' (7) 2 f '' (9) Since 6 6 (0.5 2.5) (1 2.5) 2 2.5 f '' (9) 0 2.5 f '' (4.5) 9 f '' (7) 9.6 ............. (equ 2) Cubic Splines - Example Solve the two equations: 8 f i '' (4.5) 2.5 f i '' (7) 9.6 '' '' yeild f ( 4 . 5 ) 1 . 67909 , f (7) 1.53308 '' '' 2.5 f i (4.5) 9 f i (7) 9.6 The first interval (i=1), apply for the equation: '' '' f (x ) f i ( xi ) 3 x xi 1 3 f i ( x) i i 1 xi x 6 xi xi 1 6 xi xi 1 '' f i ( xi 1 ) f i '' ( xi 1 ) xi xi 1 f i ( xi ) f i ( xi ) xi xi 1 xi x x xi 1 6 6 xi xi 1 xi xi 1 f1 ( x) 0 ( xi 3)3 1.67909 2.5 0(1.5) 1 1.67909(1.5) ( x 3)3 4 . 5 x ( x 3) 6(1.5) 1 . 5 6 1 . 5 6 f1 ( x) 0.186566 ( x 3)3 1.6667 (4.5 x) 0.24689( x 3) Cubic Splines - Example The 2nd interval (i =2), apply for the equation: f 2 ( x) 1.67909 1.53308 1 1.67909(2.5) 7 x (7 x ) 3 ( x 4.5)3 6(2.5) 6(2.5) 6 2.5 2.5 1.53308(2.5) ( x 4.5) 6 2.5 f 2 ( x) 0.111939(7 x)3 0.102205 ( x 4.5)3 0.29962(7 x) 1.638783 ( x 4.5) The 3rd interval (i =3), f 3 ( x) 0.127757(9 x)3 1.761027 (9 x) 0.25 ( x 7) For x = 5: f 2 ( x) f 2 (5) 1.102886