How to Use the Newton method to find the square root of a number

Source: Internet
Author: User

An exercise for Scip 1.1.7.

The Newton's method is also called the Newton-Raphson method ), it is an approximate method proposed by Newton in the 17th century to solve equations in real and complex fields. Most equations do not have root formulas, so it is very difficult or even impossible to find the exact root, so it is particularly important to find the approximate root of the equation. Methods The first few items of the Taylor series of function f (x) are used to find the root of the equation f (x) = 0. The old brand entertainment city with Newton iteration method is one of the important methods for finding the root of equations. Its biggest advantage is that it has square convergence near a single root of equation f (x) = 0, this method can also be used to obtain the multiple root and Compound Root of the equation. In addition, this method is widely used in computer programming.

Set R to the root of f (x) = 0, and select x0 as the initial approximate value of R. The curve y = f (x) is generated based on the point (x0, F (x0) the tangent of L, the equation of L is y = f (x0) + f' (x0) (x-x0), and obtain the X coordinate X1 = x0-f (x0) of the intersection of L and the X axis) /F' (x0), that is, X1 is an approximate value of R.

Through point (x1, F (X1) to do the curve y = f (x) tangent, and find the X coordinate X2 = x1-f (X1) /F' (X1), called X2 is the quadratic approximation of R. Repeat the above process to obtain the Approximate sequence of R, where x (n + 1) = x (n)-f (x (n)/F' (x (n )), n + 1 approximation of R. The above formula is called the Newton iteration formula.

According to the concept of Newton iteration, the following iteration formula can be obtained: x (n + 1) = [x (n) + P/XN]/2

The general programming method is as follows:

1 double sqr(double n) {
2     double k=1.0;
3     while(abs(k*k-n)>1e-9) {
4         k=(k+n/k)/2;
5     }
6     return k;
7 }

Evaluate the square root of N. First, take a number not 0 as the x (0) at the beginning of iteration. For example, the simplest x (0) = 1, then, x (k + 1) = 0.5 [x (k) + N/X (k)] is repeatedly substituted to obtain the next X. The more times the number of substitution, the more precise the reduction.

For example, the square root of 2:

  • X (0) = 1
  • X (1) = (1/2) (1 + 2/1) = 3/2 = 1.5
  • X (2) = (1/2) [3/2 + 2/(3/2)] = 17/12 = 1.41666667
  • X (3) = (1/2) [17/12 + 2/(17/12)] = 577/408 = 1.41421568...

In this way, the obtained values are more and more accurate after repeated input.

Or the following explanation:

  1. Assume y for the square root of X.
  2. Perform a simple operation to get a better guess: you only need to obtain the average values of Y and x/y (which is closer to the actual square root value ).

For example, you can use this method to calculate the square root of 2.

Conjecture Vendors Average Value
1 2/1 = 2 (2 + 1)/2 = 1.5
1.5 2/1. 5 = 1.3333 (1.3333 + 1.5)/2 = 1.4167
1.4167 2/1. 4167 = 1.4118 (1.4167 + 1.4118)/2 = 1.4142
1.4142 ... ...

Continuing this computation process, we can get an ever better approximation of the square root of 2.

The following is implemented in C language:

01 #include "stdio.h"
02 #include "math.h"
03  
04 int main(void)
05 {
06     double n,y=1.0;
07  
08     printf("Enter a number that requires the square root of the parameter :");
09     scanf("%lf",&n);
10  
11     // X (k + 1) = 0.5 [x (k) + N/X (k)]
12     while(fabs((1.0/2.0*(y+n/y))-y)>=0.00001)
13     {
14         y=1.0/2.0*(y+n/y);
15         printf"y=%lf\n", y );
16     }
17     printf("The square root is % F \ n",y);
18     return 0;
19 }

Program running result:

01 Enter a number that requires the square root: 2
02 y=1.500000
03 y=1.416667
04 y=1.414216
05 The square root is 1.414216.
06  
07 Enter a number that requires the square root: 3
08 y=2.000000
09 y=1.750000
10 y=1.732143
11 y=1.732051
12 The square root is 1.732051.

PS: After quake III exposes the source code, someone found this code in game/code/q_math.c. The function of this function is to split a data square and take it down. After testing, the code ratio (float) (1.0/SQRT (X) is 4 times faster. If you are interested, you can study it. But that's the end,

View Source print?
01 float Q_rsqrt( float number )
02 {
03     long i;
04     float x2, y;
05     const float threehalfs = 1.5F;
06     x2 = number * 0.5F;
07     y  = number;
08     i  = * ( long * ) &y;       
09     i  = 0x5f3759df - ( i >> 1 );
10     y  = * ( float * ) &i;
11     y  = y * ( threehalfs - ( x2 * y * y ) );
12     // y  = y * ( threehalfs - ( x2 * y * y ) );
13     #ifndef Q3_VM
14     #ifdef __linux__
15     assert( !isnan(y) );
16     #endif
17     #endif
18     return y;
19 }

How to Use the Newton method to find the square root of a number

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.