积分:变步长梯形法
#include <stdio.h> #include <math.h> #include <iomanip> #define e 2.7182818 double f(double x) { return pow(e,-x*x); } double Calc(double a,double b,double esp) { int done(0); int n=1; double h,Tn,T2n,k,temp,x; h=b-a; Tn=h*(f(a)+f(b))/2.0; while(!done){ temp = 0; for(k=0;k<n;k++){ x=a+(k+0.5)*h; temp+=f(x); } T2n=(Tn+h*temp)/2.0; if(fabs(T2n-Tn)<esp) done=1; else{ Tn=T2n; n=n*2; h=h/2; } } return T2n; } int main() { printf("%.50lf\n",Calc(-10000,10000,1e-50)); return 0; }
代码暂不鲁棒,待修改