自适应辛普森 代码
#include<iostream>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
using namespace std;
double f(double x)//函数
{
return 2*x;
}
double simpson(double a,double b)//[a,b]区间simpson
{
double c=a+(b-a)/2.0;
return (f(a)+4*f(c)+f(b))*(b-a)/6.0;
}
double asr(double a,double b,double esp,double aa)//递归
{
double c=a+(b-a)/2.0;
double l=simpson(a,c);
double r=simpson(c,b);
if(fabs(l+r-aa)<15*esp)
return l+r+(l+r-a)/15.0;
return asr(a,c,esp/2.0,l)+asr(c,b,esp/2.0,r);
}
double asr(double a,double b,double esp)//主过程
{
return asr(a,b,esp,simpson(a,b));
}
int main()
{
cout<<asr(0,1,0.000001)<<endl;
return 0;
}
朝闻道