Created
April 12, 2012 12:31
-
-
Save aksiksi/2366925 to your computer and use it in GitHub Desktop.
Newton's Method in Python
This file contains 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
# Finds a root of a number using Newton's method. Outputs the first 5 approximations. | |
def newton(n, x): | |
x1 = x - (x*x-n)/(2*x*x) | |
return x1 | |
root = float(raw_input("Enter a number to approximate the square root of: ")) | |
approx = float(raw_input("Enter approximation for root: ")) | |
print '' | |
for a in range(1, 6): | |
approx = newton(root, approx) | |
print "Approximation #{0}: {1:.7f}".format(a, newton(root, approx)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment