Let's examine automated testing for the bisection algorithm. With this algorithm, a zero of a real-valued function is found. It is described in Exercise 4 in Section 7.10: Exercises. An implementation of the algorithm can have the following form:
def bisect(f, a, b, tol=1.e-8):
"""
Implementation of the bisection algorithm
f real valued function
a,b interval boundaries (float) with the property
f(a) * f(b) <= 0
tol tolerance (float)
"""
if f(a) * f(b)> 0:
raise ValueError("Incorrect initial interval [a, b]")
for i in range(100):
c = (a + b) / 2.
if f(a) * f(c) <= 0:
b = c
else:
a = c
if abs(a - b) < tol:
return (a + b) / 2
raise Exception('No root found within the given tolerance {tol}')
We assume this to be stored in a file named bisection...