标签:
cdouble DX = 0.00001;
dFun Deriv (dFun g)
{
return [g] ( cdouble &x)
{ auto delta_x = x + DX;
return (g(delta_x) - g(x)) / DX;};
}
dFun NewtonTransForm (dFun g)
{
return [g] (cdouble &x)
{return (x - (g(x) / (Deriv(g)(x))));};
}
double NewtonsMethod (dFun g, cdouble &guess)
{
return FixedPoint (NewtonTransForm(g), guess);
}
double NewtonsSqrt (cdouble &x)
{
return NewtonsMethod ([x] (cdouble &y)
{return (pow(y, 2) - x);}
, 1.0);
}
int main ()
{
cout << NewtonsSqrt(121.04);
cout << endl;
return 0;
}
标签:
原文地址:http://www.cnblogs.com/wuOverflow/p/4279037.html