演算法之【牛頓迭代法】
眾所周知,計算機的基本數值演算法是加減乘除,甚至只是加減法。而次方和開根演算法都是由四則運算混合表示而成的,因而根號計算比四則運算要慢很多。無理數如√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;
}