自适应simpson公式解定积分


来自白书

反正基础就是极限思想嘛,simpson原理没深究了,当做模板用吧

求定积分:



#include
#include
#include
double f(double x)
{
    return x;//自定义函数
}
double simpson(double a,double b)
{
    double c=a+(b-a)/2;
    return (f(a)+4*f(c)+f(b))*(b-a)/6;
}
//递归过程,可以保存la,rb,和simpson的值,若la,rb未变
//则直接使用保存的simpson值不用再调用simpson函数,
//若改变则调用函数并保存修改后的值
//eps为自定义精度
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_main(double a,double b,double eps)
{
    return asr(a,b,eps,simpson(a,b));
}
int main()
{
    printf("%lf\n",asr_main(0,100,1e-5));
    return 0;
}



la 3485 Bridge

#include
#include
#include
double a;
double f(double x)
{
    return sqrt(1+4*a*a*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_main(double a,double b,double eps)
{
    return asr(a,b,eps,simpson(a,b));
}
double solve(double w,double h)
{
    a=4*h/(w*w);
    return asr_main(0,w/2,1e-5)*2;
}
int main()
{
    double h,d,b,l;
    int n,cas;
    scanf("%d",&cas);
    for(int i=1;i<=cas;i++)
    {
        scanf("%lf%lf%lf%lf",&d,&h,&b,&l);
        n=(b+d-1)/d;
        double dd=b/n;
        double ll=l/n;
        double x=0,y=h;
        while(x+1e-9


你可能感兴趣的:(数学)