Realization of sqrt () square root calculation function 2--Newton iterative method

Source: Internet
Author: User

Newton Iterative Method:

Newton's Iterative method, also known as Newton-Raphson Method , is a method proposed by Newton in 17th century to approximate the equations on the real and complex domains. Most equations do not exist to find the root formula, so it is very difficult, even impossible, to find the exact root, thus finding the approximate root of the equation is particularly important. The method uses the first few items of the Taylor series of the function f (x) to find the root of the equation f (x) = 0. Newton Iterative method is one of the important methods to find the root of the equation, and its greatest advantage is that it has a square convergence near the single root of the equation f (x) = 0, and the method can be used to find the root and complex root of the equation at this time, but the linear convergence can be changed by some methods. In addition, the method is widely used in computer programming.

Newton's iterative formula:

Set R is the root of f (x) = 0, select x0 as the R initial approximation, over point (x0,f (x0)) to do curve y = f (x) tangent l,l equation for y = f (x0) +f ' (x0) (x-x0), to find the L and x axis intersection x1 = x0-f (x0)/F ' (x0), The X1 is called an approximate value of R. Crossing point (X1,f (x1)) makes the tangent of the curve y = f (x) and asks the tangent to the x-axis intersection x2 = x1-f (x1)/F ' (x1), which is called the two approximation of R. Repeat the above process to get an approximate sequence of R, where X (n+1) =x (n)-f (x (n))/f (x (n)), called the n+1 approximation of R, is called Newton iterative formula .

Newton iterative method to find square root:

The square root in Newton's iterative formula, F (x) =x^2-a, then F ' (x) =2x. The Newton iterative formula above becomes: X (n+1) =x (n)-(x (n) ^2-a)/2x, i.e. (x (n) +a/x (n))/2. We randomly guessed a number r, assuming that R is the root of f (x) =0, after several Newton iterative formulas (the above formula) the resulting x value is the root of f (x) =0 or its very exact approximate value.

For example: I would like to ask how much the root 2 equals. If I guessed the result to be 4, although the wrong is outrageous, but you can see the use of Newton iterative method after the value quickly approaching the root of the square root 2:
(4 + 2/4)/2 = 2.25
(2.25 + 2/2.25)/2 = 1.56944..
(1.56944..+ 2/1.56944 ...) /2 = 1.42189..
(1.42189..+ 2/1.42189 ...) /2 = 1.41423..

A picture of the theft is shown in http://www.2cto.com/kf/201206/137256.html.

..

Program implementation:

#include <iostream>

#include <math.h>

using namespace Std;

Double sqrtnt (double a,double b)

{

Double x,last;

X=b;

if (a<=0)

{

return A;

}

while (x*x!=a&& (ABS (last-x) >0.0000001))

{

Last=x;

x= (x+a/x)/2;

}

return x;

}

int main ()

{

COUT<<SQRTNT (93273,5) <<endl;

return 0;

}

Realization of sqrt () square root calculation function 2--Newton iterative method

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.