ECE 3040 Lecture 17: Polynomial Interpolation © Prof
Total Page:16
File Type:pdf, Size:1020Kb
ECE 3040 Lecture 17: Polynomial Interpolation © Prof. Mohamad Hassoun This lecture covers the following topics: Introduction Interpolation using a single polynomial Newton’s interpolation polynomials Matlab built-in polynomial interpolation: polyfit The curse of high-dimensional polynomials Cubic spline interpolation Matlab built-in cubic spline interpolation: spline Interpolation using rational functions Introduction Polynomial interpolation is a procedure for modeling a set of precise data points using a polynomial function, 푝(푥), that fits the data exactly (passes through all provided data points). The data points are normally obtained from a complicated mathematical model, 푓(푥), of an engineering or scientific system derived from physical principles. Once an interpolation polynomial is computed, it can be used to replace the complicated mathematical model for the purpose of analysis and design. For instance, the interpolating polynomial can be used to estimate the value of the function at a new point 푥′, as 푓(푥′) ≅ 푝(푥′). The solution for the coefficients of the interpolation polynomial 푝(푥) can be determined by solving an associated system of linear equations, or can be computed using formulas. The data points used for interpolation can also be a set of very accurately measured experimental values. In practice, if the set of data points is large, different polynomials are used to construct a piece-wise interpolation function; this procedure is known as spline interpolation. Rational functions may also be used for interpolation. This lecture will also introduces two built-in Matlab polynomial-based interpolation functions: polyfit and spline. Interpolation Using a Single Polynomial A straight line can be completely defined using two points on the straight line. The slope, 푎1, and 푦-intercept, 푎0, coefficients in the representation 푝(푥) = 푎1푥 + 푎0 are sufficient to define the straight line. The coefficients of the straight line that passes through the points (푥1, 푓(푥1)) and (푥2, 푓(푥2)) are unique and are the solution of the system of two linear equations: 푓(푥1) = 푎1푥1 + 푎0 푓(푥2) = 푎1푥2 + 푎0 or, in matrix form, 푥 1 푎 푓(푥 ) [ 1 ] [ 1] = [ 1 ] 푥2 1 푎0 푓(푥2) Similarly, three points are required to compute the coefficients of a parabola, 2 푝(푥) = 푎2푥 + 푎1푥 + 푎0. For example, the three coefficients of the parabola that passes through the distinct (non-collinear) points (푥1, 푓(푥1)), (푥2, 푓(푥2)) and (푥3, 푓(푥3)) are unique and are the solution of the system of three linear equations: 2 푓(푥1) = 푎2푥1 + 푎1푥1 + 푎0 2 푓(푥2) = 푎2푥2 + 푎1푥2 + 푎0 2 푓(푥3) = 푎2푥3 + 푎1푥3 + 푎0 or, in matrix form, 2 푥1 푥1 1 푎2 푓(푥1) 2 [푥2 푥2 1] [푎1 ] = [푓(푥2)] 2 푎 푥3 푥3 1 0 푓(푥3) In fact, fitting a polynomial to a set of given data points is not new to us. Recall that we had encountered this problem in the context of parabolic interpolation-based optimization in Lecture 14. The following is an example of second-order polynomial interpolation. Example. Use parabolic interpolation to approximate the function 푥2 푓(푥) = − 2sin(푥) 10 from three of its points at 푥1 = 0, 푥2 = 1 and 푥3 = 2. Then, employ the quadratic model to predict the value of 푓(푥), at 푥 = 1.5. Also, compute the true error. Solution. Substituting the values of 푥 in the function 푓(푥) leads to the three interpolation points: (0,0), (1, −1.583) and (2, −1.419). The coefficients of the parabola can then be obtained (employing the above formulation) as the solution of 0 0 1 푎2 0 [1 1 1] [푎1 ] = [−1.583] 4 2 1 푎0 −1.419 The (green) plot of the interpolation parabola 푝(푥) = 0.874푥2 − 2.457푥 and the (blue) plot of 푓(푥), are shown below, within the interval [-0.5 3]. The interpolated value at 푥 = 1.5 is 푝(1.5) = −1.719, giving a true error of 푓(1.5) − 푝(1.5) = −1.770 − (−1.719) = −0.051. The above formulation can be extended to interpolating the 푛 points { (푥1, 푓(푥1)), (푥2, 푓(푥2)), … , (푥푛, 푓(푥푛))} Using the an (푛 − 1)-order polynomial 푛−1 2 푝(푥) = 푎푛−1푥 + ⋯ + 푎2푥 + 푎1푥 + 푎0 which results in the following 푛x푛 system of linear equation: 푎 푥푛−1 … 푥2 푥 1 푛−1 푓(푥 ) 1 1 1 ⋮ 1 푛−1 2 푥 ⋯ 푥 푥2 1 푓(푥2) 2 2 푎2 = [ ] ⋮ ⋮ ⋮ ⋮ ⋮ 푎 ⋮ 푛−1 2 1 [푥 ⋯ 푥 푥 1] 푓(푥푛) 푛 푛 푛 [ 푎0 ] Coefficient matrices of this form are referred to as Vandermonde matrices. Matlab can generate the Vandermonde matrix using the built-in function vander(x), where x is a vector of the 푥푖 data points. The following is an example. The Vandermonde matrix can become ill-conditioned (especially for large 푛), and therefore the solution for the above system formulation is very sensitive to round- off errors. The Matlab instruction cond(A) can be used to return the condition number of a matrix A; a high valued condition number indicates a nearly singular matrix. For example, The next section presents an alternative, numerically robust formulation for the polynomial interpolation problem. Newton’s Interpolation Polynomials There are a variety of alternative forms for expressing an interpolation polynomial beyond the previous formulation. Newton’s interpolation polynomial is among the most popular. The simplest form of interpolation is to start with two points (푥1, 푓(푥1)) and (푥2, 푓(푥2)) and connect them with a straight line. The equation for a straight line that passes through two points can be easily derived and is expressed as 푓(푥2) − 푓(푥1) 푝푁1(푥) = (푥 − 푥1) + 푓(푥1) 푥2 − 푥1 We will refer to 푝푁1(푥) as Newton’s linear-interpolation formula. It allows us to approximate the value of 푓(푥) at some point 푥 inside [푥1 푥2]. The notation 푝푁1 designates that this is a first-order Newton interpolating polynomial. The nominal form for 푝푁1 is given by 푝푁1(푥) = 푏1 + 푏2(푥 − 푥1) where the coefficients are given by 푓(푥2) − 푓(푥1) 푏1 = 푓(푥1), 푏2 = 푥2 − 푥1 It is obvious here that the smaller the interval between the data points, the better the approximation. Alternatively, one can increase the order of the polynomial for increased accuracy if more data points are available. For three data points, the second-order Newton’s interpolation polynomial, 푝푁2(푥), is 푝푁2(푥) = 푏1 + 푏2(푥 − 푥1) + 푏3(푥 − 푥1)(푥 − 푥2) with coefficients 푓(푥 ) − 푓(푥 ) 푓(푥 ) − 푓(푥 ) 3 2 − 2 1 푓(푥2) − 푓(푥1) 푥3 − 푥2 푥2 − 푥1 푏1 = 푓(푥1), 푏2 = , 푏3 = 푥2 − 푥1 푥3 − 푥1 Notice that the coefficients 푏1 and 푏2 are the same in the second and first-order Newton’s polynomials. This means that if two points are given and a first order Newton’s polynomial is fitted to pass through these points and then a third point is added, the polynomial 푝푁2 would then be computed as 푝푁2 = 푝푁1 + 푏3(푥 − 푥1)(푥 − 푥2) and only one new coefficient (푏3) would need to be computed. This observation is also true for higher order Newton polynomials. It is interesting to note that 푏3 is the ′′ finite-difference, second-derivative approximation of 푓 (푥3). The general form for Newton’s (푛 − 1)-order interpolating polynomial on the 푛 data points {(푥1, 푓(푥1)), (푥2, 푓(푥2)), … , (푥푛, 푓(푥푛))}, is given by 푝푁(푛−1)(푥) = 푏1 + 푏2(푥 − 푥1) + 푏3(푥 − 푥1)(푥 − 푥2) + ⋯ + 푏푛(푥 − 푥1)(푥 − 푥2) … (푥 − 푥푛−1) where the 푏푖 constants are evaluated directly (and efficiently) from the data points, using the formulas: 푏1 = 푓(푥1) 푓(푥2) − 푏1 푏2 = 푥2 − 푥1 푓(푥3) − 푓(푥2) − 푏2 푥3 − 푥2 푏3 = 푥3 − 푥1 푓(푥 ) − 푓(푥 ) 푓(푥 ) − 푓(푥 ) 4 3 − 3 2 푥4 − 푥3 푥3 − 푥2 − 푏3 푥4 − 푥2 푏4 = 푥4 − 푥1 and so on. Your turn: Deduce the formula for 푏5. Example. Solve the last example using a second-order Newton’s interpolation polynomial. Again, the three interpolation points are (0,0), (1, −1.583) and (2, −1.419). Solution. The 푏 coefficients are: 푏1 = 푓(푥1) = 푓(0) = 0 푓(푥2) − 푏1 −1.583 − 0 푏2 = = = −1.583 푥2 − 푥1 1 − 0 푓(푥3) − 푓(푥2) −1.419 − (−1.583) − 푏2 − (−1.583) 푥3 − 푥2 2 − 1 푏3 = = = 0.874 푥3 − 푥1 2 − 0 which lead to the interpolation polynomial, 푝푁2(푥) = 0 − 1.583(푥 − 0) + 0.874(푥 − 0)(푥 − 1) = −1.583푥 + 0.874푥(푥 − 1) = 0.874푥2 − 2.457푥 The interpolated value at 푥 = 1.5 is 푝푁2(1.5) = −1.719. These results are consistent with those obtained based on solving the Vandermonde matrix formulation (refer to the previous example). The coefficients of the Newton polynomial can also be computed systematically by solving (using forward substitution utilizing the “\” operator) the system of equations shown below. Your turn: Use it to find {푏1, 푏2, 푏3} for the last example. 1 0 0 0 … 0 푏 푓(푥 ) 1 1 1 (푥 − 푥 ) 0 0 … 0 푏 푓(푥 ) 2 1 2 2 1 (푥 − 푥 ) (푥 − 푥 )(푥 − 푥 ) 0 … 0 푏 푓(푥 ) 3 1 3 1 3 2 3 = 3 . [1 (푥푛 − 푥1) (푥푛 − 푥1)(푥푛 − 푥2) … (푥푛 − 푥1)(푥푛 − 푥2)(… )(푥푛 − 푥푛−1)] [푏푛] [푓(푥푛)] Function newtint is a Matlab implementation of Newton’s interpolation. The following is an application of the function newtint to the solution of the last example: Your turn: Rewrite the above function using the matrix formulation shown at the end of the last page. Email your Matlab function to the grader before the due date for Assignment 11.