自適應辛普森法求積分

Bridge UVALive - 3485

要計算baf(x)dx
將之放到二維座標系中,就相當於求面積。
三點辛普森公式:f(a)+4f(min(a,b))+f(b)6

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
#define ll long long
#define df double

df a;
df F(df x){ return sqrt(1+4.0*a*a*x*x);}

df simpson(df a,df b)
{
    df c=a+(b-a)/2.0;
    return (F(a)+4*F(c)+F(b))*(b-a)/6.0;
}
df asr(df a,df b,df eps,df A)
{
    df c=a+(b-a)/2.0;
    df 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.0,L)+asr(c,b,eps/2.0,R);
}
df asr(df a,df b,df eps){
    return asr(a,b,eps,simpson(a,b));
}
df parabola(df w,df h)
{
    a=4.0*h/(w*w);
    return asr(0,w/2,1e-5)*2;
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int kase=1;kase<=T;kase++)
    {
        int D,H,B,L;
        scanf("%d %d %d %d",&D,&H,&B,&L);
        int n=(B+D-1)/D;
        df D1=(df)B/n;
        df L1=(df)L/n;
        df x=0,y=H;
        while(y-x>1e-5)
        {
            df m=x+(y-x)/2;
            if(parabola(D1,m)<L1) x=m;
            else y=m;
        }
        if(kase>1) printf("\n");
        printf("Case %d:\n%.2f\n",kase,H-x);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章