以函数作参数求积分
代码:
#include<iostream>
using namespace std;
double jifen(double start, double end, double jingdu, double pingfang(double a))
{
double pre;
double after;
double outcome = 0;
for (pre = start, after =start + jingdu; after <= end; pre += jingdu, after += jingdu)//注意不是从0,而是从下界开始的
{
outcome = outcome + (pingfang(pre) + pingfang(after)) / 2 * jingdu;
//cout << outcome;
}
return outcome;
}
double ping(double x)
{
return x*x;
}
double lifang(double x)
{
return x*x*x;
}
int main()
{
double jingdu; cout << "please insert jingdu:" << endl;
cin >> jingdu;
cout << jifen(1, 2, jingdu, lifang);
}
易错点:
积分函数里面的作参数的函数要有参数;
注意积分从下界开始,上届结束;
使用划分梯形的方法来进行累加来求积分;
注意要可以设置精度;