Numerical Methods for Civil · PDF fileNumerical Methods for Civil Engineers ... Use forward...

12
CE311K 1 DCM 2/8/09 Numerical Methods for Civil Engineers Lecture Notes CE 311K Daene C. McKinney Introduction to Computer Methods Department of Civil, Architectural and Environmental Engineering The University of Texas at Austin Numerical Differentiation Introduction Taylor Series The Taylor series expansion of a function f(x) in a region of x close to x = a is the infinite series If the Taylor series exists, then knowing f(x) and all of its derivatives at x = a, we can find the value of f(x) at some x different from a, as long as we remain “sufficiently close” to x = a. As the distance between x and a increases, we reach a point where the series expansion no longer converges to the correct value, and we are no longer “sufficiently close” to a. If the series converges, then the value of f(x) will be exact if an infinite number of terms are used in the series. Consider the situation shown in the following figure. We wish to find f(b) using information from point a. From the series expansion, we have

Transcript of Numerical Methods for Civil · PDF fileNumerical Methods for Civil Engineers ... Use forward...

Page 1: Numerical Methods for Civil · PDF fileNumerical Methods for Civil Engineers ... Use forward and backward difference approximations of O(h) ... Use forward and backward difference

CE311K 1 DCM 2/8/09

Numerical Methods for Civil Engineers Lecture Notes

CE 311K Daene C. McKinney

Introduction to Computer Methods Department of Civil, Architectural and Environmental Engineering

The University of Texas at Austin

Numerical Differentiation

Introduction

Taylor Series

The Taylor series expansion of a function f(x) in a region of x close to x = a is the infinite series

If the Taylor series exists, then knowing f(x) and all of its derivatives at x = a, we can find the

value of f(x) at some x different from a, as long as we remain “sufficiently close” to x = a. As

the distance between x and a increases, we reach a point where the series expansion no longer

converges to the correct value, and we are no longer “sufficiently close” to a. If the series

converges, then the value of f(x) will be exact if an infinite number of terms are used in the

series.

Consider the situation shown in the following figure. We wish to find f(b) using information

from point a. From the series expansion, we have

Page 2: Numerical Methods for Civil · PDF fileNumerical Methods for Civil Engineers ... Use forward and backward difference approximations of O(h) ... Use forward and backward difference

CE311K 2 DCM 2/8/09

If only the first term of Equation (2) is used, then the function is assumed to be a constant, f(a),

as shown in Figure a. If the first two terms are used, then the slope of f(x) at x=a is taken into

account by using the straight line from f(a) with a slope of f’(a) as shown in Figure b.

Considering three terms allows the use of the curvature due to f’’(a) as shown in Figure c, etc.

Each additional term improves the accuracy in the approximation for f(b).

The error in the Taylor series for f(x) when the series is truncated after the term containing

is not greater than

where the subscript “max” denotes the maximum magnitude of the derivative on the interval

from a to x. The error is said to be “of the order of ”, and the notation is is

commonly used to denote this. If the series expansion for f(x) is truncated after the first three

terms, we say that f(x) is accurate to , since

Page 3: Numerical Methods for Civil · PDF fileNumerical Methods for Civil Engineers ... Use forward and backward difference approximations of O(h) ... Use forward and backward difference

CE311K 3 DCM 2/8/09

Figure 1. Approximation resulting from truncating Taylor series.

Example: Find the Taylor series expansion for sin(x) near x = 0. That is a = 0 and the Taylor series is

Since sin(0) = 0 and cos(0) = 1,

or

Example: Use a Taylor series expansion to find . If we write the expansion with zero (0) as

the base point, we have

Page 4: Numerical Methods for Civil · PDF fileNumerical Methods for Civil Engineers ... Use forward and backward difference approximations of O(h) ... Use forward and backward difference

CE311K 4 DCM 2/8/09

or

and, if x = 0.5

or, to order

Now, the error in this approximation should not be greater than

where “max” denotes the maximum magnitude on 0 x 0.5. , so the

error is no greater than

The actual error is

which lies within the error bound. Notice that in this case the error which was was

actually 0.0237 or about

Numerical Derivatives

Page 5: Numerical Methods for Civil · PDF fileNumerical Methods for Civil Engineers ... Use forward and backward difference approximations of O(h) ... Use forward and backward difference

CE311K 5 DCM 2/8/09

The Taylor series expansion of the function f(x) in a small neighborhood around the point x (see

Figure 2) is

where . Solving this equation for f’(x) we have

where

is the remainder term. Thus, we have found an expression for the first derivative of the function f(x) with respect to x which is accurate to within an error of h. So, the forward difference approximation of the first derivative f’(x) is

where a subscript notation has been used

Figure 2. Forward, backward and central finite-difference approximations of the first derivative.

Page 6: Numerical Methods for Civil · PDF fileNumerical Methods for Civil Engineers ... Use forward and backward difference approximations of O(h) ... Use forward and backward difference

CE311K 6 DCM 2/8/09

Graphically, the expression approximates the slope of the function f at the point x by

the slope of the straight line passing through f(x+h) and f(x).

We can also use the Taylor series expansion of f(x) about x to determine f(x-h):

Solving for f’(x) we have

where

So, the backward difference approximation of the first derivative f’(x) is

Graphically, the expression approximates the slope of the function f at the point x by

the slope of the straight line passing through f(x-h) and f(x).

We can derive a more accurate estimate of the first derivative by using the forward and backward

Taylor series expansions about x

and

Subtracting the second equation from the first, we see that the terms involving even powers of h cancel, leaving

Page 7: Numerical Methods for Civil · PDF fileNumerical Methods for Civil Engineers ... Use forward and backward difference approximations of O(h) ... Use forward and backward difference

CE311K 7 DCM 2/8/09

or, solving for f’(x)

where

Employing the subscript notation, we have a central difference approximation of the first

derivative, and it is accurate to or second order

An expression for the second derivative is obtained by adding the previous Taylor series expressions to get

Solving for f’’(x)

So, the central difference approximation of the second derivative accurate to , or second

order, is

Example: Consider the function with its first three derivatives

, , and Forward, backward and central finite difference formulas for the first derivative are

Approximation Formula Error

Page 8: Numerical Methods for Civil · PDF fileNumerical Methods for Civil Engineers ... Use forward and backward difference approximations of O(h) ... Use forward and backward difference

CE311K 8 DCM 2/8/09

Forward

Backward

Central

Let x = 1.0 and compare the results of using these finite difference formulas to compute the first

derivative with the exact value for the conditions x = 0.1, 0.05, 0.025, and 0.0125.

x Forward Error Backward Error Centered Error

0.1 4.88E-01 -1.25E-02 5.13E-01 1.25E-02 5.01E-01 -6.25E-04

0.05 4.94E-01 -6.25E-03 5.06E-01 6.25E-03 5.00E-01 -1.56E-04

0.025 4.97E-01 -3.13E-03 5.03E-01 3.13E-03 5.00E-01 -3.91E-05

Exercises

1. Use first- through fourth-order Taylor series expansions to predict f(3) for f(x) = ln(x) using a

base point at x = 1. Compute the percent relative error for each approximation. Discuss the

meaning of the results.

2. Consider a uniform beam 1 m long which is simply supported at both ends, the bending moment is given my the following equation

where y(x) is the deflection, M(x) is the bending moment, and EI is the flexural rigidity. Calculate the bending moment at each grid point along the beam, including the two end points, using the following deflection distribution.

Page 9: Numerical Methods for Civil · PDF fileNumerical Methods for Civil Engineers ... Use forward and backward difference approximations of O(h) ... Use forward and backward difference

CE311K 9 DCM 2/8/09

i xi yi

0 0 0.000

1 0.2 7.780

2 0.4 10.68

3 0.6 8.370

4 0.8 3.970

5 1.0 0.000

3. Calculate the first derivative of

at x = 1.0 by the three finite difference methods outlined in class: forward, backward and central approximation using x = 0.1, 0.05, and 0.02. For each approximation, calculate the

percent error in the approximation by comparing with the exact value. 4. What is the advantage of using a "central" finite-difference approximation for the first

derivative of a function rather than using a "backward" finite-difference approximation?

5. Use zero- through third-order Taylor series expansions to predict f(3) for

using a base point at x = 2. Compute the true percent relative error for each approximation.

6. Use forward and backward difference approximations of O(h) and a central difference approximation of O(h2) to estimate the first derivative of the function

Evaluate the derivative at x = 2.5 using a step size of h = 0.25. Compare your results with the true value of the derivative at x = 2.5.

7. The rate of cooling of a body can be expressed as

where T is the temperature of the body (oC), Ta is the temperature of the surrounding medium

Page 10: Numerical Methods for Civil · PDF fileNumerical Methods for Civil Engineers ... Use forward and backward difference approximations of O(h) ... Use forward and backward difference

CE311K 10 DCM 2/8/09

(oC), and k is the proportionality constant (per minute). Suppose a body is heated to 90 oC and

dropped into water that is held constant at Ta = 25 oC, the temperature of the body changes, as in

t, time (minutes) T, Temperature (oC)

0 90

5 49.9

10 33.8

15 28.4

20 26.2

25 25.4

Utilize numerical differentiation to determine dT/dt at each value of time. Use the most accurate

differentiation formulas you can at each time.

8. (adapted from Chapra and Canale Prob. 3.11) Use forward and backward difference approximations of O(h) and a central difference approximation of O(h2) to estimate the first derivative of the function examined in part (b). Evaluate the derivative at x = 2.5 using a step size of h = 0.25. Compare your results with the true value of the derivative at x = 2.5.

9. Given the following function and point:

(a) For a central difference approximation of the first derivative of f that is accurate to four-decimal-places, what is the size of x that is needed. Estimate the error at that x.

(b) Approximate the first derivative of f at the indicated point.

(c) Compare the approximate derivative with the actual value of the derivative.

10. Given the function

Page 11: Numerical Methods for Civil · PDF fileNumerical Methods for Civil Engineers ... Use forward and backward difference approximations of O(h) ... Use forward and backward difference

CE311K 11 DCM 2/8/09

f (x) = tan(40x) ,

(a) Find f (x) at x = 0.175 analytically.

(b) Find f (x) at x = 0.175 using a backward finite-difference representation of the first

derivative with x = 0.075 .

(c) Do your answers for parts (a) and (b) agree? If not, why not. Which one is correct?

11. (a) Find the Taylor series expansion about x = 0 for

f (x) = ln(1 - x)

where ln( ) is the natural logarithm function.

(b) Using the results of part (a), obtain the geometric series expansion for

g(x) = 1

1 - x, x < 1

Hint: If

ln(1 - x) = - (x + x2

2 +

x3

3 +

x4

4 + )

Differentiate both sides of this series.

12. Evaluate the accuracy (% error) gained by using a central finite difference representation of

x( )2[ ] (second-order) as compared to a forward finite difference representation of x( )[ ]

(first-order) in evaluating f (x) at x = 1 using x = 0.1 where

f (x) = e x

13. Starting from x = 25, use the first three terms of a Taylor’s series to find the value of

at x = 27.

14. The following table gives the values of a function . Using finite difference

approximations, fill in the table for . Find the approximate error, , in your calculation.

Page 12: Numerical Methods for Civil · PDF fileNumerical Methods for Civil Engineers ... Use forward and backward difference approximations of O(h) ... Use forward and backward difference

CE311K 12 DCM 2/8/09

2.00 7.3891

2.05 7.7679

2.10 8.1662

2.15 8.5849

15. Calculate the first derivative of

at x = 1.0 by the three finite difference methods: forward, backward and central approximation using x = 0.1, and 0.05. For each approximation, calculate the percent error in the

approximation by comparing with the exact value.