Numerical Differentiation
Numerical Differentiation
Olive R. Cawiding
Recall.
Definition.
The derivative of a function f at x0 is given by
f (x0 + h) − f (x0 )
f 0 (x0 ) = lim (0.1)
h→ h
Goal.
To approximate f 0 (x0 ) using different techniques
Computer Science 117
Numerical Differentiation
If x = x0 , then
f (x0 + h) − f (x0 ) h 00
f 0 (x0 ) = − f (ξ(x0 )). (0.2)
h 2
Computer Science 117
Numerical Differentiation
f (x0 + h) − f (x0 )
For small values of h, the difference quotient
h
can be used to approximate f 0 (x0 ) with an error bounded by
M |h|
, where M is a bound on |f 00 (x)| for x ∈ [a, b].
2
This formula is known as the forward-difference formula if h > 0
and the backward-difference formula if h < 0.
Computer Science 117
Numerical Differentiation
Computer Science 117
Numerical Differentiation
EXAMPLE.
Use the forward difference formula to approximate f 0 (1.8) with
h = 0.1. Find a bound for its error.
Computer Science 117
Numerical Differentiation
Numerical Differentiation
Differentiation leads to
n
0
X (x − x0 ) . . . (x − xn ) (n+1)
f (x) = f (xk )L0k (x) + Dx f (ξ(x))
(n + 1)!
k=0
(x − x0 ) . . . (x − xn )
+ Dx [f (n+1) (ξ(x))].
(n + 1)!
Computer Science 117
Numerical Differentiation
If x = xj , then
n n
X f (n+1)(ξ(xj )) Y
f 0 (xj ) = f (xk )L0k (x) + (xj − xk ). (0.4)
(n + 1)!
k=0 k=0,k6=j
Three-Point Formulas
Three-Point Formulas
We consider the case where these three nodes are equally spaced,
i.e.,
1 h2
f 0 (x0 ) = [−3f (x0 ) + 4f (x0 + h) − f (x0 + 2h)] + f (3) (ξ0 )
2h 3
where ξ0 lies between x0 and x0 + 2h
Computer Science 117
1 h2
f 0 (x0 ) = [f (x0 + h) − f (x0 − h)] − f (3) (ξ1 )
2h 6
where ξ1 lies between (x0 − h) and (x0 + h).
Remark.
The error term for both three-point formulas is O(h2 ).
Computer Science 117
Five-Point Formulas
1
f 0 (x0 ) = [f (x0 − 2h) − 8f (x0 − h) + 8f (x0 + h) − f (x0 + 2h)]
12h
h4 (5)
+ f (ξ)
30
where ξ lies between x0 − 2h and x0 + 2h.
Computer Science 117
Five-Point Formulas
1
f 0 (x0 ) = [−25f (x0 ) + 48f (x0 + h) − 36f (x0 + 2h)]
12h
h4
+ 16f (x0 + 3h) − 3f (x0 + 4h)] + f (5) (ξ)
5
where ξ lies between x0 and x0 + 4h.
This formula can be used for left-endpoint approximations with
h > 0 and for right-endpoint approximations with h < 0.
Remark.
The error term for both five-point formulas is O(h4 ).
Computer Science 117
Numerical Differentiation
EXAMPLE.
The values for f (x) = xex are given in the following table. Use
applicable three-point and five-point formulas to approximate
f 0 (2.0).
Computer Science 117
Numerical Differentiation
1 h2 (4)
f 00 (x0 ) = [f (x 0 − h) − 2f (x 0 ) + f (x 0 + h)] − f (ξ) (0.5)
h2 12
for some ξ, where x0 − h < ξ < x0 + h.
EXAMPLE.
Use the table of values in the previous example to approximate
f 00 (2.0).
Computer Science 117
1 h2
f 0 (x0 ) = [f (x0 + h) − f (x0 − h)] − f (3) (ξ1 )
2h 6
. Suppose that in evaluating f (x0 + h) and f (x0 − h) we
encounter round-off errors e(x0 + h) and e(x0 − h), i.e.,
Problem.
We want to minimize the error bound for the three-point midpoint
formula given by
h2
E(h) = + M. (0.10)
h 6
At what value of h is the error bound minimized?
Computer Science 117
EXAMPLE.
Consider the following values of f (x) = sin x using different values
of x. Use these values to approximate f 0 (0.900).
Computer Science 117
The optimal choice for h appears to lie between 0.005 and 0.05.
We can verify this by checking the minimum error bound.
Computer Science 117
Seatwork.