Computing square roots of numbers with Newton's method(Implementation in python)
Newton's method
In this short article I will introduce you to how you can write a small python script that calculates square root of any number you give provided that your initial guess converges. You will also gain bragging rights on knowing how those smart guys calculate square roots of numbers in your calculator.
From calculus we know that we can approximate functions by linearisation with the gradient of the said function and an example of this is the Taylor expansion series. In this article we will talk about the Newton's method which is used for estimating a solution of an equation of the form . I will explain how this works in the subsequent paragraph.
From the equaltion of the line we know that or maybe you're more familiar with the equation of a slope given by . In Newton's method, we set to zero and solve for to obtain . Where a is our initial guess. Subsequently we plug the obtained as a and do this repeatedly till our solution gets close enough to zero otherwise we assume the initial guess was not good enough and it did not converge.
Example
Find the square root of . We will apply Newton's method analytically below with an initial guess of 1.
In equation 2, we rewrite the square root to the form . We calculate the derivative to obtain . We evaluate the derivative at point 1 to get 2 and plug it into the equation for x. This was how we obtained . If we plug this value into equation 2 we get 0.25 which is quite bigger than 0 so we repeat the process till we get a number satisfactorily close to zero.
The code implemented in python can be seen below.
x
from scipy.misc import derivative
# Newtons method
def f(x, root):
return x ** 2 - root
def newtons_method(root, guess):
x = guess
guessed = guess
counter = 0
while abs(guessed) > 0.0001 and counter < 1000:
d = derivative(f, x, args=(root,))
guessed = f(x, root)
x = ((x * d) - guessed) / d
counter += 1
result = f"The square root is {round(x, 4)}" if counter < 1000 else f"The square root is {round(x, 4)} but maybe did not converge."
print(result)
newtons_method(2, 1)
An external library scipy is used to calculate the derivative. I have checked that the number is greater than 0.0001 to stop the iteration. I also make sure I don't make over 1000 iterations. These are all arbitrary numbers and you can play around with them and see what gets the best result you want. For the , the algorithm returns 1.4142 which is more or less what your calculator would return and I think under the hood this is the algorithm that is used for computing square root of numbers.
You can now brag to your friends that you know how square roots are computed by calculators. Thanks for reading.
Comments
Post a Comment