结合之前博客中的代码产生的牛顿法求根函数

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;
}

 

posted @ 2015-02-07 16:53  wu_overflow  阅读(163)  评论(0)    收藏  举报