week2B_sp25
week2B_sp25
Spring 2025
Numerical algorithms for evaluating derivatives
′ f (x0 + h) − f (x0 )
ffwd (x0 ) =
h
′ (x ) provides an approximate value of
Forward difference ffwd 0
f ′ (x0 ).
• Truncation error:
|f ′ (x0 ) − ffwd
′
(x0 )| = O(h)
sin(x0 + h) − sin(x0 )
sin′fwd (x0 ) =
h
h is a parameter we need to determine.
Python example of forward difference
Round-off error can become larger than truncation error
′ (x) need to be
• Round-off error: numerical error since ffwd
evaluated numerically.
The smallest round-off error of a double precision number is
f (x + h) − f (x) + 2ϵ
total error = − f ′ (x) (3)
h
f (x + h) − f (x) 2ϵ
= − f ′ (x) + (4)
| h {z h
} |{z}
trunc err r .o. err
f ′′ (s) 2ϵ
≤ h + (5)
| 2{z } h
|{z}
trunc err r .o. err
h 2ϵ
≤ + (6)
2
|{z} h
|{z}
trunc err r .o. err
Truncation error decreases as h decreases, but round-off error
increases as h decreases.
h 2ϵ
total error = + (7)
2 h
f (x0 + h) − f (x0 − h)
f ′ (x0 ) = + O(h2 ) (13)
2h
which has numerical accuracy of O(h2 ).
Recall that forward difference
f (x0 + h) − f (x0 )
f ′ (x0 ) = + O(h) (14)
h
has numerical precision of O(h).
• This means that truncation error goes to zero as h2 . If we
decrease h by a factor 0.1, then the truncation error decreases by a
factor of 0.01 in the centered difference.
• In contrast, the forward difference decreases by a factor of 0.1.
Python example of centered difference
Round-off error of centered difference
We can follow the same analysis performed on the forward
difference to estimate the total error of centered difference.
The only change is that the truncation is error is O(h2 ) (not
O(h)). Then the total error is
2ϵ
total error = Dh2 + (15)
h
Its minimum value is attained at
d 2ϵ
total error = 2Dh − 2 = 0
dh h
ϵ 1/3 10−16 1/3
h= ≈ ≈ 10−5 .
D 0.2
|F (x) − F (x + δx)|
absolute condition number = lim
δx→0 |δx|
|F (x) − F (x + δx)|/|F (x)|
relative condition number = lim
δx→0 |δx|/|x|
Example: condition number of evaluating a function
|f (x) − f (x + δx)|
abs cond num = lim = |f ′ (x)|
δx→0 |δx|
|f (x) − f (x + δx)|/|f (x)|
rel cond num = lim = |xf ′ (x)/f (x)|
δx→0 |δx|/|x|
How much does the root r˜ of f˜(x) deviate from the root r of f (x)?
In other words, how large is δr in
r˜ = r + δr ?
Estimate δr by finding an approximation of its value.
0 = f˜(˜
r ) = f (r + δr ) + ϵh(r + δr )
f ′′ (r )
= f (r ) + f ′ (r )δr + (δr )2 + ...
2
+ ϵ[h(r ) + h′ (r )δr + ...]
≈ 0 + f ′ (r )δr + ϵh(r )
ϵh(r )
δr = − .
f ′ (r )
r˜ − r δr 1
Ka (r ) = = = ′ .
˜
f −f ϵh(r ) |f (r )|
Example: condition number of finding a root
Consider a function
The root of f is
f (r ) = e kr − e k = 0 → r = 1.
error ≤ |a − b|/2k
Python example of Bisection Method