做完啦
#include<iostream>
#include<cmath>
using namespace std;
double integration(double a, double b, double (*fun)(double));
double f(double);
double g(double);
double h(double);
int main(){
double PI=3.14159;
double half_PI=PI/2;
cout<<"函数f(x)在[1,3]上的积分为:"<<integration(1,3,f)<<endl;
cout<<"函数g(x)在[0,1]上的积分为:"<<integration(0,1,g)<<endl;
cout<<"函数h(x)在[0,half_PI]上的积分为:"<<integration(0,half_PI,h)<<endl;
}
double integration(double a, double b, double (*fun)(double x)){
double p=0.000001,s=0,s_m=s,l;
for(int m=1;fabs(s-s_m)<p;m++){
for(int i=0;i<=m;i++){
l=(b-a)/m;
s=s+((*fun)(i*l+a)+(*fun)((i+1)*l+a))*0.5;
}
return s;
}
}
double f(double x){
return 1/x;
}
double g(double x){
return exp(x)/(1+exp(x));
}