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; }
时间: 2024-10-11 13:01:26