You are on page 1of 1

Bisection Method

Mohammad Safiuddin
B170765ME
September 28, 2020

[1]: def bisection(f,x_a,x_b,epsilon_max):


'''A function that implements the Bisection method
f : function of x
x_a : lower bound of the interval [x_a,x_b]
x_b : upper bound of the interval [x_a,x_b]
epsilon_max : Maximum Tolerance
'''
if f(x_a)*f(x_b) >=0:
print('Assumption is wrong')
return
i = 1
while abs(x_b - x_a) >= epsilon_max:
x_c = (x_b + x_a)/2
if f(x_c)*f(x_a) < 0:
x_b = x_c
else:
x_a = x_c
print('Iteration: {0}; x_a = {1:.7f} , x_b = {2:.7f} , x_c = {3:.7f} ;␣
,→f(x) = {4}'.format(i,x_a,x_b,x_c,f(x_c)))

i = i+1
print('Root of the given function is {0:.6f}'.format(x_c)) # Approximated to␣
,→6 decimal places

return x_c

[2]: def f(x):


return x**3 + x -1

[3]: root = bisection(f,-1,1,5e-7)

Iteration: 1; x_a = 0.0000000 , x_b = 1.0000000 , x_c = 0.0000000 ; f(x) = -1.0


Iteration: 2; x_a = 0.5000000 , x_b = 1.0000000 , x_c = 0.5000000 ; f(x) =
-0.375
Iteration: 3; x_a = 0.5000000 , x_b = 0.7500000 , x_c = 0.7500000 ; f(x) =
0.171875
Iteration: 4; x_a = 0.6250000 , x_b = 0.7500000 , x_c = 0.6250000 ; f(x) =
-0.130859375
Iteration: 5; x_a = 0.6250000 , x_b = 0.6875000 , x_c = 0.6875000 ; f(x) =

You might also like