double func(double a, double b, double x) {
double r = a * exp(- x*x) + b * sqrt(x);
return r*r;//return的积分常数,例如这里f(x*x)dx|[0,h]
}
double integrate(double a, double b, double h) {
unsigned long steps = 1, it = 1;
double V = h * (func(a, b, 0) + func(a, b, h)) / 2.0;
double Vold;
do {
double tmp = 0;
steps *= 2;
//cout << V << endl;
for (unsigned long i = 1; i < steps; i += 2) {
tmp += func(a, b, (h * i) / steps);
}
Vold = V;
V = (V / 2.0) + ((tmp * h) / steps);
} while (fabs(V - Vold) > eps);
return V;
}
/*上述模板:func()为积分的函数,integrate()为区间【0,h】上的积分*/
转载于:https://www.cnblogs.com/little-w/p/3379203.html