We first import the modules and functions that we need.
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
from numpy import exp,sin,linspace,sign,abs
from matplotlib.pyplot import plot,grid,xlabel,ylabel
First we define the function for which the root is required.
def fun(x):
#f = x**2 - 4*x*sin(x) + (2*sin(x))**2
f = exp(x) - sin(x)
return f
Let us plot and visualize the function.
x = linspace(-4,-2,100)
f = fun(x)
plot(x,f,'r-')
grid(True)
xlabel('x')
ylabel('f');
From the figure, we see that $[-4,-2]$ is a bracketing interval. We now implement the bisection method.
# Initial interval [a,b]
a, b = -4.0, -2.0
N = 100 # Maximum number of iterations
eps = 1.0e-4 # Tolerance on the interval
delta = 1.0e-4 # Tolerance on the function
fa, fb = fun(a), fun(b)
sa, sb = sign(fa), sign(fb)
for i in range(N):
e = b-a
c = a + 0.5*e
if abs(e) < eps*abs(c):
print("Interval size is below tolerance\n")
break
fc = fun(c)
if abs(fc) < delta:
print("Function value is below tolerance\n")
break
sc = sign(fc)
if sa != sc:
b = c
fb = fc
sb = sc
else:
a = c
fa = fc
sa = sc
print("%5d %16.8e %16.8e %16.8e"%(i+1,c,abs(b-a),abs(fc)))
1 -3.00000000e+00 1.00000000e+00 1.90907076e-01 2 -3.50000000e+00 5.00000000e-01 3.20585844e-01 3 -3.25000000e+00 2.50000000e-01 6.94209267e-02 4 -3.12500000e+00 1.25000000e-01 6.05288259e-02 5 -3.18750000e+00 6.25000000e-02 4.61629389e-03 6 -3.15625000e+00 3.12500000e-02 2.79283147e-02 7 -3.17187500e+00 1.56250000e-02 1.16471966e-02 8 -3.17968750e+00 7.81250000e-03 3.51301957e-03 9 -3.18359375e+00 3.90625000e-03 5.52273640e-04 10 -3.18164062e+00 1.95312500e-03 1.48021741e-03 11 -3.18261719e+00 9.76562500e-04 4.63932552e-04 Function value is below tolerance