Interpolation Escuela de Ingeniería Informática de Oviedo (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 1 / 34
Outline 1 Introduction 2 Lagrange interpolation 3 Piecewise polynomial interpolation (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 2 / 34
Introduction Outline 1 Introduction 2 Lagrange interpolation 3 Piecewise polynomial interpolation (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 3 / 34
Introduction Motivation Often, when solving mathematical problems, we need to get the value of a function in several points. But: it can be expensive in terms of processor use or machine time (for instance, a complicated function which we need to evaluate many times). possibly, we only have the function values in few points (for instance, if they come from data trials). A possible solution is to replace the complicated function by another similar but simpler to evaluate. These simpler functions are polynomials, trigonometric functions,... (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 4 / 34
Introduction Interpolation Data: n + 1 different points x 0, x 1,..., x n n + 1 values ω 0, ω 1,..., ω n Problem: Find a (simple) function f verifying f (x i ) = y i with i = 0, 1,..., n. Remarks: x 0, x 1,..., x n are called interpolation nodes ω 0, ω 1,..., ω n can be the values of a function f in the nodes: ω i = f (x i ) i = 0, 1,..., n (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 5 / 34
Introduction Example Here, f P 5 is a polynomial of degree 5: (x i,ω i ) P 5 (x 4,ω 4 ) (x 2,ω 2 ) (x 0,ω 0 ) (x 1,ω 1 ) (x 3,ω 3 ) (x 5,ω 5 ) (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 6 / 34
Introduction Polynomial interpolation If f is a polynomial or a piecewise polynomial function we speak of polynomic interpolation. Main examples: 1 Lagrange interpolation. 2 Piecewise polynomial interpolation. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 7 / 34
Lagrange interpolation Outline 1 Introduction 2 Lagrange interpolation 3 Piecewise polynomial interpolation (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 8 / 34
Lagrange interpolation Lagrange interpolation Data: n + 1 different points x 0, x 1,..., x n n + 1 values ω 0, ω 1,..., ω n Problem: We seek for a polynomial P n of degree at most n verifying P n (x 0 ) = ω 0 P n (x 1 ) = ω 1 P n (x 2 ) = ω 2... P (n) n (x n ) = ω n (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 9 / 34
Lagrange interpolation Lagrange linear interpolation (n = 1) Data: 2 different points x 0, x 1 2 values ω 0, ω 1 Problem: Find a polynomial P n of degree at most 1 verifying P 1 (x 0 ) = ω 0 P 1 (x 1 ) = ω 1 y = P 1 (x) is the straight line joining (x 0, ω 0 ) and (x 1, ω 1 ) P 1 (x) = ω 0 + ω 1 ω 0 x 1 x 0 (x 1 x 0 ) (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 10 / 34
Lagrange interpolation Lagrange interpolation (for any n) Problem: Find a polynomial satisfying P n (x) = a 0 + a 1 x + a 2 x 2 + + a n x n P n (x 0 ) = ω 0 P n (x 1 ) = ω 1 P n (x 2 ) = ω 2... P n (x n ) = ω n (1) Conditions (1) imply that coefficients solve 1 x 0 x0 2 x n 0 1 x 1 x1 2 x1 n....... 1 x n xn 2 xn n a 0 a 1. a n = ω 0 ω 1. ω n (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 11 / 34
Lagrange interpolation The coefficient matrix is of Vandermonde type: 1 x 0 x0 2 x0 n 1 x 1 x1 2 x1 n A =....... 1 x n xn 2 xn n, with det (A) = 0 l k n (x k x l ) 0. Therefore, the system has a unique solution which determines P n. P n is the Lagrange interpolation polynomial at x 0, x 1,..., x n relative to the values ω 0, ω 1,..., ω n. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 12 / 34
Lagrange interpolation An example of Lagrange interpolation (x i,ω i ) P 1 (x) (x i,ω i ) P 2 (x) (x i,ω i ) P 3 (x) (x i,ω i ) P 4 (x) (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 13 / 34
Lagrange interpolation Computing the Lagrange s interpolation polynomial Computing the Lagrange s interpolation polynomial Motivation: Solving the Vandermonde system of equations is not efficient and large errors may arise due to bad conditioning. Two ways of computing: Using fundamental Lagrange polynomials. Using divided differences. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 14 / 34
Lagrange interpolation Computing the Lagrange s interpolation polynomial Fundamental Lagrange polynomials Fundamental polynomials: For each i = 0, 1,..., n there exists an unique polynomial l i of degree at most n such that l i (x k ) = δ ik (zero if i k, one if i = k): l i (x) = n j = 0 j i x x j x i x j, l 0, l 1,..., l n are the fundamental Lagrange polynomials of degree n. Lagrange formula: Lagrange s polynomial at x 0, x 1,..., x n relative to ω 0, ω 1,..., ω n is P n (x) = ω 0 l 0 (x) + ω 1 l 1 (x) + + ω n l n (x). Remark: If we want to add new nodes, we need to re-compute all the fundamental polynomials. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 15 / 34
Lagrange interpolation Computing the Lagrange s interpolation polynomial Example 3 2 1 0 1 l 0 (x) 2 0 1 2 3 4 3 2 1 0 1 l 1 (x) 2 0 1 2 3 4 3 2 1 0 1 l 2 (x) 2 0 1 2 3 4 3 2 1 0 1 ω 0 l 0 (x) 2 0 1 2 3 4 3 2 1 0 1 ω 1 l 1 (x) 2 0 1 2 3 4 3 2 1 0 1 ω 2 l 2 (x) 2 0 1 2 3 4 3 2 1 0 1 P 2 (x) 2 0 1 2 3 4 (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 16 / 34
Lagrange interpolation Computing the Lagrange s interpolation polynomial Divided differences Divided differences of 0-order [ω i ] = ω i, i = 0, 1,..., n. Divided differences of order 1 Divided differences of order 2 [ω i, ω i+1 ] = ω i+1 ω i x i+1 x i, i = 0, 1,..., n 1. [ω i, ω i+1, ω i+2 ] = [ω i+1, ω i+2 ] [ω i, ω i+1 ] x i+2 x i, i = 0, 1,..., n 2. Divided differences of order k (k = 1,..., n) [ω i, ω i+1,..., ω i+k ] = [ω i+1,..., ω i+k ] [ω i, ω i+1,..., ω i+k 1 ] x i+k x i, i = 0, 1,..., n k. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 17 / 34
Lagrange interpolation Computing the Lagrange s interpolation polynomial Newton s formula: Lagrange polynomial at x 0, x 1,..., x n relative to ω 0, ω 1,..., ω n is P n (x) = [ω 0 ] + [ω 0, ω 1 ] (x x 0 ) + [ω 0, ω 1, ω 2 ] (x x 0 ) (x x 1 ) + + + [ω 0, ω 1,..., ω n ] (x x 0 ) (x x 1 ) (x x n 1 ). Consequence: P 0, P 1,..., P n may be computed recursively, adding in each iteration a new term, P n (x) = P n 1 (x) + [ω 0, ω 1,..., ω n ] (x x 0 ) (x x 1 ) (x x n 1 ). Notation: If ω i = f (x i ), with i = 0, 1,..., n, f [x i,..., x i+k ] = [ω i,..., ω i+k ]. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 18 / 34
Example Lagrange interpolation Computing the Lagrange s interpolation polynomial P 1 (x) P 2 (x) P 3 (x) P 4 (x) (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 19 / 34
Error Lagrange interpolation Error Hypothesis: f : [a, b] R is n + 1 times differentiable in [a, b] with continuous derivatives. x 0, x 1,..., x n [a, b]. ω i = f (x i ) for i = 0, 1,..., n. Error estimate: The absolute error satisfies f (x) P n (x) sup f (n+1) (y) (x x 0) (x x 1 ) (x x n ). (n + 1)! y [a,b] (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 20 / 34
Piecewise polynomial interpolation Outline 1 Introduction 2 Lagrange interpolation 3 Piecewise polynomial interpolation (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 21 / 34
Piecewise polynomial interpolation Piecewise polynomial interpolation Motivation: If we increase the number of nodes The Lagrange s interpolation polynomial degree increases as well, generating oscillations. Often, when increasing the number of nodes the error also increases (not intuitive). Estrategy: Use low degree piecewise polynomials. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 22 / 34
Piecewise polynomial interpolation Example: oscillations of Lagrange polynomials e x2 (x i,ω i ) P 10 (x) (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 23 / 34
Piecewise polynomial interpolation Example: piecewise linear interpolation e x2 (x i,ω i ) s (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 24 / 34
Piecewise polynomial interpolation Constantwise polynomial interpolation Interpolation by degree zero piecewise polynomials is that in which the polynomials are constant between nodes. For instance, ω 0 if x [x 0, x 1 ), ω 1 if x [x 1, x 2 ), f (x) =... ω n 1 if x [x n 1, x n ), ω 0 if x = x n. Observe that if ω i ω i+1 then f is discontinuous at x i+1. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 25 / 34
Piecewise polynomial interpolation Linearwise polynomial interpolation The degree one piecewise polynomial interpolation is that in which the polynomials are straight lines joining two consecutive nodes, for i = 0,..., n 1. f (x) = ωi + (ω i+1 ω i ) x x i x i+1 x i if x [x i, x i+1 ], In this case, f is continuous, but its first derivative is, in general, discontinuous at the nodes. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 26 / 34
Piecewise polynomial interpolation Spline interpolation Data: n + 1 points x 0 < x 1 <... < x n. n + 1 values ω 0, ω 1,..., ω n. Problem: Interpolating by splines of order p (or degree p) consists on finding a function f such that: 1 f is p 1 times continuously differentiable in [x0, x n ]. 2 f is a piecewise function given by the polynomials f0, f 1,..., f n 1 defined, respectively, in [x 0, x 1 ], [x 1, x 2 ],..., [x n 1, x n ], and of degree lower or equal to p. 3 Interpolation condition: f 0 (x 0 ) = ω 0,..., f n (x n ) = ω n. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 27 / 34
Piecewise polynomial interpolation The problem has solution! Each solution f is called spline interpolator of order p at x 0, x 1,..., x n relative to ω 0, ω 1,..., ω n. The most used spline is that ot third order, known as cubic spline. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 28 / 34
Example Piecewise polynomial interpolation Cubic spline sin(x) Lineal Spline (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 29 / 34
Piecewise polynomial interpolation Cubic spline Cubic spline Definition: A Cubic spline is a function f such that 1 f is twice continuously differentiable in [x0, x n ]. 2 f0, f 1,..., f n 1 are of degree at most 3. 3 f (x0 ) = ω 0 f (x1 ) = ω 1... f (xn ) = ω n Remarks: In each sub-interval [x i, x i+1 ], the interpolant f is determined from the values of f at x i and x i+1. Such values ( f (x i ) and f (x i+1 )) are obtained by solving a linear system of equations. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 30 / 34
Piecewise polynomial interpolation Computing the cubic spline Cubic spline Task: Deduce an algorithm to compute the cubic spline. By definition, f is continuous in [x 0, x n ]. Therefore ω 0 = ω 1 = ω 2 = f f f 0 (x 0) 0 (x 1) = f 1 (x 1) 1 (x 2) = f 2 (x 2) ω n 1 = f n 2 (x n 1) = ω n = f n 1 (x n) f n 1 (x n 1) (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 31 / 34
Piecewise polynomial interpolation Cubic spline Integrating twice, we get fi (x) = ω i (x i+1 x) 3 + ω (x x i ) 3 i+1 + a i (x i+1 x) + b i (x x i ), 6h i 6h i where a i = ω i ω h i i h i 6, b i = ω i+1 ω h i i+1 h i 6. Therefore, once ω i are known, the cubic spline is fully determined. Using that f is twice continuously differentiable gives the n 1 linear equations h i 6 ω i + h i+1 + h i ω i+1 + h i+1 3 6 ω i+2 = ω i ( 1 + 1 ) ωi+1 + ω i+2. h i h i+1 h i h i+1 For full determination of the n + 1 values ω i we need two additional equations. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 32 / 34
Piecewise polynomial interpolation Cubic spline Natural cubic spline: There are several options to determine uniquely the cubic spline, for instance adding two equations to close the system, fix the value of two unknowns. If the values of ω 0 and ω n are fixed, we talk about natural splines. In this case, ω in = (ω 1,..., ω n ) are solution of Hω in = 6d, where d = ( 1 0,..., n 1 n 2 ), with i = (ω i+1 ω i )/h i, H = 2 (h 0 + h 1 ) h 1 0 0 0 h 1 2 (h 1 + h 2 ) h 2 0 0......... 0 0 0 2 (h n 3 + h n 2 ) h n 2 0 0 0 h n 2 2 (h n 2 + h n 1 ) (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 33 / 34
Error Piecewise polynomial interpolation Error Hypothesis: f : [a, b] R is four times continuously differentiable in [a, b]. x 0, x 1,..., x n [a, b]. Error estimation: Let h = max i=0,...,n h i. Then, for all x [a, b] we have f (x) f (x) c h p+1 max f (p+1) (y), y [a,b] where c is a constant independent of f, x and h. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 34 / 34