Docsity

Numerical Differentiation: Finite Difference Approximations and Error Analysis - Prof. Jam, Study notes of Mathematical Methods for Numerical Analysis and Optimization

Toggle sidebar
bg1
Jim Lambers
MAT 460/560
Fall Semester 2009-10
Lecture 23 Notes
These notes correspond to Section 4.1 in the text.
Numerical Differentiation
We now discuss the other fundamental problem from calculus that frequently arises in scientific
applications, the problem of computing the derivative of a given function 𝑓(𝑥).
Finite Difference Approximations
Recall that the derivative of 𝑓(𝑥) at a point 𝑥0, denoted 𝑓′(𝑥0), is defined by
𝑓′(𝑥0) = lim
ℎ→0
𝑓(𝑥0+ℎ)−𝑓(𝑥0)
ℎ.
This definition suggests a method for approximating 𝑓′(𝑥0). If we choose ℎto be a small positive
constant, then
𝑓′(𝑥0)≈𝑓(𝑥0+ℎ)−𝑓(𝑥0)
ℎ.
This approximation is called the forward difference formula.
To estimate the accuracy of this approximation, we note that if 𝑓′′(𝑥) exists on [𝑥0, 𝑥0+ℎ],
then, by Taylor’s Theorem, 𝑓(𝑥0+ℎ) = 𝑓(𝑥0) + 𝑓′(𝑥0)ℎ+𝑓′′(𝜉)ℎ2/2,where 𝜉∈[𝑥0, 𝑥0+ℎ].Solving
for 𝑓′(𝑥0), we obtain
𝑓′(𝑥0) = 𝑓(𝑥0+ℎ)−𝑓(𝑥0)
ℎ+𝑓′′(𝜉)
2ℎ,
so the error in the forward difference formula is 𝑂(ℎ). We say that this formula is first-order
accurate.
The forward-difference formula is called a finite difference approximation to 𝑓′(𝑥0), because it
approximates 𝑓′(𝑥) using values of 𝑓(𝑥) at points that have a small, but finite, distance between
them, as opposed to the definition of the derivative, that takes a limit and therefore computes the
derivative using an “infinitely small” value of ℎ. The forward-difference formula, however, is just
one example of a finite difference approximation. If we replace ℎby −ℎin the forward-difference
formula, where ℎis still positive, we obtain the backward-difference formula
𝑓′(𝑥0)≈𝑓(𝑥0)−𝑓(𝑥0−ℎ)
ℎ.
Like the forward-difference formula, the backward difference formula is first-order accurate.
1
pf3
pf4
pf5

Partial preview of the text

Download Numerical Differentiation: Finite Difference Approximations and Error Analysis - Prof. Jam and more Study notes Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

Jim Lambers MAT 460/ Fall Semester 2009- Lecture 23 Notes

These notes correspond to Section 4.1 in the text.

Numerical Differentiation

We now discuss the other fundamental problem from calculus that frequently arises in scientific applications, the problem of computing the derivative of a given function 푓 (푥).

Finite Difference Approximations

Recall that the derivative of 푓 (푥) at a point 푥 0 , denoted 푓 ′(푥 0 ), is defined by

푓 ′(푥 0 ) = lim ℎ→ 0

This definition suggests a method for approximating 푓 ′(푥 0 ). If we choose ℎ to be a small positive constant, then

푓 ′(푥 0 ) ≈

This approximation is called the forward difference formula. To estimate the accuracy of this approximation, we note that if 푓 ′′(푥) exists on [푥 0 , 푥 0 + ℎ], then, by Taylor’s Theorem, 푓 (푥 0 + ℎ) = 푓 (푥 0 ) + 푓 ′(푥 0 )ℎ + 푓 ′′(휉)ℎ^2 / 2 , where 휉 ∈ [푥 0 , 푥 0 + ℎ]. Solving for 푓 ′(푥 0 ), we obtain

푓 ′(푥 0 ) =

so the error in the forward difference formula is 푂(ℎ). We say that this formula is first-order accurate. The forward-difference formula is called a finite difference approximation to 푓 ′(푥 0 ), because it approximates 푓 ′(푥) using values of 푓 (푥) at points that have a small, but finite, distance between them, as opposed to the definition of the derivative, that takes a limit and therefore computes the derivative using an “infinitely small” value of ℎ. The forward-difference formula, however, is just one example of a finite difference approximation. If we replace ℎ by −ℎ in the forward-difference formula, where ℎ is still positive, we obtain the backward-difference formula

Like the forward-difference formula, the backward difference formula is first-order accurate.

If we average these two approximations, we obtain the centered-difference formula

To determine the accuracy of this approximation, we assume that 푓 ′′′(푥) exists on the interval [푥 0 − ℎ, 푥 0 + ℎ], and then apply Taylor’s Theorem again to obtain

ℎ^2 +

ℎ^3 ,

ℎ^2 −

ℎ^3 ,

where 휉+ ∈ [푥 0 , 푥 0 + ℎ] and 휉− ∈ [푥 0 − ℎ, 푥 0 ]. Subtracting the second equation from the first and solving for 푓 ′(푥 0 ) yields

ℎ^2.

By the Intermediate Value Theorem, 푓 ′′′(푥) must assume every value between 푓 ′′′(휉−) and 푓 ′′′(휉+) on the interval (휉−, 휉+), including the average of these two values. Therefore, we can simplify this equation to

푓 ′(푥 0 ) =

ℎ^2 ,

where 휉 ∈ [푥 0 −ℎ, 푥 0 +ℎ]. We conclude that the centered-difference formula is second-order accurate. This is due to the cancellation of the terms involving 푓 ′′(푥 0 ).

Example Consider the function

푓 (푥) =

sin^2

푥^2 +푥 cos 푥−푥

sin

√푥−^1 푥^2 +

Our goal is to compute 푓 ′(0.25). Differentiating, using the Quotient Rule and the Chain Rule, we obtain

2 sin

푥^2 +푥 cos 푥−푥

cos

푥^2 +푥 cos 푥−푥

 h 2 푥+ 2 √푥^2 +1(cos 푥−푥) +

√ 푥^2 +1(sin 푥+1) (cos 푥−푥)^2

i

sin

√푥−^1 푥^2 +

sin

푥^2 +푥 cos 푥−푥

cos

√푥−^1 푥^2 +

 h 1 2 √푥 √ 푥^2 +1 −^

푥(√푥−1) (푥^2 +1)^3 /^2

i

sin^2

√푥−^1 푥^2 +

Evaluating this monstrous function at 푥 = 0.25 yields 푓 ′(0.25) = − 9. 066698770.

which is fourth-order accurate. The reason it is called a five-point formula, even though it uses the value of 푓 (푥) at four points, is that it is derived from the Lagrange polynomials for the five points 푥 0 − 2 ℎ, 푥 0 − ℎ, 푥 0 , 푥 0 + ℎ, and 푥 0 + 2ℎ. However, 푓 (푥 0 ) is not used in the formula because ℒ′ 4 , 0 (푥 0 ) = 0, where ℒ 4 , 0 (푥) is the Lagrange polynomial that is equal to one at 푥 0 and zero at the other four points. If we do not have any information about 푓 (푥) for 푥 < 푥 0 , then we can use the following five-point formula that actually uses the values of 푓 (푥) at five points,

ℎ^4 ,

where 휉 ∈ [푥 0 , 푥 0 + 4ℎ]. As before, we can replace ℎ by −ℎ to obtain a similar formula that approximates 푓 ′(푥 0 ) using the values of 푓 (푥) at 푥 0 , 푥 0 − ℎ, 푥 0 − 2 ℎ, 푥 0 − 3 ℎ, and 푥 0 − 4 ℎ. The strategy of differentiating Lagrange polynomials to approximate derivatives can be used to approximate higher-order derivatives. For example, the second derivative can be approximated using a centered difference formula,

푓 ′′(푥 0 ) ≈

ℎ^2

which is second-order accurate.

Example We will construct a formula for approximating 푓 ′(푥) at a given point 푥 0 by interpolating 푓 (푥) at the points 푥 0 , 푥 0 + ℎ, and 푥 0 + 2ℎ using a second-degree polynomial 푝 2 (푥), and then approximating 푓 ′(푥 0 ) by 푝′ 2 (푥 0 ). Since 푝 2 (푥) should be a good approximation of 푓 (푥) near 푥 0 , especially when ℎ is small, its derivative should be a good approximation to 푓 ′(푥) near this point. Using Lagrange interpolation, we obtain

푝 2 (푥) = 푓 (푥 0 )ℒ 2 , 0 (푥) + 푓 (푥 0 + ℎ)ℒ 2 , 1 (푥) + 푓 (푥 0 + 2ℎ)ℒ 2 , 2 (푥),

where {ℒ 2 ,푗 (푥)}^2 푗=0 are the Lagrange polynomials for the points 푥 0 , 푥 1 = 푥 0 + ℎ and 푥 2 = 푥 0 + 2ℎ. Recall that these polynomials satisfy

ℒ 2 ,푗 (푥푘) = 훿푗푘 =

1 if 푗 = 푘 0 otherwise

Using the formula for the Lagrange polynomials,

Y^2

푖=0,푖∕ =푗

we obtain

ℒ 2 , 0 (푥) =

푥^2 − (2푥 0 + 3ℎ)푥 + (푥 0 + ℎ)(푥 0 + 2ℎ)

2 ℎ^2

푥^2 − (2푥 0 + 2ℎ)푥 + 푥 0 (푥 0 + 2ℎ)

−ℎ^2

푥^2 − (2푥 0 + ℎ)푥 + 푥 0 (푥 0 + ℎ)

2 ℎ^2

It follows that

ℒ′ 2 , 0 (푥) =

2 ℎ^2

ℎ^2

2 ℎ^2

We conclude that 푓 ′(푥 0 ) ≈ 푝′ 2 (푥 0 ), where 푝′ 2 (푥 0 ) = 푓 (푥 0 )ℒ′ 2 , 0 (푥 0 ) + 푓 (푥 0 + ℎ)ℒ′ 2 , 0 (푥 0 ) + 푓 (푥 0 + 2ℎ)ℒ′ 2 , 0 (푥 0 )

≈ 푓 (푥 0 )

Using Taylor’s Theorem to write 푓 (푥 0 + ℎ) and 푓 (푥 0 + 2ℎ) in terms of Taylor polynomials centered at 푥 0 , it can be shown that the error in this approximation is 푂(ℎ^2 ), and that this formula is exact when 푓 (푥) is a polynomial of degree 2 or less. □

In a practical implementation of finite difference formulas, it is essential to note that roundoff error in evaluating 푓 (푥) is bounded independently of the spacing ℎ between points at which 푓 (푥) is evaluated. It follows that the roundoff error in the approximation of 푓 ′(푥) actually increases as ℎ decreases, because the errors incurred by evaluating 푓 (푥) are divided by ℎ. Therefore, one must choose ℎ sufficiently small so that the finite difference formula can produce an accurate approximation, and sufficiently large so that this approximation is not too contaminated by roundoff error.

Automatic Differentiation

If the function 푓 (푥) can be represented as a sequence of computations, each consisting of the evaluation of some differentiable function with differentiable arguments, then a similar sequence of