Bisection Method
import numpy as np import matplotlib.pyplot as plt from math import exp, log, sin
def bisection(a,b,f,err): c = (a+b)/2 while c-a > err: if f(c) == 0: return c elif f(a)*f(c) > 0: a = c else: b = c c = (a+b)/2 return c
x = np.arange(1,5,0.001) y = np.log(x)-np.sin(x) plt.plot(x,y) plt.show()
def f(x): return np.log(x)-np.sin(x)
bisection(1,5,f,0.001)
f(2.2197265625)