Implementation of Newton Method written in Python.
IN -3 0.000001 OUT X: -3.0 f(x): 2.0 X: -2.5 f(x): 0.25 X: -2.4166666666666665 f(x): 0.006944444444443976 X: -2.4142156862745097 f(x): 6.007304882427178e-06
# ? 2018 TheFlyingKeyboard and released under MIT License # theflyingkeyboard.net def function(x): return x * (x + 2) - 1 def functionDerivative(x): return 2 * x + 2 intervalBegin = float(input("Enter inital guess: ")) precision = float(input("Enter precision of method: ")) x = intervalBegin prevX = intervalBegin while(abs(function(x)) > precision): print("X: ", x, " f(x): ", function(x)) x = prevX - (function(prevX) / functionDerivative(prevX)) prevX = x
Python Newton Method