I have this line of code that finds the root of a function using the Bisection Method. How can I turn this into a recursive function? It needs to output the root of the function.
a=float(input())
b=float(input())
c=float(input())
d=((a)**2)-c
e=((b)**2)-c
if d>0 and e>0:
print ("Error")
if d<0 and e<0:
print ("Error")
f=abs(a-b)
while f>(10**-5):
d=(a**2)-c
e=(b**2)-c
g=(a+b)/2
h=(g**2)-c
if (d*h)>=0:
a=g
else:
b=g
f=abs(a-b)
print (a,b, "or", ((a+b)/2))
Thanks in advance!
I tried this: (But it just kept on returning Error and None)
a=float(input())
b=float(input())
c=float(input())
def f(x):
return (x**2)-c
def bisect(a,b,c):
if f(a)*f(b)>0:
print ("Error")
else:
midpoint=(a+b)/2
if f(midpoint)==0 and (b-a)/2<=(10**(-7)):
return midpoint
if f(a)*f(midpoint)<0 and (b-a)/2<=(10**(-7)):
b=midpoint
return bisect(a,b,c)
else:
a=midpoint
return bisect(a,b,c)
print (bisect(a,b,c))