首页 > 代码库 > 定积分(任意函数求积分)

定积分(任意函数求积分)

 1 #define eps 1e-8 2  3 double fun(double x) { 4     /*函数部分*/ 5 } 6  7 double Definite_Integral(double a, double b) { 8     double p = eps + 1.0; 9     double t, h = b - a, t1 = (fun(a) + fun(b)) * h / 2;10     while(p >= eps) {11         double s = 0;12         for(double k = a + h / 2; k <= b; k += h)s += fun(k);13         t = t1 / 2 + h * s / 2;14         p = fabs(t - t1);15         t1 = t;16         h /= 2;17     }18     return t;19 }