0% found this document useful (0 votes)
5 views

Bisection Method

Uploaded by

vug46240
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Bisection Method

Uploaded by

vug46240
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

# 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)

You might also like