In: Computer Science
Using Python, write a program that finds a root of function f(x) = x^2-4x+3 on interval [2,5] using bisection search method. Recall that r is a root of a function if f(r) = 0. Output the number of iterations it took your algorithm to find a root.
def f(n):
return (n*n)-(4*n)+3
def bisection_method(a,b):
count = 0
if(f(a)*f(b)>=0):
print("Wrong intervals")
return
while((b-a)>0.01):
#count for no of iterations
count+=1
#taking c as middle point of the intervals
c=(b+a)/2
if(f(c)==0):
print("No.of Iternations:",count)
return c
if(f(a)*f(c)<0):
b=c
else:
a=c
print("No.of Iternations:",count)
return c
if __name__=="__main__":
a=2
b=5
print("Root is",bisection_method(a,b))