Created
April 5, 2014 06:11
-
-
Save pdparker/9988036 to your computer and use it in GitHub Desktop.
Newton square root
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
def newton_sqrt(x,s=1., kmax=100, tol=1e-8): | |
""" | |
Uses the newton method to approximate square roots. | |
Tolerence set to | |
""" | |
for k in range(kmax): | |
pre_s = s | |
s = .5 * (s + x/s) | |
if abs(s - pre_s) < tol: | |
break | |
print "Converged after %s iterations" %(k) | |
print "The square root of %s is %20.15f" %(x,s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment