CF_INPL

23
zar97/09/03 Numerical Methods For Engineers page 4-21 4.2 Interpolation “ The shortest way is not necessarily the safest way.”- zar97 Interpolation - a process of determining an approximating value between precise data points. Two situations may arise (1) Given some function f(x) and an interval I, find the approximation to f(x) for any x in I. (2) Given a set of n data points (x i , y i ), i =1,2,... n, estimate the value of f(x) at some point x not among the x i ’s. Three methods to be investigated: (a) Newton’s interpolating polynomial, (b) Lagrange interpolating polynomial, and (c) Spline interpolation (fitting data in a piecewise fashion). The most common method used for this purpose is polynomial interpolation. The Polynomial of n th degree has a general form as y a ax ax ax n n 0 1 2 2 ... (4.2-1) For (n+1) data points, there is only one polynomial of order n that may pass through all the points. For example, However, there are several mathematical formats in which the polynomial can be expressed: Newton and Lagrange polynomials are two alternatives suited for computer implementation.

description

fkm uitm

Transcript of CF_INPL

zar97/09/03 Numerical Methods For Engineers page 4-21 4.2 Interpolation

“ The shortest way is not necessarily the safest way.”- zar97 Interpolation - a process of determining an approximating value between precise data points. Two situations may arise (1) Given some function f(x) and an interval I, find the approximation to f(x) for any x in I. (2) Given a set of n data points (xi , yi), i =1,2,... n, estimate the value of f(x) at some point x not among the xi’s. Three methods to be investigated: (a) Newton’s interpolating polynomial, (b) Lagrange interpolating polynomial, and (c) Spline interpolation (fitting data in a piecewise fashion). The most common method used for this purpose is polynomial interpolation. The Polynomial of nth degree has a general form as y a a x a x a xn

n 0 1 22 . . . (4.2-1)

For (n+1) data points, there is only one polynomial of order n that may pass through all the points. For example, However, there are several mathematical formats in which the polynomial can be expressed: Newton and Lagrange polynomials are two alternatives suited for computer implementation.

zar97/09/03 Numerical Methods For Engineers page 4-22 4.2.1 Newton’s Divided-Difference Interpolating Polynomials a. First-order interpolating polynomial : Linear interpolation - connecting two data points with a straight line. Using similar triangle, we can derive

p x f x f x f xx x

x x1 01 0

1 00( ) ( ) ( ) ( )

( )( )

(4.2-2)

Note

fx

is a finite-divided-difference approximation of the first derivative. In general,

the smaller the interval between the data points, the better the approximation.

For f(x) = ln x, estimate ln(2) using linear interpolation in the interval (i) between x= 1 and x=5 and (ii) between x=1 and x= 3. Solution:

x f(x) x f(x) 1 0 1 0 5 1.609638 3 1.098612

Using linear interpolation formula,

p p1 12 0 1609438 05 1

2 1 2 0 1098612 03 1

2 1( ) ( )= 0.402360 ( ) ( )= 0.549306

. .

Percent true error, respectively (i) t = 42.0 % (ii) t = 20.8 %

Example 4.2-1:

zar97/09/03 Numerical Methods For Engineers page 4-22.1

Estimate p(4) using linear interpolation in the interval between x= 2 and x=6

and compute the percent true error knowing the exact value f(4) = 0.6021.

x f(x) 2 0.3010 6 0.7782

Solution:

Estimate p(4) using quadratic for the following set of data also compute the

percent true error.

x f(x) 1 0 2 0.3010 6 0.7782

Solution:

Worksheet 4.2-

Worksheet 4.2-

zar97/09/03 Numerical Methods For Engineers page 4-26.1

Estimate p(4) using cubic interpolation for the following set of data also

compute the percent true error.

x f(x) 1 0 2 0.3010 6 0.7782

10 1 Solution:

Worksheet 4.2-

zar97/09/03 Numerical Methods For Engineers page 4-23 b. Second-order interpolating polynomial : Quadratic interpolation - connecting three data points with a curvature. A second-order polynomial for this purpose can be conveniently expressed as p x b b x x b x x x xo2 0 1 0 2 1( ) ( ) ( )( ) (4.2-3) Note eq.(4.2-3) and (4.2-1) are equivalent. Multiplying the terms in eq.(4.2-3), we can show that

a b b x b x xa b b x b xa b

o

0 1 0 2 0 1

1 1 2 0 2 1

3 2

The coefficients in eq.(4.2-3) can be solved in a simple manner: at x = x0 , b0 = f(x0) (4.2-4) x = x1 ,

bf x f x

x x11 0

1 0

( ) ( ) (4.2-5)

x = x2 ,

bx x

f x f xx x

f x f xx x

2

2 12 1

1 01 0

2 0

( ) ( ) ( ) ( )

(4.2-6)

zar97/09/03 Numerical Methods For Engineers page 4-24

Fit a second-order polynomial to three points used in example 4.2-1 and

use the polynomial to evaluate ln(2). Solution:

x f(x) 1 0 3 1.098612 5 1.609638

Eq.(4.2-4) yields b0 = 0 Eq.(4.2-5) gives

b11 098612 0

3 10549306

. .

and eq.(4.2-6) gives

b2

1609638 1 0986125 3

0549306

5 10 073448

. . ..

The quadratic polynomial is then

p2(x) = 0.549306(x-1) - 0.073448(x-1)(x-3) and at x = 2, p(2) = 0.622754 which has a percent true error of t = 10.2 %. Thus, the quadratic interpolation improves the result as compared to the linear interpolation.

Example 4.2-2:

zar97/09/03 Numerical Methods For Engineers page 4-25 c. General Form of Newton’s Interpolating polynomial : - connecting (n+1) data points with nth-order polynomial. The nth-order polynomial is p x b b x x b x x x x b x x x x x xn n n( ) ( ) ( )( ) . . . ( )( ) . . . ( ) 0 1 0 2 0 1 0 1 1 (4.2-7) The (n+1) data points, x0, x1, x2, ... , xn can be used to determine the coefficients b0, b1, b2, ... , and bn in the following manner b0 = f (x0) (4.2-8) b1 = f [x1, x0] (4.2-9) b2 = f [x2, x1, x0] (4.2-10) . . . bn = f [xn, xn-1, xn-2, ... , x1, x0] (4.2-11) where the bracketed function evaluations are finite divided differences (FDD). For example,

1st FDD: f x xf x f x

x xi ji j

i j[ , ]

( ) ( )

(4.2-12)

2nd FDD: f x x xf x x f x x

x xi j ki j j k

i k[ , , ]

[ , ] [ , ]

(4.2-13)

and nth FDD: f x x x xf x x x f x x x

x xn nn n n n

n[ , , ... , , ]

[ , , ... , ] [ , , ... , ]

1 1 0

1 1 1 2 0

0 (4.2-14)

Then the nth-order Newton’s Interpolating polynomial is

p x f x f x x x x f x x x x x x x

f x x x x x x x x x x x xn

n n n

( ) ( ) [ , ]( ) [ , , ]( )( ) . . .[ , , ... , , ]( )( )( )... ( )

0 1 0 0 2 1 0 0 1

1 1 0 0 1 2 1 (4.2-15)

Remark: 1. Data points may not be equally spaced or x values be in ascending order. 2. Eq.(4.2-12) through (4.2-14) are recursive i.e. higher-order differences are

composed of lower-order differences which is very useful in computer implementation.

zar97/09/03 Numerical Methods For Engineers page 4-26

Determine a third-order Newton’s interpolating polynomial to problem in

example 4.2-2 with x3= 4 and use the result to evaluate ln(2). Solution:

i xi f(xi) 1st FDD 2nd FDD 3rd FDD 0 1 0 1 3 1.098612 2 5 1.609638 3 4 1.386294

With n = 3, p x b b x x b x x x x b x x x x x x3 0 1 0 2 0 1 3 0 1 3( ) ( ) ( )( ) ( )( )( ) The 1st FDD [eq.(4.2-12)]

f x x

f x x

f x x

[ , ] . .

[ , ] . . .

[ , ] . . .

1 0

2 1

3 2

1 098612 03 1

0549306

1609638 1 0986125 3

0 255513

1386294 16096384 5

0 223344

The 2nd FDD [eq.(4.2-12)]

f x x x

f x x x

[ , , ] . . .

[ , , ] . . .

2 1 0

3 2 1

0 255513 05493065 1

0 073448

0 223344 0 2555134 3

0 032169

and eq.(4.2-6) gives

f x x x x[ , , , ] . ( . ) .3 2 1 00 032169 0 073448

4 10 013760

Then p3(x) = 0.549306(x-1) - 0.073448(x-1)(x-3) + 0.013760(x-1)(x-3)(x-5)

p(2) = 0.622754 + 0.04128 = 0.664034 which has a percent true error of t = 4.2 %. Thus, the cubic interpolation polynomial improves the result as compared to the linear and quadratic interpolating polynomials.

Example 4.2-3:

zar97/09/03 Numerical Methods For Engineers page 4-27 Errors of Newton’s Interpolating Polynomials Recall that the truncation error for the Taylor Series could be expressed as

R fn

x x where x xn

n

i in

i i

1

11

11( )

( )!( )

An equivalent expression for the error for the nth-order interpolating polynomial may be written as

R fn

x x x x x x where x xn

n

n n

1

0 1 01( )

( )!( )( ). . .( )

(4.2-16)

This expression requires that the function is known and differentiable which is not usually the case. An alternative form using a finite divided difference to approximate the (n+1)th derivative is recommended. R f x x x x x x x x x xn n n n [ , , , . . ., ]( )( ) . . .( )1 0 0 1 The above error expression may be then be used to estimate the error if an additional data point f(xn+1) is available, as in the form R f x x x x x x x x x xn n n n n [ , , ,. . ., ]( )( ). . .( )1 1 0 0 1 (4.2-17)

zar97/09/03 Numerical Methods For Engineers page 4-28

Estimate the error in both a quadratic and a cubic interpolating polynomial in

the previous examples. Solution:

i xi f(xi) 1st FDD 2nd FDD 3rd FDD 4th FDD 0 1 0 1 3 1.098612 2 5 1.609638 3 4 1.386294

Example 4.2-4:

zar97/09/03 Numerical Methods For Engineers page 4-29 4.2.2 Lagrange Interpolating Polynomials The Lagrange interpolating polynomial is a reformulation of the Newton polynomial that avoids the calculation of finite divided differences. It can be represented as

p x L x f xn i ii

n( ) ( ) ( )

0 (4.2-18)

where

L xx xx xi

j

i jjj i

n( )

0

(4.2-19)

with denotes the “product of”. For example, (i) Linear version (n=1)

p x x xx x

f x x xx x

f x11

0 10

0

1 01( ) ( ) ( )

(ii) the second-order version (n=2)

p x x xx x

x xx x

f x x xx x

x xx x

f x x xx x

x xx x

f x21

0 1

2

0 20

0

1 0

2

1 21

0

2 0

1

2 12( ) ( ) ( ) ( )

zar97/09/03 Numerical Methods For Engineers page 4-30

For f(x) = ln x, estimate ln(2) using Lagrange interpolating polynomials of the

first and second order

i x f(x) 0 1 0 1 3 1.609638 2 6 1.791760

Solution: First order,

p1 2 2 31 3

0 2 13 1

1098612 0 549306( ) ( ) ( . ) .

Second order,

p2 22 31 3

2 61 6

02 13 1

2 63 6

10986122 16 1

2 36 3

1791760 0 612957( ) ( ) ( . ) ( . ) .

Example 4.2-5:

zar97/09/03 Numerical Methods For Engineers page 4-31 Error Estimate of Lagrange’s Interpolating Polynomials An error associated with the Lagrange interpolating polynomial can be estimated in a similar manner as with the Newton’s method and is given as

R f x x x x x xn n n n ii

n

[ , , , . . ., ]1 1 0

0 (4.2-20)

However, since this estimate contains the finite divided differences, it is rarely used with the Lagrange interpolation. Read section 12.3 and section 12.4 (p.384-387). Remark: 1. Interpolation using both the Newton and the Lagrange polynomials may be used with equally or arbitrarily spaced data.. However, the method with equally spaced data is preferable since it may later be used to derive numerical integration formulas. 2. Most systems in engineering are notoriously ill-conditioned, especially when we attempt to determine the coefficients of the polynomial in the conventional form. Therefore, lower-order polynomials are recommended and results must be checked carefully.

zar97/09/03 Numerical Methods For Engineers page 4-32 4.2.3 Spline Interpolation So far we have used the nth-order polynomials to interpolate between (n+1) data points. However, there are cases when the polynomials, especially of higher degree, lead to erroneous results. Thus, an alternative approach connecting lower order polynomials to subsets of data points is used. Such connecting polynomials are called spline functions. Spline functions have special characteristics that the connections between adjacent curves are visually smooth. They also provide superior approximation when there is abrupt change in the function. I. Linear Splines - connecting two points with a straight line successively. The first-order splines for a set of ordered data points (x0, x1, x2,. . ., xn) can be defined as a set of (n-1) linear functions, f(x) = f(x0) + m0 (x - x0 ) x0 x x1 f(x) = f(x1) + m1 (x - x0 ) x1 x x2 (4.2-21) . . . f(x) = f(xn-1) + mn-1 (x - xn-1 ) xn-1 x xn where

m f x f xx xii i

i i

( ) ( )1

1 (4.2-22)

These equations may be used to evaluate the function at any point in the corresponding interval. Note that the approach is similar to linear interpolation.

zar97/09/03 Numerical Methods For Engineers page 4-33

Fit the data with first-order splines and evaluate the function at x = 2.7.

x 1 2 3 4 f(x) 1 4 9 16

Solution: The interval 2 x 3 contains the point x = 2.7, therefore, the slope

m

9 43 2

5

and the resulting linear spline, f(x) = f(2) + 5 (x - 2) 2 x 3 f(2) = 4 + 5 (2.7 - 2) = 7.5 Note: The first-order splines are neither continuous in the first derivative nor smooth at the connections (called a knot). This is the main disadvantage of the linear spline.

Example 4.2-6 :

zar97/09/03 Numerical Methods For Engineers page 4-34 II. Quadratic Splines. To ensure that the mth derivatives are continuous at the knots, we have to use a spline of at least degree (m+1). Therefore, the quadratic splines will have continuous first derivatives at the knots. In this section, we wish to derive a second-order polynomial for each interval between data points. The general form of the polynomial for each interval can be represented as iiii cxbxaxf 2)( (4.2-23) For (n+1) data points (x0, x1, x2, . . . , xn), there are n intervals. In each interval, there are 3 unknown constants to be determined, consequently, 3n unknown constants (the a’s, b’s, and c’s) to evaluate. They require 3n simultaneous equations or conditions to be solved. Figure 4-5 : Quadratic splines over three intervals.

zar97/09/03 Numerical Methods For Engineers page 4-35 These conditions are as follows 1. The value of the functions must be equal at the interior nodes, that is f x a x b x c a x b x c for i to ni i i i i i i i i i i( ) .

21

21 1 1 1 (4.2-24)

These equations provide 2n - 2 conditions since only interior nodes are used. 2. The first and the last functions must pass through the end points, that is f x a x b x c and f x a x b x cn n n n n n( ) ( )0 1 0

21 0 1

2 (4.2-25) This provides two additional conditions for a total of 2n conditions. 3. The first derivatives at the interior points must be equal, that is 2 2 1 11 1a x b a x b for i to ni i i i i i . (4.2-26) This provides another n - 1 conditions for a total of 3n - 1. 4. Assume that the second derivative is zero at the first point, that is a1 = 0. (4.2-27) Therefore, we now have 3n conditions which can be used to solve for 3n unknown constants. Note: the last condition restricts the function to be linear for the first two points.

zar97/09/03 Numerical Methods For Engineers page 4-36

Fit the data with the quadratic splines and evaluate the function at x = 2.7.

x f(x) 1 1 2 4 3 9 4 16

Solution: Since we have 4 data points, we need 3 intervals and 9 conditions to solve for the quadratic splines. 1. Functions are equal at the interior nodes. @ x = 2, 4a1 + 2b1 + c1 = 4a2 + 2b2 + c2 = 4 @ x = 3, 9a2 + 3b2 + c2 = 9a3 + 3b3 + c3 = 9 2. First and last functions pass through the end points. @ x = 1, a1 + b1 + c1 = 1 @ x = 4, 16a3 + 4b3 + c3 = 16 3. The first derivatives are equal at the interior nodes. @ x = 2, 4a1 + b1 = 4a2 + b2 @ x = 3, 6a2 + b2 = 6a3 + b3 4. The second derivative at the first point is zero. a1 = 0

Example 4.2-7 :

zar97/09/03 Numerical Methods For Engineers page 4-37 We now only have 8 unknowns since a1 = 0. We may express the preceding conditions in a matrix form as

2 1 0 0 0 0 0 00 0 4 2 1 0 0 00 0 9 3 1 0 0 00 0 0 0 0 9 3 11 1 0 0 0 0 0 00 0 0 0 0 16 4 11 0 4 1 0 0 0 00 0 6 1 0 6 1 0

44991

1600

1

1

2

2

2

3

3

3

bcabcabc

Solving for the unknown constants yield a1 = 0 b1 = 3 c1 = - 2 a2 = 2 b2 = - 5 c2 = 6 a3 = 0 b3 = 7 c3 = - 12 The quadratic splines for each interval are then, f1 (x) = 3x - 2 1 x 2

f2 (x) = 2x2 - 5x + 6 2 x 3

f3 (x) = 7x - 12 3 x 4 The interpolation at x = 2.7, f2 (2.7) = 2 (2.7)2 - 5 (2.7) + 6 = 7.08 and the exact value is 7.29.

zar97/09/03 Numerical Methods For Engineers page 4-38 III. Cubic Splines. To ensure that the mth derivatives are continuous at the nodes, we have to use a spline of at least degree (m+1). Therefore, the cubic splines will have continuous first and second derivatives at the knots. In this section, we wish to derive a third-order polynomial for each interval between data points. The general form of the cubic spline for each interval can be represented as f x a x b x c x di i i i i( ) 3 2 (4.2-28) For (n+1) data points (x0, x1, x2, . . . , xn), there are n intervals. In each interval, there are 4 unknown constants to be determined, consequently, 4n unknown constants (the a’s, b’s, c’s and d’s) to evaluate. They require 4n simultaneous equations or conditions. Figure 4-6 : Cubic splines over three intervals.

zar97/09/03 Numerical Methods For Engineers page 4-39 These conditions are as follows 1. The value of the functions are equal at the interior nodes, that is f x a x b x c x d a x b x c x d for i to ni i i i i i i i i i i i i i i( ) .

3 21

31

21 1 1 1 (4.2-29)

These equations provide 2n - 2 conditions since only interior nodes are used. 2. The first and the last functions must pass through the end points, that is f x a x b x c x d and f x a x b x c x dn n n n n n n i( ) ( )0 1 0

31 0

21 0 1

3 2 (4.2-30) This provides two additional conditions for a total of 2n conditions. 3. The first derivatives at the interior nodes must be equal, that is 3 2 3 2 1 12

12

1 1a x b x c a x b x c for i to ni i i i i i i i i i . (4.2-31) This provides another n - 1 conditions for a total of 3n - 1. 4. The second derivative at the interior nodes must be equal, that is 6 2 6 2 1 11 1a x b a x b for i to ni i i i i i . (4.2-32) This provides an additional n - 1 conditions for a total of 4n - 2. 5. Assume that the third derivatives at the first and last nodes are zero, that is a1 = 0 and an = 0. We now have 4n conditions which can be used to solve for 4n unknown constants. Note: the last condition restricts the function to be quadratic for the first dan last two points and there are several assumptions could be made.

zar97/09/03 Numerical Methods For Engineers page 4-40

Fit the data with the quadratic splines and evaluate the function at x = 2.7.

x f(x) 1 1 2 4 3 9 4 16

Solution: Since we have 4 data points, we need 3 intervals and 12 conditions to solve for the cubic splines. 1. Functions are equal at the interior nodes. @ x = 2, 8a1 + 4b1 + 2c1 + d1 = 8a2 + 4b2 + 2c2 + d2 = 4 @ x = 3, 27a2 + 9b2 + 3c2 + d2 = 27a3 + 9b3 + 3c3 + d3 = 9 2. First and last functions pass through the end points. @ x = 1, a1 + b1 + c1 + d1 = 1 @ x = 4, 64a3 + 16b3 + 4c3 + d3 = 16 3. The first derivatives are equal at the interior nodes. @ x = 2, 12a1 + 4b1 + c1 = 12a2 + 4b2 + c2 @ x = 3, 27a2 + 6b2 + c2 = 27a3 + 6b3 + c3 4. The second derivative at the interior nodes are equal. @ x = 2, 12a1 + 2b1 = 12a2 + 2b2 @ x = 3, 18a2 + 2b2 = 18a3 + 2b3

Example 4.2-8 :

zar97/09/03 Numerical Methods For Engineers page 4-41 5. The third derivative is zero at the end nodes. a1 = 0 and a3 = 0. We now only have 10 unknowns since a1 = 0 and a3 = 0. We may express the preceding conditions in a matrix form as

4 5 1 0 0 0 0 0 0 00 0 0 8 4 2 1 0 0 00 0 0 27 9 3 1 0 0 00 0 0 0 0 0 0 9 3 11 1 1 0 0 0 0 0 0 00 0 0 0 0 0 0 16 4 14 1 0 0 0 0 12 4 1 00 0 0 27 6 1 0 6 1 00 2 0 0 0 0 12 2 0 00 0 0 18 2 0 0 2 0 0

1

1

1

2

2

2

2

3

3

3

bcdabcdbcd

44991

160000

Solving for the unknown constants yields a1 = 0 b1 = 0.625 c1 = 1.125 d1 = -0.75 a2 = 0.25 b2 = - 1.125 c2 = 5.875 d2 = - 5.25 a3 = 0 b3 = 1.125 c3 = - 0.875 d3 = 1.5 The quadratic splines for each interval are then, f1 (x) = 0.625x2 + 1.125x - 0.75 1 x 2

f2 (x) = 0.25x3 - 1.125x2 + 5.875x - 5.25 2 x 3

f3 (x) = 1.125x2 - 0.875x + 1.5 3 x 4 The interpolation at x = 2.7, f2 (2.7) = 0.25 (2.7)3 - 1.125 (2.7)2 + 5.875 (2.7) - 5.25 = 7.332.