Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168695
Image: ubuntu2004
def bisection_find_root(f, a, b, num_iterations=10, full_output=True): """ Use Bisection method to numerically find a root of ``f`` on the closed interval `[a,b]` (or `[b,a]`) if possible, where ``f`` is a function in the one variable. """ #convert to floats a = float(a); b = float(b) if a > b: a, b = b, a for i in range (num_iterations): c = (a + b)/2 if f(a)*f(c) < 0: b=c else: a=c if full_output == True: print float(c) return c
f(x) = x^2 - 2 bisection_find_root(f, 1, 2,20)
1.5 1.25 1.375 1.4375 1.40625 1.421875 1.4140625 1.41796875 1.416015625 1.4150390625 1.41455078125 1.41430664062 1.41418457031 1.41424560547 1.41421508789 1.4141998291 1.4142074585 1.41421127319 1.41421318054 1.41421413422 1.4142141342163086