Skip to main content

Program to find root by Bisection method for the function y=x-tanh(2x)

#Program to find root by Bisection method for the function y=x-tanh(2x):
from math import*
def f(x):
 y=x-tanh(2*x)
 return y
a=input("Enter the lower limit:")
b=input("Enter the upper limit:")
a=float(a)
b=float(b)
while f(a)*f(b)>0:
 print("The root of the given function does not lie between the entered limits.")
 a=input("Enter the new lower limit:")
 b=input("Enter the new upper limit:")
 a=float(a)
 b=float(b)
s=0
while(f(a)*f(b))<0:
 c=(a+b)/2.0
 if abs(f(c))<0.00000000001:
  break
 if f(a)*f(c)<0:
  b=c
 else:
  a=c
 s=s+1
print ("The root of the given function is=",c,"\nRequired No. of steps:",s)
print ("The value of the function at the point of root is=",f(c))

Comments