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:
3 |
while ( abs (k*k-n)>1e-9) { |
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:
- Assume y for the square root of X.
- 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:
08 |
printf ( "Enter a number that requires the square root of the parameter :" ); |
11 |
// X (k + 1) = 0.5 [x (k) + N/X (k)] |
12 |
while ( fabs ((1.0/2.0*(y+n/y))-y)>=0.00001) |
15 |
printf ( "y=%lf\n" , y ); |
17 |
printf ( "The square root is % F \ n" ,y); |
Program running result:
01 |
Enter a number that requires the square root: 2 |
05 |
The square root is 1.414216. |
07 |
Enter a number that requires the square root: 3 |
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 ) |
05 |
const float threehalfs = 1.5F; |
09 |
i = 0x5f3759df - ( i >> 1 ); |
11 |
y = y * ( threehalfs - ( x2 * y * y ) ); |
12 |
// y = y * ( threehalfs - ( x2 * y * y ) ); |
How to Use the Newton method to find the square root of a number