# Bisection method:
def Bisection(function, a, b, tolerance_accepted):
def f(x):
f = eval(function)
return f
tolerance = abs(a-b)
i=1
while tolerance > tolerance_accepted:
x = 0.5*(a+b)
if f(a) * f(x) < 0:
tolerance = abs(a-b)
b=x
if i<10:
print(f"Iteration {i}: a = {format(a,".5f")} b = {format(b,".5f")} tolerance =
{format(tolerance,".5f")} x = {format(x,".5f")}")
else:
print(f"Iteration {i}: a = {format(a,".5f")} b = {format(b,".5f")} tolerance =
{format(tolerance,".5f")} x = {format(x,".5f")}")
i+=1
elif f(b) * f(x)<0:
tolerance = abs(a-b)
a=x
if i<10:
print(f"Iteration {i}: a = {format(a,".5f")} b = {format(b,".5f")} tolerance =
{format(tolerance,".5f")} x = {format(x,".5f")}")
else:
print(f"Iteration {i}: a = {format(a,".5f")} b = {format(b,".5f")} tolerance =
{format(tolerance,".5f")} x = {format(x,".5f")}")
i+=1
else:
print("Something went wrong!")
quit()
print(f"The approximate root is: {round(x,5)}")
f = input("f(x) = ")
a = float(input("a = "))
b = float(input("b = "))
t = float(input("t = "))
Bisection(f, a,b,t)