自适应辛普森积分

自适应辛普森积分

非常简单而自欺欺人的一个东西,,就是拿积分区间中的端点和中点去模拟函数,如果分成两段积与分成一段积差别不大,就认为是这段的积分了
具体地,对于区间\([l,r]\),我们认为它的面积为
\[S = \frac{(r - l)(f(l) + 4f(\frac{l + r}{2}) + f(r))}{6}\]
如果差别大于设定的精确度,就递归求解
板题1
板题2

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define cls(s,v) memset(s,v,sizeof(s))
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
#define eps 1e-12
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 0x3f3f3f3f;
double a,b,c,d,L,R;
double f(double x){return (c * x + d) / (a * x + b);}
double S(double l,double r){
    return (r - l) / 6.0 * (f(l) + 4.0 * f((l + r) / 2.0) + f(r));
}
double simpson(double L,double R){
    double mid = (L + R) / 2.0,Sl = S(L,mid),Sr = S(mid,R),SS = S(L,R);
    if (fabs(SS - Sl - Sr) < eps) return (SS + Sl + Sr) / 2.0;
    return simpson(L,mid) + simpson(mid,R);
}
int main(){
    scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&L,&R);
    printf("%.6lf",simpson(L,R));
    return 0;
}
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define cls(s,v) memset(s,v,sizeof(s))
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
#define eps 1e-9
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 0x3f3f3f3f;
double a;
double f(double x){return pow(x,a / x - x);}
double S(double l,double r){
    return (r - l) / 6.0 * (f(l) + 4.0 * f((l + r) / 2.0) + f(r));
}
double simpson(double L,double R){
    double mid = (L + R) / 2.0,Sl = S(L,mid),Sr = S(mid,R),SS = S(L,R);
    if (fabs(SS - Sl - Sr) < eps) return (SS + Sl + Sr) / 2.0;
    return simpson(L,mid) + simpson(mid,R);
}
int main(){
    scanf("%lf",&a);
    if (a < 0) puts("orz");
    else printf("%.5lf",simpson(eps,20));
    return 0;
}

转载于:https://www.cnblogs.com/Mychael/p/9262080.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值