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
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close