simpson求积分,二分加速提高精度

本文介绍了一种使用辛普森法则进行数值积分的方法,并通过C++编程语言实现了该算法。主要内容包括定义必要的数学函数、利用递归进行区间划分以提高积分精度,以及通过条件判断确保积分过程的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-8
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 100005
double r1, r2;
double f(double x)
{
    x=x*x;
    return sqrt((r2 * r2 - x ) * (r1 * r1 - x ));
}
double simpson(double l, double r)
{
    return (f(l) + f(r) + 4 * f((l + r) / 2)) / 6 * (r - l);
}
double work(double l, double r)
{
    double mid = (l + r) / 2;
    double ans = simpson(l, r);
    if (sgn(ans - simpson(l, mid) - simpson(mid, r)) == 0)
    {
        return ans;
    }
    else
    {
        return work(l, mid) + work(mid, r);
    }
}
int main()
{
#ifdef DeBUGs
    freopen("//home//amb//桌面//1.in", "r", stdin);
#endif
    while (scanf("%lf%lf", &r1, &r2) + 1)
    {
        if (r2 < r1)
            swap(r1, r2);
        double ans = work(0,r1);
        printf("%.4lf\n", ans * 8.0);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值