sgu 217 分类: sgu 2015-06-24 23:44 15人阅读 评论(0) 收藏
自适应辛普森积分
以其中一圆柱的中轴线为
那么,以
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
const double eps = 1e-8;
double r1, r2;
double func(double x)
{
return sqrt((r1*r1-x*x)*(r2*r2-x*x));
}
double simp(double l, double r)
{
return (func(l)+func(r)+func((l+r)/2)*4)*(r-l)/6;
}
double simpson(double l, double r)
{
double mid = (l + r)/2, fw = simp(l, r);
double fl = simp(l, mid), fr = simp(mid, r);
if(fabs(fw - fl - fr) < eps) return fw;
else return simpson(l, mid) + simpson(mid, r);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("sgu217.in","r",stdin);
freopen("sgu217.out","w",stdout);
#endif
std::cin >> r1 >> r2;
printf("%lf",simpson(0, std::min(r1,r2))*8);
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。