自适应辛普森积分
非常简单而自欺欺人的一个东西,,就是拿积分区间中的端点和中点去模拟函数,如果分成两段积与分成一段积差别不大,就认为是这段的积分了
具体地,对于区间\([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;
}