数值积分-自适应辛普森法

自适应辛普森法模板题 UVAlive3485;

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<cctype>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<stack>
#define ll long long
#define MAX 1000
#define INF INT_MAX
#define EPS 1e-5

using namespace std;

double a;

double f(double x){                       //被积函数
	return sqrt(1 + 4*a*a*x*x);
}

double simpson(double x, double y){      //三点辛普森公式
	double z = x + (y - x) / 2;
	return (f(x) + 4*f(z) + f(y))*(y-x) / 6;
}

double asr(double x, double y, double eps, double A){    //自适应辛普森法,求函数f在区间[x,y]上的积分
	double z = x + (y-x) / 2;
	double L = simpson(x,z), R = simpson(z,y);
	if (fabs(L+R-A) <= 15*eps) return L + R + (L+R-A) / 15.0;
	return asr(x,z,eps/2,L) + asr(z,y,eps/2,R);
}

double Length(double w, double h){
	a = 4.0*h/(w*w);
	return solve(0,w/2,1e-5,simpson(0,w/2)) * 2;
}

int main(){
	int T,D,H,B,L;
	scanf("%d",&T);
	for (int cas = 1; cas <= T; cas++){
		scanf("%d%d%d%d",&D,&H,&B,&L);
		int n = (B+D-1) / D;              //注意向上取整 
		double w = (double)B / n;
		double l = (double)L / n;
		double x = 0.0, y = H*1.0;
		while (y - x > EPS){
			double m = x + (y-x) / 2.0;
			if (Length(w,m) > l){
				y = m;
			}
			else x = m;
		}
		printf("Case %d:\n%.2lf\n",cas,H - x);
		if (cas < T) printf("\n");
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值