Pt4Part 4 Chapter 15

PlPolynomi ilItal Interpol ltiation

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University Revised by Prof. Jang, CAU

All images copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Objectives

• Recogni zi ng that eval uati ng pol ynomi al coeffi ci ent s with simultaneous equations is an ill-conditioned problem. • Knowinggpy how to evaluate coefficients and interpolate with MATLAB’s polyfit and polyval functions. • Knowing how to perform an interpolation with Newton’s polynomial. • Knowing how to perform an interpolation with a Lagrange polynomial. • Knowing how to solve an inverse interpolation problem by recasting it as a roots problem. • Appreciating the dangers of extrapolation . • Recognizing that higher-order can manifest large oscillations. Introduction • Suppose that you desired the density at a temperature not included in the table below. In such a case, you would have to ilThiinterpolate. That is, you would ldhihl have to estimate the values at the desired temperature based on the densities that include it. • The simpppplest approach is to determine the eq uation for the straight line connecting the two adjacent values and use this equation to estimate the density at the desired intermediate temperature. • Although such linear interpolation is perfectly adequate in many cases, error can be introduced when the data exhibit siifiignificant curvature. -> hihigh her ord er i nterpol ati on i s requi red .

T (C)  (kg/m3)  (Ns/m2) v (m2/s) 200 0.746 2.57  10-5 3.45  10-5 250 0.675 2.75  10-5 4.08  10-5 300 0.616 2.93  10-5 4.75 10-5 400 0.525 3.25  10-5 6.20  10-5 500 0.457 3.55  10-5 7.77  10-5 • You will frequently have occasions to estimate intermediate values between precise data points. • The function you use to interpolate must pass through the actual data points - this makes interppgolation more restrictive than fitting. • The most common method for this purpose is ppyolynomial interp olation,,( where an (n-1)th order polynomial is solved that passes through n data points: 2 n1 f (x)  a1  a2 x  a3x  an x MATLAB version : f (x)  p xn1  p xn2   p x  p  1 2  n1 n Determining Coefficients

• A straightforward way for computing the coefficients is based on the fact that n data points are required to determine the n coefficients. Hence, the polynomial coefficients can be found exactly using linear algebra.

• MATLAB’s built in polyfit and polyval commands can also be used - all that is required is making sure the order of the fit for n data points is n-1. Example 15.1 ( 1/2)

Q. Determine the coefficients of the parabola that passes through the last three density values from Table 15.1 2 f (x)  p1 x  p2 x  p3 Solution)

2 x1  300 f (x1)  0.616 0.616  p1(300)  p2 (300)  p3 2 x2  400 f (x2 )  0.525 0.525  p1(400)  p2 (400)  p3 x  500 f (x )  0.457 2 3 3 0.457  p1(500)  p2 (500)  p3

 90,000 300 1 p1  0.616     160,000 400 1 p  0.55525   2         250,000 500 1 p3  0.457 Example 15.1 (2/2)

>> format long >> A = [90000 300 1; 160000 400 1; 250000 500 1]; >> b = [0.616 0.525 0.457]' ; >> p = A\b p = 0.00000115000000 -0.00171500000000 1.02700000000000

f (x)  0.00000115x 2  0.001715x 1.027

This polynomial can provide a means to determine intermediate points. For examppyle, the value of density at 350C can be found as f (350)  0.00000115(350)2  0.001715(350) 1.027  0.567625 Polynomial Interpolation Problems

n1 n2 f (x)  p1x  p2 x  pn1x  pn • One problem that can occur with solving for the coefficients of a polynomial is that the system to be inverted is in the form:  n1 n2    x x x 1  p  fx 1 1 1  1 1   xn1 xn2 x 1 p  f x  2 2  2  2    2          n1 n2 p    xn1 xn1  xn1 1 n1 fxn1 n1 n2       pn  xn xn  xn 1  f xn    • Matrices such as that on the left are known as Vandermonde matrices,,y and they are very ill-conditioned - meaning their solutions are very sensitive to round-off errors. • The issue can be minimized by scaling and shifting the data . Newton Interpolating Polynomials

• Another way to express a polynomial interpolation is to use Newton’s itint ltierpo l illating polynomial. • The differences between a simple polynomial and Newton’s interpolating polynomial for first and second order interpolations are:

Order Simple Newton

1st (linear) f1(x)  a1  a2 x f1(x)  b1  b2 (x  x1) 2 2nd (dti(quadratic) f2 (x)  a1  a2 x  a3x f2 (x)  b1  b2 (x  x1)  b3 (x  x1)(x  x2 ) Newton Interpolating Polynomials (Liiinear interpo lilation) • The first -order Newton interpolating polynomial may be obtained from linear interpolation and similar triangles, as shown. • The resulting formula based on

kitknown points x1 and x2 andthd the values of the dependent function at those points is :

fx 2  fx 1 f1x  fx1  x  x1 x2  x1

• In general, the smaller the interval Graphical representation of between the data points, the linear interpolation better the approximation. Newton Interpolating Polynomials (QdiiQuadratic interpo lilation) • The second -order Newton interpolating polynomial introduces some curvature to the line connecting the points, but still goes through the first two points.

• The resulting formula based on known points x1, x2, and x3 and the values of the dependent function at those poitints i s:

f2 (x)  b1  b2 (x  x1)  b3 (x  x1)(x  x2 )

fx  fx fx  fx  3 2  2 1 f x2  f x1 x3  x2 x2  x1 f2 x  fx1  x  x1  x  x1 x  x2 x2  x1 x3  x1 Example 15.2 Q. Estimate the natural of 2 using linear interpolation. First compute it by interpolating between ln 1  0 and ln 6  1 .791759 . Then repeat the procedure using smaller interval from ln1 and ln4 (1.386294). Note: ln 2  0.6931472

Solution) Using x1 = 1 and x2 = 6 1.791759  0 f (2)  0  (2 1)  0.3583519 1 6 1

Using x1 = 1 and x2 = 4, 1.386294  0 f (2)  0  (2 1)  0.4620981 1 4 1

The value εt are 48.3% and 33.3% respectively. Example 15.3 Q. Use a second-order to estimate ln2 with the same three points used in Ex . 15.2 . Solution) x1 1 f (x1)  0

x2  4 f (x2 ) 1.386294

x3  6 f (x3 ) 1.791759 1.386294  0 b1  0 b   0.4620981 2 4 1 1.791759 1.386294  0.4620981 b  6  4  0.0518731 3 6 1

f2 (x)  0  0.4620981(x 1)  0.0518731(x 1)(x  4)

f 2 (2)  0.5658444

The value εt are 18.4% . -> The quadratic formula improves the error. Newton Interpolating Polillynomials (Generalfl form) • In general, an ( n-1)th Newton interpolating polynomial can be fitted to n data points. • The general formula is: f x  b  b x  x   b x  x x  x  x  x  n1 1 2 1  n 1 2  n1 where b1  fx 1

b2  fx2, x1

b3  fx 3, x2, x1  b  f x , x , , x , x  n n n1  2 1 and the f[…] represent divided differences. Divided Differences • Divided difference are calculated as follows: fx i  fx j  f xi , x j  xi  x j

fxi , x j  fxj , xk fxi , x j , xk  xi  xk fx, x , , x  fx, x , , x fx, x , , x , x  n n1  2 n1 n2  1 n n1  2 1 x  x  n 1 • High er-orddiiddder divided differences are ca lcu la te d us ing divided difference of lower-order terms:

Graphical representation of the recursive nature of finite divided differences Example 15.4 (1/2)

Q. Estimate ln2 using a third-order Newton’s interpolating

polilUthfllifitlynomial. Use the following four points x1 = 1, x2 = 4, x3 = 6, and x4 = 5, [f(x4) = 1.609438]. Solution)

f3 (x)  b1  b2 (x  x1)  b3 (x  x1)(x  x2 )  b4 (x  x1)(x  x2 )(x  x3 ) 1.38629 4  0 1.791759 1.386294 f [x2 , x1 ]   0.4620981 f [x , x ]   0.2027326 4 1 3 2 6  4 1.609438 1.791759 f [x , x ]   0.1823216 4 3 5  6

0.1823216  0.2027326 f [x , x , x ]   0.02041100 4 3 2 5  4 0.2027326  0.4620981 f [x , x , x ]   0.05187311 3 2 1 6 1 0.02041100  (0.05187311) f [x , x , x , x ]   0.007865529 4 3 2 1 5 1 Example 15.4 (2/2)

f3 (x)  b1  b2 (x  x1)  b3 (x  x1)(x  x2 )  b4 (x  x1)(x  x2 )(x  x3 )

xi f(xi) First Second Third 1 0 0.4620981 -0.05187311 0.007865529 4 1. 386294 0. 2027326 -0. 02041100 6 1.791759 0.1823216 5 1.609438

f3 (x)  0  0.4620981(x 1)  0.05187311(x 1)(x  4) f3 (2)  0.6287686  0.007865529(x 1)(x  4)(x  6) MATLAB Implementation Lagrange Interpolating Polillynomials • Another method that uses shifted value to express an interpolating polynomial is the Lagrange interpolating interpolating polynomial polynomial. • The differences between a simply polynomial and Lagrange interpolating polynomials for first and second order polynomials are: Order Simple Lagrange

1st f1(x)  a1  a2 xf1(x)  L1 fx1  L2 fx2 2 2nd f2 (x)  a1  a2 x  a3x f2 (x)  L1 f x1 L2 f x2  L3 f x3

where the Li are weighting coefficients that are functions of x. Lagrange Interpolating Polynomials (Linear) • The first-order Lagggrange interpolating polynomial may be obtained from a weighted combination of two linear interpolations, as shown. • The resulting formula based on

known points x1 and x2 and the values of the dependent function at those points is:

f1(x)  L1 fx1  L2 fx 2 

x  x2 x  x1 L1  , L2  x1  x2 x2  x1

x  x2 x  x1 f1(x)  f x1 f x2  x1  x2 x2  x1 Lagrange Interpolating Polillynomials (cont)

(x  x2 )(x  x3 ) (x  x1 )(x  x3 ) (x  x1 )(x  x2 ) f 2 (x)  f (x1 )  f (x2 )  f (x3 ) (x1  x2 )(x1  x3 ) (x2  x1 )(x2  x3 ) (x3  x1 )(x3  x2 ) • For quadratic formula, three parabolas would be used with each one passi ng th rough one of th e poi nt s and equaling zero at the other two. Their sum would represent the unique parabola that connects the three points. • In gen er al , th e L agr an ge pol yno mia l inte rpo lat io n fo r n points is: n fn1xi  Li x fxi i1 n x  x where L is given by: j i Li x   j1 xi  x j ji MATLAB Implementation Inverse Interpolation • Interpolation general means finding some value f(x)f) for some x thibhat is between gi ven independent data points. • Sometimes, it will be useful to find the x for which f(x) has a certain value - this problem is called inverse interpolation. • Rather than findinggp an interpolation of x as a function of f(x), it may be useful to find an equation for f(x) as a function of x using interpolation and then solve the corresponding roots problem:

f(x)-fdesired=0 for x. Extrapolation • Two issues related to polynomial interpolation. – extrapolation and oscillation.

• Extrapolation is the process of estimating a value of f(x) that lies outside the range of the

known base points x1, x2, …, xn.

• Extrapolation represents a step ihinto the unk(known (curve beyond the known region), and extreme care should be exercised when extrapolating! Example 15.6 ((/)1/2)

Q. This table shows the population in millions of US from 1920 to 2000.

Year 1920 1930 1940 1950 1960 1970 1980 1990 2000

Population 106.46 123.08 132.12 152.27 180.67 205.05 227.23 249.46 281.42

Fit a seventh-order polynomial to the first 8 points (1920 to 1990). Use it to comppppute the population in 2000 by extrapolation and compare your prediction with the actual result. Example 15.6 (2/2)

>> ts = (t-1955)/35; >> p = polyfit(ts, pop, 7) p = Columns 1 through 8 -61.9393 -32.9356 147.0977 38.1565 -115.3518 6.4156 101.6934 166.3235 >> p2000 = polyval(p, (2000-1955)/35) p2000 = 175.0800 >> tt=linspace(1920,2000); >> pp = poll((lyval(p, (tt-1955)/35); >> plot(t,pop,'o',tt,pp)

The polynomial seems to fit the data nicely from 1920 to 1990. H owever, the po lynom ia l p lunges to the erroneous prediction in 2000 (outside). [175 vs. 281 (actual)] Oscillations • Although “more is better” in many contexts, it is absolutely not true for ppyolynomial interp olation. • Higher-order polynomials can not only lead to round-off errors due to ill-conditioning, but can also introduce oscillations to an interpolation or fit where they should not be . • In the figures below, the dashed line represents an function, the circles represent samples of the function, and the solid line represents thlflililihe results of a polynomial interpolation:

• IiiIn most engineering contexts, lower-ordlilder polynomials can bdbe used effectively to capture the curvature effects without oscillation.