<<

Chapter 4

Polynomial

In this chapter, we consider the important problem of approximating a f(x), whose values at a set of distinct points x0, x1, x2, . . . , xn are known, by a P (x) such that P (xi) = f(xi), i = 0, 1, 2, . . . , n. Such a polynomial is known as an interpolating polynomial. An approximation of this function is desirable if f(x) is difficult to evaluate or manipulate like the error function

x 2 t2 erf(x) = e− dt, √π !0 for example. Note that are easily evaluated, differentiated or integrated. Another purpose of such an approximation is that a very long table of function values f(xi) may be replaced by a short table and a compact interpolation subroutine.

4.1

Let f(x) be given at two distinct points xi and xi+1. Now, given f(xi) = fi and f(xi+1) = fi+1, we wish to determine the interpolating polynomial P1(x) of degree 1 (i.e., a straight line) that approximates f(x) on the interval [xi, xi+1]. Let P1(x) = αx + β. From the conditions P1(xi) = fi and P1(xi+1) = fi+1 we obtain the two equations

αxi + β = fi

αxi+1 + β = fi+1, i.e., x 1 α f i = i . x 1 β f " i+1 # " # " i+1 # This system is clearly nonsingular since x = x , and it has a unique solution: i " i+1 f f f x f x α = i+1 − i β = i i+1 − i+1 i . x x x x i+1 − i i+1 − i Therefore, f f P (x) = f + i+1 − i (x x ). 1 i x x − i " i+1 − i #

Example 4.1 Using linear interpolation, approximate sin 6.5◦ from a table which gives

(a) sin 6◦ = 0.10453; sin 7◦ = 0.12187.

(b) sin 0◦ = 0; sin 10◦ = 0.17365.

56 Chap. 4. CS414 Class Notes 57

Solution 0.12187 0.10453 (a) P1(6.5) = 0.10453 + 7−6 0.11320. − ≈ $ % (b) P (6.5) = 0 + 0.17365 (6.5) 0.11287. 1 10 ≈ The first answer is correct to 5 decimals whereas the second answer is correct only to 2 decimals! 2

Conclusion: Linear interpolation is suitable only over small intervals.

4.2 Polynomial Interpolation

Since linear interpolation is not adequate unless the given points are closely spaced, we consider higher order interpolating polynomials. Let f(x) be given at the selected sample of (n + 1) points: x < x < < x , 0 1 · · · n i.e., we have (n + 1) pairs (xi, fi), i = 0, 1, 2, . . . , n. The objective now is to find the lowest degree polynomial that passes through this selected sample of points. f(x)

Pn(x)

f(x)

x0 x1 x2 x3 ... xn

Figure 4.1: Interpolating the function f(x) by a polynomial of degree n, Pn(x).

Consider the nth degree polynomial

P (x) = a + a x + a x2 + + a xn. n 0 1 2 · · · n We wish to determine the coefficients aj, j = 0, 1, . . . , n, such that

Pn(xj ) = f(xj), j = 0, 1, 2, . . . , n. These (n + 1) conditions yield the linear system

2 n a0 + a1x0 + a2x0 + + anx0 = f0 2 · · · n a0 + a1x1 + a2x1 + + anx1 = f1 2 · · · n a0 + a1x2 + a2x2 + + anx2 = f2 . . . · · · ...... a + a x + a x2 + + a xn = f 0 1 n 2 n · · · n n n or V a = f, where aT = (a , a , . . . , a ), f T = (f , f , . . . , f ), and V is an (n + 1) (n + 1) matrix given by 0 1 n 0 1 n × 2 n 1 x0 x0 x0 2 · · · n 1 x1 x1 x1  1 x x2 · · · xn  V = 2 2 · · · 2 .  . . . .   . . . .     1 x x2 xn   n n · · · n    Chap. 4. Polynomial Interpolation CS414 Class Notes 58

The matrix V is known as the . It is nonsingular with nonzero

n 1 − det(V ) = (x x ). i − j j=0 ,i>j

For n = 3, for example,

det(V ) = (x x )(x x )(x x )(x x )(x x )(x x ) 1 − 0 2 − 0 3 − 0 2 − 1 3 − 1 3 − 2 1 Hence a can be uniquely determined as a = V − f. Another proof of the uniqueness of the interpolating polynomial can be given as follows.

Theorem 4.1 Uniqueness of interpolating polynomial. Given a set of points x0 < x1 < < xn, there exists only one polynomial that interpolates a function at those points. · · · Proof Let P (x) and Q(x) be two interpolating polynomials of degree at most n, for the same set of points x0 < x1 < < xn. Also, let · · · R(x) = P (x) Q(x). − Then R(x) is also a polynomial of degree at most n. Since

P (xi) = Q(xi) = fi, we have, R(xi) = 0 for i = 0, 1, . . . , n. In other words R(x) has (n + 1) roots. From the Fundamental Theorem of Algebra however, R(x) cannot have more than n roots. A contradiction! Thus, R(x) 0, and P (x) Q(x). ≡ 2 ≡ Example 4.2 Given the following table for the function f(x), obtain the lowest degree interpolating polynomial.

xk –1 2 4 fk –1 –4 4

Solution Since the number of nodes = 3 = n + 1, therefore n = 2. Let

2 P2(x) = a0 + a1x + a2x , that satisfies the following conditions at the points x0 = 1; x1 = 2, x2 = 4:

P ( 1) = 1; P (2) = 4; P (4) = 4. − − −

1 1 1 a0 1 1 −2 4 a = −4 .    1   −  1 4 16 a2 4 Using with partial pivoting,we compute the solution

a 4 0 − a = a = 2 ,  1   −  a2 1     Therefore, the interpolating polynomial is given by

P (x) = 4 2x + x2. 2 − − We can use this approximation to estimate the root of f(x) that lies in the interval [2, 4]:

2 + √4 + 16 γ = = 1 + √5. 2 Chap. 4. Polynomial Interpolation CS414 Class Notes 59

2 An important remark is in order. One, in general, should not determine the interpolating polynomial by solving the Vandermonde linear system. These systems are surprisingly ill-conditioned for n no larger than 10. For example, for 0 < x0 < x1 < < xn = 1 uniformly distributed in [0,1], large n yields a Vandermonde matrix with almost linearly dependen· · ·t columns, and the Vandermonde system becomes almost singular. A more satisfactory form of the interpolating polynomial is due to Lagrange,

P (x) = f(x )L (x) + f(x )L (x) + + f(x )L (x). n 0 0 1 1 · · · n n

Here Lj(x), for 0 j n, are polynomials of degree n called fundamental polynomials or Lagrange polyno- mials. From the (n≤+ 1)≤ conditions

P (x ) = f(x ), 0 i n, n i i ≤ ≤ we see that 0 i = j L (x ) = j i 1 i =" j - The above property is certainly satisfied if we choose

(x x0)(x x1) (x xj 1)(x xj+1) (x xn) Lj(x) = − − · · · − − − · · · − (xj x0)(xj x1) (xj xj 1)(xj xj+1) (xj xn) − − · · · − − − · · · − i.e., n (x xi) Lj(x) = − . (xj xi) i=0 − ,i=j # The fact that Lj(x) is unique follows from the uniqueness of the interpolation polynomial. Recall that there is one and only one polynomial of degree n or less that interpolates f(x) at the (n + 1) nodes x < x < < x . Thus, for this given set of nodes, both forms yield the same polynomial! 0 1 · · · n Vandermonde Approach. n i Pn(x) = aix V a = f i=0 . Operation count: It can be shown that the Vandermonde system V a = f can be solved in O(n2) arithmetic operations rather than O(n3) operations because of the special form of the Vandermonde matrix.

Lagrange Approach.

n (x x ) n n − i n  i=0  Pn(x) = fjLj(x) = gj , = gj νj (x x ) · j=0 j=0  j  j=0 . .  −  .       where f(xj) gj = n . (x x ) j − i i=0 ,i=j # Operation count: The Lagrange form avoids solving the ill-conditioned Vandermonde linear, systems, and evaluates the polynomials 1 n ν = (x x ) j (x x ) − i j i=0 − , Chap. 4. Polynomial Interpolation CS414 Class Notes 60 for a given x. The cost of these operations for each j = 0, 1, 2, . . . , n are:

n u(x) = (x x ) (2n + 1) ops − i → i=0 u,(x) νj = (n + 1) ops (x xj ) → − f g = j 2n(n + 1) ops (evaluated once only) j n → (x x ) j − i i=0 i,=j # Now, the cost for evaluating the polynomial

n P (x) = g ν (2n + 1) ops n j j → j=0 .

Therefore, the total number of operations are 5n + 3 once gj’s are evaluated.

4.3 Error in Polynomial Interpolation

Let en(x) be the error in polynomial interpolation given by

e (x) = f(x) P (x). n − n

Since Pn(xi) = f(xi), we have e (x ) = 0 x < x < < x . n i 0 1 · · · n In other words en(x) is a function with at least (n + 1) roots. Hence, we can write f(x)

Pn(x)

f(x)

x0 x1 x2 x3 ... xn

Figure 4.2: Computing the error in polynomial interpolation.

e (x) = (x x )(x x ) (x x )h(x) = π(x) h(x) n − 0 − 1 · · · − n · where h(x) is a function of x. Let us define

g(z) = e (z) π(z)h(x) n − where x = x , for 0 j n. Thus, " j ≤ ≤ g(x) = 0, g(x ) = 0, 0 j n, j ≤ ≤ Chap. 4. Polynomial Interpolation CS414 Class Notes 61 i.e., g(z) has (n+2) distinct roots. Assuming that f(x) has at least n+1 continuous , the (n+1)th of g(z), i.e., d(n+1) g(z) g(n+1)(z) dz(n+1) ≡ has at least one root x∗. Hence,

x0 x2 x4 g(z) x1 x3

g!(z)

g!!(z)

g!!!(z)

iv g (z) x*

Figure 4.3: The (n + 1)th derivative of a function g(x) with n + 2 roots (n = 3) must have at least one root.

g(n+1)(z) = e(n+1)(z) h(x)π(n+1)(z), n − where π(n+1)(z) = (n + 1)! Moreover, since e (z) = f(z) P (z), therefore, n − n e(n+1)(z) = f (n+1)(z) P (n+1)(z) n − n (n+1) in which the last term Pn (z) is zero. Using the above equations, we have

g(n+1)(z) = f (n+1)(z) h(x)(n + 1)! − (n+1) We had shown earlier that g (z) has at least one root x∗ in the interval containing x and xj , j = 0, . . . , n. (n+1) This implies that g (x∗) = 0, and therefore,

f (n+1)(x ) h(x) = ∗ (n + 1)!

Consequently, we have the following representation of the error in polynomial interpolation

f (n+1)(x ) e (x) = f(x) P (x) = (x x )(x x ) (x x ) ∗ n − n − 0 − 1 · · · − n (n + 1)! where x∗ is in the interval containing x and xj, j = 0, . . . , n. Chap. 4. Polynomial Interpolation CS414 Class Notes 62

Example 4.3 Obtain the Lagrange interpolating polynomial from the data

π 1 π √3 π sin 0 = 0, sin = , sin = , sin = 1, 6 2 3 2 2 π π and use it to evaluate sin 12 and sin 4 Solution The interpolating polynomial is given by

P3(x) = f0L0(x) + f1L1(x) + f2L2(x) + f3L3(x) 1 √3 = L (x) + L (x) + L (x) 2 1 2 2 3 π At x = 12 , π π π π π π 12 0 12 3 12 2 15 L1 = − − − = 12 π 0 π π π π 16 $ % 5 6 − 6 5 6 − 3 6 56 − 2 6 π π π π π π 125 0 6 512 6 6 512 2 6 5 L2 = − − − = 12 π 0 π π π π −16 $ % 5 3 − 6 5 3 − 6 6 53 − 2 6 π π π π π π 512 06 5 12 6 5 12 63 1 L3 = − − − = 12 π 0 π π π π 16 $ % 5 2 − 6 5 2 − 6 6 52 − 3 6 Therefore, 5 6 5 6 5 6 π P = 0.26062. 3 12 π $ % Since sin 12 = 0.25882, we have error = 0.0018. π | | At x = 4 , π π π L = 0.5625, L = 0.5625, L = 0.0625. 1 4 2 4 3 4 Therefore $ % $ % $ % π P = 0.70589. 3 4 π $ % Since sin 4 = 0.70711, we have error = 0.00122. | | π Let us estimate the error en(x) at x = 4 :

π π π π π π π π f (4)(x ) e = 0 ∗ 3 4 4 − 4 − 6 4 − 3 4 − 2 4! $ % $ % $ % $ % $ % Since f(x) = sin x; f (4)(x) = sin x, and thus,

f (4)(x) 1. | | ≤ Therefore, π π π π π 1 3 e = 1.76 10− . 3 4 ≤ 4 · 12 · 12 · 4 · 4! × 7 $ %7 Similarly, e (x) at x = π is bounded7 b7y n 12 7 7

π π π π 5π 1 3 e = 2.94 10− . 3 12 ≤ 12 · 12 · 4 · 12 · 4! × 7 $ %7 7 7 2 7 7 Chap. 4. Polynomial Interpolation CS414 Class Notes 63

4.4 Runge’s Function and Equidistant Interpolation

In this section, we discuss a problem that arises when one constructs a global interpolation polynomial on a fairly large number of equally spaced nodes. Consider the problem of approximating the following function on the interval [ 1, 1] − 1 f(x) = 1 + 25x2 using the interpolation polynomial Pn(x) on the equidistant nodes 2j x = 1 + , j = 0, 1, . . . , n. j − n Figure ?? shows how the polynomials P (x), P (x), P (x), and P (x) approximate f(x) on [ 1, 1]. We see 4 8 12 16 −

n = 4 n = 8 1 1

0.8 0.8

0.6

0.6 0.4

0.2 0.4

0

0.2 −0.2

−0.4 0

−0.6

−0.2 −0.8

−0.4 −1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

n = 12 n = 16 1 2

0.5 0

0 −2

−0.5

−4 −1

−1.5 −6

−2 −8

−2.5 −10 −3

−12 −3.5

−4 −14 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

Figure 4.4: Equal spaced interpolation of Runge’s function. that as n increases, the interpolation error in the central portion of the interval diminishes, while increasing rapidly near the ends of the interval. Such behavior is typical in equidistant interpolation with polynomials of high degree. This is known as Runge’s phenomenon, and the function f(x) is known as Runge’s function. If the (n + 1) interpolation nodes are not chosen equally spaced, but rather placed near the ends of the interval at the roots of the so called Chebyshev polynomial of degree (n + 1), the problem with Runge’s function disappears (see Fig. ??). The roots of the Chebyshev polynomial of degree (n + 1) are given by

k + 1 x = cos 2 π , k = 0, 1, . . . , n. k − n + 1 " # Chap. 4. Polynomial Interpolation CS414 Class Notes 64

n = 4 n = 8 1.2 1.2

1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0

−0.2 −0.2 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 n = 12 n = 16 1 1

0.9 0.9

0.8 0.8

0.7 0.7

0.6 0.6

0.5 0.5

0.4 0.4

0.3 0.3

0.2 0.2

0.1 0.1

0 0 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

Figure 4.5: Chebyshev interpolation of Runge’s function.

4.5 The Newton Representation

The Lagrange form for the interpolating polynomial is not suitable for practical computations. The Newton form of the interpolating polynomial proves to be much more convenient. Let us obtain a different form of the lowest degree interpolating polynomial at the distinct interpolation nodes given by

x0 < x1 < x2 < < xn 1 · · · − where f(x ) = f , j = 0, 1, . . . , n 1. Let the interpolating polynomial be j j − P (x) = c + c (x x ) + c (x x )(x x ) + n 0 1 − 0 2 − 0 − 1 c (x x )(x x )(x x ) + + 3 − 0 − 1 − 2 · · · cn(x x0)(x x1) (x xn 1). − − · · · − − Using the n conditions P (x ) = f , j = 0, 1, . . . , n 1, n j j − we can construct a system of equations in the unknown cj. For n = 3, for example, this yields a system of linear equations of the form Lc = f, where L is given by, 1 0 0 0 1 (x x ) 0 0 L = 1 0 .  1 (x − x ) (x x )(x x ) 0  2 − 0 2 − 0 2 − 1  1 (x3 x0) (x3 x0)(x3 x1) (x3 x0)(x3 x1)(x3 x2)   − − − − − −    Chap. 4. Polynomial Interpolation CS414 Class Notes 65

Note that the evaluation of L for n nodes requires O(n2) arithmetic operations. Assuming that the nodes are equidistant, i.e., x x = h, the system Lc = f is given by i+1 − i

1 0 0 0 c0 f0 1 h 0 0 c1 f1  2    =   . 1 2h 2h 0 c2 f2 2 3  1 3h 6h 6h   c3   f3              Step 1.

c0 = f0 f (1) = (f f )/h 1 1 − 0 f (1) = (f f )/2h 2 2 − 0 f (1) = (f f )/3h. 3 3 − 0 Then we have the system order 3:

(1) 1 0 0 c1 f1 1 h 0 c (1) 2 =  f2  .  2    (1) 1 2h 2h c3 f  3        Step 2.

(1) c1 = f1 f (2) = (f (1) f (1))/h 2 2 − 1 f (2) = (f (1) f (1))/2h 3 3 − 1 Then we get the system of order 2:

(2) 1 0 c2 f2 = (2) . 1 h c3 f " # " # 8 3 9

Step 3.

(2) c2 = f2 f (3) = (f (2) f (2))/h 3 3 − 2

Step 4. (3) c3 = f3

MATLAB function c = InterpolateNewton(x,f) % x: interpolating nodes % f: function values at x % c: coef. of Newton’s form of interpolating polynomial n = length(x); for k = 1:n-1 f(k+1:n) = (f(k+1:n)-f(k))./(x(k+1:n)-x(k)); end c = y; Chap. 4. Polynomial Interpolation CS414 Class Notes 66

4.6 Interpolation

Draftsmen used to draw smooth curves through data points by using splines. These are thin flexible strips of plastic or wood which were laid on paper and held with weights so as to pass through the required data points (or nodes). The weights are constructed in such a way that the spline is free to slip. As a result, the flexible spline straightens out as much as it can subject to passing over these points. Theory of elasticity suggests ∞ ∞ ∞ ∞

x1 x2 x3 x4

Figure 4.6: . that this mechanical spline is given by a cubic polynomial (degree 3 polynomial) in each subinterval [xi, xi+1], i = 1, 2, . . . , n 1, with adjacent cubics joint continuously with continuous first and second derivatives. Let − 2 3 s(x) = a0 + a1x + a2x + a3x , be the form of each cubic spline. Since we have (n 1) subintervals [x , x ], 1 i n 1, then we have − i i+1 ≤ ≤ − 4(n 1) unknowns; (4 parameters for each subinterval: x0,a1,a2, and a3). Suppose si(x) denotes the cubic spline− the ith interval. The conditions to be satisfied by these cubic splines are: 1. Continuity at each interior node:

si 1(xi) = si(xi), i = 2, 3, . . . , n 1. − −

2. Continuity of first derivative at each interior node:

si% 1(xi) = si% (xi), i = 2, 3, . . . , n 1. − −

3. Continuity of second derivative at each interior node:

si%% 1(xi) = si%%(xi), i = 2, 3, . . . , n 1. − −

4. Interpolation of function at each node:

s(xj ) = f(xj), j = 1, 2, . . . , n.

These provide 3(n 2) + n = 4n 6 conditions; however, in order to completely specify the cubic splines, − − we still need 2 additional conditions. These are conditions specified at the end points x1 and xn. For the “mechanical” or “natural” spline we set

s%%(x1) = s%%(xn) = 0. Chap. 4. Polynomial Interpolation CS414 Class Notes 67

Let the nodes xj, j = 1, 2, . . . , n, be equidistant, i.e.,

x x = h j+1 − j and let, 1 σ = s%%(x ) j 6 j i.e., si%% 1(xi) = si%%(xi) = 6σi − for all i satisfying condition ??. Since

2 3 s(x) = a0 + a1x + a2x + a3x , we have

2 s%(x) = a1 + 2a2x + 3a3x

s%%(x) = 2a2 + 6a3x, i.e., s%%(x) is a straight line. Thus, si%%(x) takes the following form (see Fig. ??):

xi+1 x (x xi) s%%(x) = − (6σ ) + − (6σ ), i = 1, 2, . . . , n 1. i h i h i+1 −

6σi+1 s''i(x) 6σi

x i x xi+1

h

Figure 4.7: Computing spline within an interval.

Integrating once, we have

2 2 6σi (xi+1 x) 6σi+1 (x xi) s% (x) = − − + − + β i h · 2 h · 2 1 and integrating once more, we have σ σ s (x) = i (x x)3 + i+1 (x x )3 + β x + β i h i+1 − h − i 1 2 where β1 and β2 are constants. Clearly we can write si(x) as follows, σ σ s (x) = i (x x)3 + i+1 (x x )3 + γ (x x ) + γ (x x). i h i+1 − h − i 1 − i 2 i+1 − Using conditions ?? and ??,

2 si(xi) = fi fi = σih + γ2h s (x ) = f ⇒ f = σ h2 + γ h i i+1 i+1 ⇒ i+1 i+1 1 Chap. 4. Polynomial Interpolation CS414 Class Notes 68

Therefore, f γ = i+1 σ h, 1 h − i+1 f γ = i σ h, 2 h − i and σ σ f s (x) = i (x x)3 + i+1 (x x )3 + i+1 σ h (x x ) i h i+1 − h − i h − i+1 − i " # f + i σ h (x x) h − i i+1 − " # 3σi 2 3σi+1 2 fi+1 s% (x) = − (x x) + (x x ) + σ h i h i+1 − h − i h − i+1 " # f i σ h . − h − i " # Using condition ??, i.e., si% (xi) = si% 1(xi), we get − fi+1 fi fi fi 1 3σih + − h(σi+1 σi) = 3σih + − − h(σi σi 1) − h − − h − − − " # " # or ∆i ∆i 1 σi+1 + 4σi + σi 1 = − − i = 2, 3, . . . , n 1, − h − where f f ∆ = i+1 − i . i h Using the additional conditions σ1 = σn = 0, we have 4σ + σ = (∆ ∆ )/h 2 3 2 − 1 σi 1 + 4σi + σi+1 = (∆i ∆i 1)/h i = 3, 4, . . . , n 2 − − − − σn 2 + 4σn 1 = (∆n 1 ∆n 2)/h. − − − − − Now, assigning ∆j ∆j 1 fj+1 2fj + fj 1 g = − − = − − , j h h2 we have the linear system

4 1 σ2 g2 1 4 1 σ3 g3  . . .   .   .  ...... = . .        1 4 1   σn 2   gn 2     −   −   1 4   σn 1   gn 1     −   −        The tridiagonal system above, denoted by [1, 4, 1], may be factored as follows:

1 µ1 1 µ2 1 λ1 1    λ 1  .. 2 µ3 .  . .     .. ..   ..     . 1   λ 1    n 3  µn 2   −   −      The following conditions must be satisfied:

µ1 = 4, λiµi = 1, λi + µi+1 = 4 The algorithm for computing L$ and U is given below. Chap. 4. Polynomial Interpolation CS414 Class Notes 69

Triangular decomposition for system [1,4,1]

µ1 = 4 for i = 1, 2, . . . , n 3 1 − λi = µi− µi+1 = 4 λi end; −

Once L$ and U are obtained, the above system is solved as shown in Chapter ??.