您的位置:首页 > 其它

hdu 1724数学题 辛普森公式求积分

2014-09-16 23:06 453 查看
Problem Description

Math is important!! Many students failed in 2+2’s mathematical test, so let's AC this problem to mourn for our lost youth..

Look this sample picture:



A ellipses in the plane and center in point O. the L,R lines will be vertical through the X-axis. The problem is calculating the blue intersection area. But calculating the intersection area is dull, so I have turn to you, a talent of programmer. Your task
is tell me the result of calculations.(defined PI=3.14159265 , The area of an ellipse A=PI*a*b )

 

就是求阴影部分面积,用辛普森公式求积分, 求f(x)*dx在区间 [l,r]上的积分。#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;
double a1,b1;
double F(double x){ //函数f[x]
return b1/a1*sqrt(a1*a1-x*x);
}
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),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);
}

double asr(double a,double b,double eps){
return asr(a,b,eps,simpson(a,b));
}

double parabola_arc_length(double l,double r){ // 区间[l,r]
return asr(l,r,1e-5)*2;
}
int main()
{
double l,r;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lf%lf%lf%lf",&a1,&b1,&l,&r);
printf("%.3lf\n",parabola_arc_length(l,r));
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: