Bisection method

In [5]:
from numpy import exp,sin,linspace,sign,abs
from matplotlib.pyplot import plot,grid,xlabel,ylabel
In [6]:
def fun(x):
    #f = x**2 - 4*x*np.sin(x) + (2*np.sin(x))**2
    f = exp(x) - sin(x)
    return f
In [7]:
x=linspace(-4,-2,100)
f=fun(x)
plot(x,f,'r-')
grid(True)
xlabel('x')
ylabel('f')
Out[7]:
<matplotlib.text.Text at 0x10db64910>
In [8]:
# Initial interval [a,b]
a, b = -4, -2

M=100         # Maximum number of iterations
eps=1.0e-4   # Tolerance on the interval
delta=1.0e-4 # Tolerance on the function

fa = fun(a)
fb = fun(b)
sa = sign(fa)
sb = sign(fb)

for i in range(M):
    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 "{0:5d} {1:16.8e} {2:16.8e} {3:16.8e}".format(i+1,c,np.abs(b-a),np.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