Newton-Raphson Method
Introduction
Given a function $f$, sometimes we would like to find one of its roots, i.e., the value of x for which $f(x) = 0$. Computing its analytical root might be difficult, but we could use some approximate algorithms to find an approximate root.
In this blog post, I would like to discuss the Newton-Raphson Method, which is commonly used for finding the approximate root of a given function.
Newton-Raphson Method
The Newton-Raphson method is simple. Given a function $f$, its derivative $f’$, and an initial guess $x_0$, the approximate root after $n$ iterations is
$$
x_{1} = x_0 - \frac{f(x_0)}{f^{\prime}(x_0)}
$$
$$
x_{n+1} = x_n - \frac{f(x_n)}{f^{\prime}(x_n)}
$$
To “animate” the iterative process, we have
The derivation of the update function is also simple. At any iteration $n$, the function of the line is
$$
y = f^{\prime}(x_n)(x - x_n) + f(x_n)
$$
When $y = 0$,
$$
x = x_n - \frac{f(x_n)}{f^{\prime}(x_n)}
$$
Newton-Raphson Method VS Binary Search Method
There is another conventional algorithm that could be used for root searching, which is the binary search method. Here, we have implemented both the Newton-Raphson method and the binary search method for finding the root of a specific function.
The Newton-Raphson method and the binary search method have slightly different input signatures, which might be applied to different scenarios. Although without rigorous proof, it seems that the convergence of the Newton-Raphson method is faster than the binary search method.
1 | from typing import Callable |
To execute the program, we run the following command in the terminal.
1 | $ python find_root.py |
In order to find one of the roots of $f(x) = x^2 - 4x - 7$, we could see from the example above that despite the difference in input signature it took 4 iterations and 50 iterations for the Newton-Raphson method and the binary search method, respectively.
Newton-Raphson Method for Multiple Variables
1 | from typing import Callable |
To execute the program, we run the following command in the terminal.
1 | $ python find_multivariate_root.py |
The Newton-Raphson method still worked very well for finding the root of the two-variable function $f(x_1, x_2) = x_1^2 - x_1 x_2 + x_2^2 - 6$. In principle, it could also be generalized to finding the root of functions that has more variables.
Caveats
No algorithm will always work, so does the Newton-Raphson method. There are scenarios where the Newton-Raphson method fails. If interested, please check the Wikipedia on this topic.
References
Newton-Raphson Method