算法之【牛顿迭代法】
众所周知,计算机的基本数值算法是加减乘除,甚至只是加减法。而次方和开根算法都是由四则运算混合表示而成的,因而根号计算比四则运算要慢很多。无理数如√2的浮点数计算就是由牛顿迭代法得出的。牛顿迭代法是一种用于计算曲线方程根的精确算法(尤其是幂函数方程),比二分法更加高效,因为它基于微分。
As we all know, basic numerical calculation in the computer is: addition, subtraction, multiplication and division.
Or even only the addition and subtraction. But the power and root algorithm is a complex combination of the four fundamental operations, so they are much slower. Irrational numbers such as √2 whose floating point is obtained by the Newton-Raphson method. Newton-Raphson method is a precise algorithm used to calculate the curvilinear equation (especiallypower function), it’s more efficient than Dichotomy because it is based on the differential.
以计算√x(精度e)为例的c语言函数:
A example using C language calculating √x with precision ’e’:
double abs_value(double x)
{
if(x<0)x=-x;
return x;
}
double sqrt_root(double x,double e)
{
double x0=1;
while(abs_value(x0*x0-x)>e)
X0=(x0+x/x0)/2;
return x0;
}