Skip to content

Instantly share code, notes, and snippets.

@mentix02
Last active March 5, 2020 09:19
Show Gist options
  • Save mentix02/4a094e1d8c84f721e98b51fd5c905a52 to your computer and use it in GitHub Desktop.
Save mentix02/4a094e1d8c84f721e98b51fd5c905a52 to your computer and use it in GitHub Desktop.
Newton's Method implemented in Python
def newton(x0, f, f_, n = 1, r=7):
for i in range(n):
xi = x0 - (f(x0) / f_(x0))
print(f'x{i+1} =', round(xi, r))
x0 = xi
"""
Works for arbitrarily precise floating points.
Usage - calculating √169 (13).
def f(x):
return x**2 - 169
def f_(x):
return 2*x
newton(12, f, f_, 5, 7)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment