#Program to find root of a function by Bisection method (y=x^2-x-2):
def f(x):
y=x*x-x-2
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 in the entered limits.")
a=input("Enter the new lower limit:")
b=input("Enter the new upper limit:")
a=float(a)
b=float(b)
while(f(a)*f(b))<0:
c=(a+b)/2.0
if abs(f(c))<0.0000001:
break
if f(a)*f(c)<0:
b=c
else:
a=c
print("The root of the given function is:",c)
def f(x):
y=x*x-x-2
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 in the entered limits.")
a=input("Enter the new lower limit:")
b=input("Enter the new upper limit:")
a=float(a)
b=float(b)
while(f(a)*f(b))<0:
c=(a+b)/2.0
if abs(f(c))<0.0000001:
break
if f(a)*f(c)<0:
b=c
else:
a=c
print("The root of the given function is:",c)
Comments
Post a Comment