Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168693
Image: ubuntu2004
def secant_find_root(func, x0, x1, num_iterations=10, full_output=True): """ Use Secant method to find a root of ``func`` with initial guesses x0, and x1 where ``func`` is a function in one variable. """ x0 = float(x0) x1 = float(x1) for i in range (num_iterations): x2 = x1 - (f(x1)*(x1-x0))/(f(x1) - f(x0)) x1, x0 = x2, x1 if full_output == True: print float(x2) return x2
f(x) = x^2 - 2 secant_find_root(f,10, 5)
3.46666666667 2.28346456693 1.72448420668 1.48150560738 1.42072598049 1.41436456133 1.41421390923 1.41421356239 1.41421356237 1.41421356237 1.41421356237310