Newton's Method
Introduction
This article introduces Newton's method for finding roots, a technique proposed by Newton in the 17th century for approximating solutions to equations.
Specifically, the task is: for a continuous and monotonic function \(f(x)\) on \([a,b]\), find an approximate solution to the equation \(f(x)=0\).
Explanation
Initially, we start with a given \(f(x)\) and an approximate solution \(x_0\) (the choice of initial value relates to Newton fractals; refer to 3Blue1Brown's Newton Fractal).
Suppose our current approximate solution is \(x_i\). We draw the line \(l\) tangent to \(f(x)\) at the point \((x_i,f(x_i))\), and denote the x-coordinate of its intersection with the x-axis as \(x_{i+1}\). This gives a better approximate solution. Repeat this iterative process. Based on the geometric meaning of the derivative, we have:
Rearranging yields the recurrence relation:
Intuitively, if \(f(x)\) is sufficiently smooth, then as the number of iterations increases, \(x_i\) will converge to the true solution.
Newton's method has quadratic convergence, meaning the number of accurate digits approximately doubles with each iteration. For a proof of convergence of Newton's method, refer to citizendium - Newton method Convergence analysis
Of course, Newton's method also has limitations; see Xiaolin Wu - Roots of Equations pages 18 - 20 analysis for details.
Computing Square Roots
We attempt to use Newton's method to compute square roots. Let \(f(x)=x^2-n\), then the approximate solution to this equation is an approximation of \(\sqrt{n}\). Thus we obtain:
When implementing, pay attention to setting an appropriate precision. Code is as follows:
Implementation
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 | |
Computing Integer Square Root
Although we can call the sqrt() function to obtain the square root value, here we discuss a variant of Newton's method for finding the maximum integer solution to the inequality \(x^2\le n\). We still consider a process similar to Newton iteration but with slight modifications to the boundary conditions. If during iteration the approximate solution decreases from the previous step and then increases in the current step, we skip this iteration and exit the loop.
Implementation
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 3 4 5 6 7 8 9 10 | |
High-Precision Square Root
Finally, consider Newton's method for high-precision computation. The iterative method remains unchanged, but we need to pay attention to the initial approximate solution, i.e., the value of \(x_0\). Since numbers requiring high precision are typically very large, the choice of initial value significantly affects algorithm efficiency. A natural idea is to set \(x_0=2^{\left\lfloor\frac{1}{2}\log_2n\right\rfloor}\), which allows quick computation of \(x_0\) while being relatively close to the square root approximation.
Implementation
Java code implementation:
1 2 3 4 5 6 7 8 9 10 11 12 | |
Practical performance: For \(n=10^{1000}\), the runtime of this algorithm is 60 ms. If we do not optimize the initial value \(x_0\) and start iterating directly from \(x_0=1\), the runtime increases to 120 ms.
Exercises
- UVa 10428 - The Roots
-
This page is mainly translated from the blog post Метод Ньютона (касательных) для поиска корней and its English translation Newton's method for finding roots. The Russian version is licensed under Public Domain + Leave a Link; the English version is licensed under CC-BY-SA 4.0.
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用