Bisection Method

 #Root of x-cos(x) by bisection method

from math import*

def f(x):

return (x-cos(x))

a=0

b=1

x=(a+b)/2

print ("The successive approximation root is ")

while abs(f(x))>0.0000001:

x=(a+b)/2

print (x)

if f(x)<0:

a=x

elif f(x)>0:

b=x

#Correct root is 0.7390851

Ouput Screen:

The successive approximation root is 
0.5
0.75
0.625
0.6875
0.71875
0.734375
0.7421875
0.73828125
0.740234375
0.7392578125
0.73876953125
0.739013671875
0.7391357421875
0.73907470703125
0.739105224609375
0.7390899658203125
0.7390823364257812
0.7390861511230469
0.7390842437744141
0.7390851974487305
0.7390847206115723
0.7390849590301514
0.7390850782394409

Comments