
P1: JYS
c04 JWBK378-Fletcher May 23, 2009 4:21 Printer: Yet to come
Basic Mathematical Tools 35
4.4 ROOT FINDING
We will present two schemes in this section for finding the roots of a function y = f (x) with
f : R → R.
4.4.1 Bisection Method
The bisection method is a classiscal root-finding routine that does not require derivative infor-
mation. The ppf.math.root
finding module provides the following implementation
derived from the Boost.Math
Toolkit library:
import math
from special
functions import sign, max flt
def bisect(f, min, max, tol, max
its):
"""Bisection method
"""
fmin, fmax = f(min), f(max)
if fmin == 0: return (min, min, 0)
if fmax == 0: return (max, max, 0)
if min >= max: raise RuntimeError, "Arguments in wrong order"
if fmin*fmax >= 0: raise RuntimeError, "Root not bracketed"
count = max
its
if count < 3:
count = 0
else: count -= 3
while count and tol(min, max) == 0:
mid = (min + max)/2.
fmid = f(mid)
if mid == max or mid ==min:
break
if fmid == 0:
min = max = mid
break
elif sign(fmid)*sign(fmin) < 0:
max, fmax = mid, fmid
else:
min, fmin = mid, fmid
--count
max
its -= count
return (min, max, max
its)
The quadratic y(x) = x
2
+ 2x −1 has roots
−1 −
√
2 =−2.4142135623730950488016887242097
−1 +
√
2 =+0.4142135623730950488016887242097.