Routine Name: Bisection
Author: David Merkley
Language: Python
Description/Purpose: Approximates a root in the interval [a, b].
Input: A function, starting endpoint, and ending endpoint
Output: A root
Implementation/Code:
def bisection(f, a, b, maxiter=200):
fa = f(a)
fb = f(b)
if fa > 0 and fb < 0 or fa < 0 and fb > 0:
counter = 0
while counter in range(int(maxiter)):
c = (a + b) / 2
fc = f(c)
if fa * fc < 0:
b = c
else:
fa = fc
a = c
counter = counter + 1
c = (a + b) / 2
return c
else:
print("Values were not opposite signs")