Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168693
Image: ubuntu2004
def method_of_false_position_find_root(f, a, b, num_iterations=10, full_output=True): """ Use Method of False Position to 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. page 67 of Timothy Sauer's Numerical Analysis text """ #convert to floats a = float(a); b = float(b) if a > b: a, b = b, a for i in range (num_iterations): c = (f(b)*a - f(a)*b)/(f(b) - f(a)) 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 method_of_false_position_find_root(f, 0, 10,20)
0.2 0.392156862745 0.569811320755 0.728311317387 0.865291181364 0.98045341223 1.07504978885 1.15128131535 1.21177224136 1.25918740674 1.29599708578 1.32436036803 1.34608959667 1.36266295405 1.3752612045 1.38481321543 1.39204147266 1.39750322756 1.40162559788 1.40473442504 1.40473442503999