Calculate the square root using Newton Iteration Method

Source: Internet
Author: User

Evaluate the square root of N. Assume that x 0 = 1 is the predicted value. Then, calculate the X1 according to the following formula, then substitute X 1 into the right of the formula, and continue to obtain the X2... After an effective iteration, the square root of N can be obtained, XK + 1.

Let's verify the accuracy of this clever method and calculate the square root of 2 (computed by mathomatic)
1-> x_new = (x_old + Y/x_old)/2
Y
(X_old + -----)
X_old
#1: x_new = ---------------
2
1-> calculate x_old 1
Enter Y: 2
Enter initial x_old: 1
X_new = 1.5
1-> calculate x_old 2
Enter Y: 2
Enter initial x_old: 1
X_new = 1.4166666666667
1-> calculate x_old 3
Enter Y: 2
Enter initial x_old: 1
X_new = 1.4142156862745
1-> calculate x_old 10
Enter Y: 2
Enter initial x_old: 1
Convergence reached after 6 iterations.
X_new = 1.4142135623731
...

It can be seen that as the number of iterations increases, the calculation value will become closer to the actual value. Amazing algorithms, but how come they come? I checked Wikipedia and wolfram. The original algorithm is called Newton's iteration (Newton Iteration Method ).

The following is a mathematical introduction of boring. I don't like the meaning of mathematics, that is, most people can skip it.
Simple Derivation

Suppose f (x) is a function about X:

Find the first derivative of f (x), that is, the slope:

Simplified equation:

Then, the final formula is used for iterative calculation until a satisfactory value is obtained. Why can iterative calculation be used? The reason is Intermediate Value Theorem ):

If the f function is continuous in the closed range [a, B], there must be a bit x so that f (x) = C, and C is a bit of the function F in the closed range [a, B ].

Let's first guess an initial value of X, for example, 1. Of course, the Earth knows that the square root of any number except 1 itself will not be 1. Then, the initial values are substituted, and iterative operations are carried forward, gradually approaching the exact value until we obtain the value that we consider to be satisfactory. For example, the square root of 768 is required. Because 252 = 625 and 302 = 900, We can first generate a prediction value of 26 and then iterate to get a more accurate value: 27.7128.

Return to the "inexplicable" formula at the beginning. We need the square root of N, so X2 = n. Assume that the F (x) function of X is:

F (x) = x2-n

Evaluate the first order of f (x) as follows:

F' (x) = 2x

In the final formula obtained above:

XK + 1 = XK-(xk2-N)/2xk

Simplification gives us the magic formula for finding the square root we mentioned at first:


Derivation Using Taylor Formula

I have previously introduced the use of the Taylor formula to calculate the square root algorithm in the art and science of C. In fact, the Newton iteration method can also be seen as the simplification of the Taylor Formula (Taylor series, let's review Taylor's formula:

Only the first two items on the right of the equation are retained:

Make F (x0 + ε) = 0, and get:

Then, Let X1 = x0 + ε 0 and get ε 1... We can see that:

Convert:

Extended

From the derivation, in fact, the Newton iteration method can be used not only to obtain the square root, but also to obtain the cube root, or even more complex operations.

Similarly, we can use the C language to implement the simplest formula for finding the square root (although we can directly use SQRT)
# Include <stdio. h>
# Include <math. h>
# Define n 768
Main (){
Float x = 1;
Int I;
For (I = 1; I <= 1000; I ++) {// recursion times: 1000
X = (x + N/X)/2;
}
Printf ("the square root of % d is % F/N", n, x );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.