寻找方程不动点

const double tolerance = 0.00001;

bool CloseEnough (const double &val1
                  ,const double &val2)
{
    return (abs(val1 - val2) < tolerance);
}

double FixedPoint (function<double (const double&)> f
                   , const double &guess)
{
    const auto next = f (guess);
    if (CloseEnough (guess, next)) {
        return next;
    }
    else {
        return FixedPoint (f, next);
    }
}

int main ()
{
    cout << FixedPoint ([] (const double &x)
                        {return cos(x)+ sin(x);}
                        , 1.0);
    cout << endl;
    return 0;
}

 

posted @ 2015-02-05 22:27  wu_overflow  阅读(241)  评论(0编辑  收藏  举报