f(x) = x^2-35 # roots of this are +sqrt(35) and -sqrt(35) # We know there is a root between x=5 (f is negative) and x=6 (f is positive) a=5 b=6 # bisection n_bisection = 0 while b-a>.000000001: n_bisection += 1 c = (a+b)/2 if f(a)*f(c)>0: a=c else: b=c print('Root is between ',a.n(),' and ',b.n()) print(sqrt(35).n()) print('Number of steps for bisection: ',n_bisection) print('Error bound estimate: ', n(b-a)) # Regula Falsi Method a=5.9 b=6 n_RF = 0 while abs(b-a)>.000000001: n_RF +=1 c = (a*f(b)-b*f(a))/(f(b)-f(a)) if f(c)*f(a)>0: a=c else: b=c print('Root is approximately ',b.n()) print(sqrt(35).n()) print('Number of steps for RF: ',n_RF) print('Error bound estimate: ', abs(b-a).n()) # Secant Method a=5 b=6 n_RF = 0 while abs(b-a)>.000000001: n_RF +=1 c = (a*f(b)-b*f(a))/(f(b)-f(a)) a=b b=c print('Root is approximately ',b.n()) print(sqrt(35).n()) print('Number of steps for Secant: ',n_RF) print('Error bound estimate: ', abs(b-a).n()) ## Newton-Raphson a=5 b=6 n_RF = 0 while abs(b-a)>.000000001: n_RF +=1 c = b - f(b)/(2*b) # 2c is the derivative a=b b=c print('Root is approximately ',b.n()) print(sqrt(35).n()) print('Number of steps for Newton: ',n_RF) print('Error bound estimate: ', abs(b-a).n())