Post · 4 min read· pp. 442–444

Newton's method for finding roots

This is an iterative method invented by Isaac Newton around 1664. However, this method is also sometimes called the Raphson method, since Raphson invented the same algorithm a few years after Newton, but his article was published much earlier.

The task is as follows. Given the following equation:

f(x)=0f(x) = 0

We want to solve the equation. More precisely, we want to find one of its roots (it is assumed that the root exists). It is assumed that f(x)f(x) is continuous and differentiable on an interval [a,b][a, b].

Algorithm

The input parameters of the algorithm consist of not only the function f(x)f(x) but also the initial approximation - some x0x_0, with which the algorithm starts.

plot_f(x)

Suppose we have already calculated xix_i, calculate xi+1x_{i+1} as follows. Draw the tangent to the graph of the function f(x)f(x) at the point x=xix = x_i, and find the point of intersection of this tangent with the xx-axis. xi+1x_{i+1} is set equal to the xx-coordinate of the point found, and we repeat the whole process from the beginning.

It is not difficult to obtain the following formula,

xi+1=xi−f(xi)f′(xi)x_{i+1} = x_i - \frac{f(x_i)}{f^\prime(x_i)}

First, we calculate the slope f′(x)f'(x), derivative of f(x)f(x), and then determine the equation of the tangent which is,

y−f(xi)=f′(xi)(x−xi)y - f(x_i) = f'(x_i)(x - x_i)

The tangent intersects with the x-axis at coordinate, y=0y = 0 and x=xi+1x = x_{i+1},

−f(xi)=f′(xi)(xi+1−xi)- f(x_i) = f'(x_i)(x_{i+1} - x_i)

Now, solving the equation we get the value of xi+1x_{i+1}.

It is intuitively clear that if the function f(x)f(x) is "good" (smooth), and xix_i is close enough to the root, then xi+1x_{i+1} will be even closer to the desired root.

The rate of convergence is quadratic, which, conditionally speaking, means that the number of exact digits in the approximate value xix_i doubles with each iteration.

Application for calculating the square root

Let's use the calculation of square root as an example of Newton's method.

If we substitute f(x)=x2−nf(x) = x^2 - n, then after simplifying the expression, we get:

xi+1=xi+nxi2x_{i+1} = \frac{x_i + \frac{n}{x_i}}{2}

The first typical variant of the problem is when a rational number nn is given, and its root must be calculated with some accuracy eps:

text
double sqrt_newton(double n) {
    const double eps = 1E-15;
    double x = 1;
    for (;;) {
        double nx = (x + n / x) / 2;
        if (abs(x - nx) < eps)
            break;
        x = nx;
    }
    return x;
}

Another common variant of the problem is when we need to calculate the integer root (for the given non-negative nn find the largest xx such that x2≤nx^2 \le n). Here it is necessary to slightly change the termination condition of the algorithm, since it may happen that xx will start to "jump" near the answer. Therefore, we add a condition that if the value xx has decreased in the previous step, and it tries to increase at the current step, then the algorithm must be stopped.

text
int isqrt_newton(int n) {
    if (n == 0)
        return 0;
    int x = 1;
    bool decreased = false;
    for (;;) {
        int nx = (x + 1LL * n / x) >> 1;
        if (x == nx || nx > x && decreased)
            break;
        decreased = nx < x;
        x = nx;
    }
    return x;
}

Finally, we are given the third variant - for the case of bignum arithmetic. Since the number nn can be large enough, it makes sense to pay attention to the initial approximation. Obviously, the closer it is to the root, the faster the result will be achieved. It is simple enough and effective to take the initial approximation as the number 2bits/22^{\textrm{bits}/2}, where bits\textrm{bits} is the number of bits in the number nn. Here is the Java code that demonstrates this variant:

text
public static BigInteger isqrtNewton(BigInteger n) {
    if (n.signum() == 0)
        return BigInteger.ZERO;
    BigInteger a = BigInteger.ONE.shiftLeft(n.bitLength() / 2);
    boolean p_dec = false;
    for (;;) {
        BigInteger b = n.divide(a).add(a).shiftRight(1);
        if (a.compareTo(b) == 0 || a.compareTo(b) < 0 && p_dec)
            break;
        p_dec = a.compareTo(b) > 0;
        a = b;
    }
    return a;
}

For example, this code is executed in 6060 milliseconds for n=101000n = 10^{1000}, and if we remove the improved selection of the initial approximation (just starting with 11), then it will be executed in about 120120 milliseconds.

Practice Problems