Extra chapter on the growth of programmers - how to understand Newton's iterative method and how to use Newton's iterative method to find the square root of a number

It’s been a long time, friends, I’m here again, this time I’m sharing how to understand Newton’s iterative method and how to use Newton’s iterative method to find the square root of a number

What is Newton's iteration method?

Official language : The Newton iteration method is also known as the Newton-Raphson (Raphson) method (Newton-Raphson method), which is a method proposed by Newton in the 17th century to approximate the solution of equations in the real number field and complex number field.
In short, an algorithm for simulating approximations.

Background generated by Newton's iterative method

Most equations do not have root-finding formulas, so finding exact roots is very difficult, or even unsolvable, so it is particularly important to find approximate roots of equations.

What is the use of Newton's iteration method?

Newton's iterative method can be used to solve scenarios where functions such as arithmetic square roots have second derivatives.

The basic idea of ​​Newton's iteration method? (Take arithmetic square root as an example)

Overview: By continuously making tangents to find the intersection with the x-axis, approaching the zero point infinitely

  1. The constructor y=x^2 - n
    insert image description here
    explains the reason for this construction here. When y=0, the value of x is the required square number, and this function can be derived twice, so the root can be found by Newton iteration method Approximate value of n
  2. After that, take the point C(1, 1-n) close to 0, and then make the tangent of the function through this point to intersect the x-axis at point B(k', 0), and find the point B'(k) on the function when x=k' ',k'^2 - n)
    insert image description here
  3. After infinite iterations, after many iterations, we found that this value will get closer and closer to A(square root n,0)
  4. The value of the limit is the value of the square root n

The code for finding the square root of Newton's iterative method

public static double sqrt(double n) {
    
    
    double threshold= 1e-15;
    double k = 1.0;
    while (Math.abs(k*k - n) > threshold) {
    
    
        k = (k + n/k) / 2.0;
    }
    return k;
}

Proof of finding the square root of Newton's iterative method

  1. The known constructed function is y = x^2 - n
  2. Let C be a point on the function, and the abscissa value of C is set to k, that is, x = k, and y = k^2 - n, that is, C(k,k^2-n)
  3. The slope of the tangent line at point C is set to m = (x^2 - n)' = 2x = 2k
  4. Then there is the intersection point B(k',0) of the tangent line and the abscissa, and m = Δy/Δx, then there is Δx = Δy/m = (k^2 - n)/ 2k, so there is k' = k - Δx = k - (k^2 - n)/ 2k = (k^2 + n)/2k = (k + n/k) / 2, then use the point of x=k' on the function as point C to repeat step 2 , 3, 4
  5. When Δy -> 0, that is, k^2 - n -> 0, that is, when point C is infinitely approaching zero, k is the solution sought, that is, the root number n
  6. Completed

———————————— If you have any questions, please leave a message in the comment area——————————

おすすめ

転載: blog.csdn.net/qq_31236027/article/details/130903954