DEV Community

Cover image for Newton & Square Roots
Abhimanyu Singh Shekhawat
Abhimanyu Singh Shekhawat

Posted on • Edited on • Originally published at abhimanyutimes.com

Newton & Square Roots

The web is an interesting place, it humbles you down and shows how little you actually know. While hitchhiking through the hyperlinks, I had one such realization recently.

What if I ask you to calculate the square root of a number? Depending upon your background you are likely to come up with a variety of solutions. Let me talk about that specific fraction of you who will be interested in churning out code for doing so. [No, sqrt()]

Binary Search seems to be a valid and intuitive approach, after all, we can easily discard half of the search space in each iteration. But I want you to hold that thought and glance over this small snippet below.

double sq(double num)
{
    double xn = num/2;
    while(abs((xn*xn)-num)>eps) //eps is precision
    {
        xn = (xn + num/xn)/2;
    }
    return xn;
}
Enter fullscreen mode Exit fullscreen mode

Magical isn't it? When I first looked at this, I was really startled by the elegance of this solution. So what is it and why does it work?

Sweet Mathematics

You just witnessed the glamour of Newton Raphson method.

In simple terms, it is a way to quickly find a good approximation for the roots of a real-valued function f(x) = 0.

This is our cue, square root for a number can be minimally represented by solving for the roots of f(x) = x2 - N, where N is the number we have to find the root of.

That is all cool! But how does it actually work? Let's look at the simple math behind this:-

  1. Consider a general function: y = f(x)

    We start by choosing an arbitrary value x1 as our starting point[1]. For our specific case, we can choose N/2 as our starting point and can safely discard values greater than that from the candidature of √N.

  2. Now let's draw a tangent (Differential) to the curve at that point and mark its intersection with the x-axis. Let's call this x2.

  3. Repeat step 2 for point x2 to obtain x3. You must have noticed that with each iteration we are nearing the roots of the quadratic equation. Slow and steady!

    Depending upon the precision, you can decide upon the number of iterations required.

Formal Expression

Let's try to workout an elegant formula for f(x) = x2 - N which will get us the compact code we saw in the beginning. Please note that we are aiming to get a relation between Nth and (N+1)th iteration.

  • For any point xN, by using the equation of line we can get:
    f'(xN) = (f(xN+1) - f(xN))/(xN+1 - xN)
    [f'(x) being the slope at point x]

  • Putting 0 for y coordinate at point xN+1 where tangent crosses x-axis we get:
    f'(xN) = (- f(xN))/(xN+1- xN)

  • Rearranging the terms & putting f'(x) = 2xN we have our final expression:
    xN+1 = (xN + N/xN)/2

Visualising Newton Raphson method for N = 10000

Notes

[1] For the sake of simplicity, I have omitted the details about choosing the initial value which plays a crucial role in more complex functions.

Special thanks to AMSI for facilitating illustrations


If you liked this post, you can find more by:

Following me on Twitter: @abshekha

Connecting on LinkedIn: abhimanyubitsgoa

Visiting my website: https://abhimanyutimes.com


Thanks for reading!

Top comments (2)

Collapse
 
nickgrim profile image
Nick Grimshaw

I think you've got a typo in the code at the top. I think:

while((abs(xn*xn)-num)>eps)
Enter fullscreen mode Exit fullscreen mode

should be:

while(abs((xn*xn)-num)>eps)
Enter fullscreen mode Exit fullscreen mode

Compare the behaviours of the two versions with num = 2.

Collapse
 
abhimanyu profile image
Abhimanyu Singh Shekhawat

Thanks a lot for pointing that out. I have corrected it.