Last active
March 5, 2020 09:19
-
-
Save mentix02/4a094e1d8c84f721e98b51fd5c905a52 to your computer and use it in GitHub Desktop.
Newton's Method implemented in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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