Implementation of Secant Method written in Python.
IN -3 -1 0.0000001 OUT X: -3.0 f(x): 2.0 X: -2.0 f(x): -1.0 X: -3.0 f(x): 2.0 X: -2.3333333333333335 f(x): -0.22222222222222188 X: -2.4 f(x): -0.04000000000000026 X: -2.4146341463414633 f(x): 0.0011897679952406204 X: -2.41421143847487 f(x): -6.007286838749515e-06
# ? 2018 TheFlyingKeyboard and released under MIT License # theflyingkeyboard.net def function(x): return x * (x + 2) - 1 xn1 = float(input("Enter begining of interval: ")) xn = float(input("Enter end of interval: ")) precision = float(input("Enter precision of method: ")) x = xn1 if(function(xn1) * function(xn) < 0): while(abs(function(x)) > precision): print("X: ", x, " f(x): ", function(x)) x = xn - ((function(xn) * (xn - xn1)) / (function(xn) - function(xn1))) xn1 = xn xn = x
Python Secant Method