Using Newton iterative method to find square root

Source: Internet
Author: User

To find the square root of N, first if a value X0 = 1, and then according to the following formula to find X1, and then X1 into the formula to the right, continue to find X2 ... The square root of n can be obtained after the effective iteration, xk+1

Let us first verify the accuracy of this ingenious method to 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
...

As can be seen, as the number of iterations is added, the value of the operation becomes closer to the real value. It's a wonderful algorithm, but how does it come about? Check out Wikipedia and Wolfram, the original algorithm is called Newton's Iteration (Newton iterative method).

The following is the extremely つまらない (boring) mathematical introduction, do not like the meaning of mathematics is that most people can skip.
Simple derivation

If f (x) is a function of x:



Find the first-order guide of F (x), that is, the slope:



Simplify the equation to get:



Then, using the resulting iterative algorithm to find a more accurate and comfortable value, why can we use iterative method? The reason is the median theorem (intermediate value theorem):

Assuming that the F function is contiguous within the closed interval [a, b], there must be a point x so that f (x) = C,c is the point of the function f in the closed interval [a, b]

Let's start with an X initial value, like 1, and of course the Earth knows that the square root of any number except 1 itself will not be 1. It then takes the initial value, advances through iterative operations, and steps closer to the exact value until it gets the value that we feel is more comfortable. For example, a square root of 768 is required, because 252 = 625, and 302 = 900, we can first put a value of 26, and then iterate the operation to get a more accurate value: 27.7128.

Back to our first "inexplicable" formula, we asked for the square root of n, making x2 = n, if a function f (x) about X is:

F (X) = X2-n

To find the first order of F (X):

F ' (X) = 2X

In the last-found-in-the-box:

Xk+1 = Xk-(xk2-n)/2xk

Simplification is the wonderful formula for the square root that we have originally mentioned:


Deduce with Taylor formula

I have previously introduced the algorithm that works in the Art and science of C to find the square root of the Taylor formula, in fact Newton's iterative method can also be seen as the simplification of Taylor's formula (Taylor Series), first recall the Taylor formula:



Keep only the first two items to the right of the equation:



Make f (x0+ε) = 0, get:



Again make X1 = X0 +ε0, get ε1 ... And so forth:



Translate to:


Extended

In fact, Newton's iterative method can be used not only to find square roots, but also to find cubic roots and even more complex operations.

In the same way, we can also use C to implement the simplest formula for the square root (although we can use sqrt () to complete)
#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);
}

Using Newton iterative method to find square root

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.