Solutions for http://quant-econ.net/scipy.html
Here's a reasonable solution:
In [6]:
def bisect(f, a, b, tol=10e-5):
"""
Implements the bisection root finding algorithm, assuming that f is a
real-valued function on [a, b] satisfying f(a) < 0 < f(b).
"""
lower, upper = a, b
if upper - lower < tol:
return 0.5 * (upper + lower)
else:
middle = 0.5 * (upper + lower)
print('Current mid point = {}'.format(middle))
if f(middle) > 0: # Implies root is between lower and middle
bisect(f, lower, middle)
else: # Implies root is between middle and upper
bisect(f, middle, upper)
We can test it as follows
In [8]:
import numpy as np
f = lambda x: np.sin(4 * (x - 0.25)) + x + x**20 - 1
bisect(f, 0, 1)
In [ ]: