题目:点击打开链接
辛普森公式:
C为斯科特系数。
证明:
知乎上看过一种方法,simpson其实就是用抛物线逼近,把f(x)设为抛物线,对抛物线求积分,整理就整理成上式。
所以对于2次方的函数,精度是足够的,但事实上,它具有三阶代数精度。
代码实现是使用Simpson对两端求积分,再分成两段求积分,差值足够小就返回,否则一直分段。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps=1e-6;
double a,b,l,r;
double f(double x){
return sqrt((b*b)*(1-(x*x)/(a*a)));
}
double simpson(double a,double b){
double c=a+(b-a)/2;
return (f(a)+4*f(c)+f(b))*(b-a)/6;
}
double asr(double a,double b,double eps,double A){
double c=a+(b-a)/2;
double L=simpson(a,c);
double R=simpson(c,b);
if(fabs(L+R-A)<=15*eps) return L+R+(L+R-A)/15.0;
return asr(a,c,eps/2,L)+asr(c,b,eps/2,R);
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%lf%lf%lf%lf",&a,&b,&l,&r);
printf("%.3f\n",2*asr(l,r,eps,simpson(a,b)));
}
return 0;
}