zoukankan      html  css  js  c++  java
  • simpson积分模板

    河工大校赛卡B题卡了三个小时。当时还很疑惑怎么这么多人积分学的这么好,补题的时候才知道居然有直接的算法模板可以套用。

    记录下神奇的积分模板:

    #include<cstdio>
    #include<cmath>
    #include <algorithm>
    using namespace std;
    
    double r;//公式需要用到的变量// simpson公式用到的函数,就是待积分函数
    double F(double t)
    {
        return f(x);
    }
    
    // 三点simpson法。这里要求F是一个全局函数
    double simpson(double a, double b)
    {
        double c = a + (b-a)/2;
        return (F(a)+4*F(c)+F(b))*(b-a)/6;
    }
    
    // 自适应Simpson公式(递归过程)。已知整个区间[a,b]上的三点simpson值A
    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);
    }
    
    // 自适应Simpson公式(主过程)
    double asr(double a, double b, double eps)
    {
        return asr(a, b, eps, simpson(a, b));
    }
    
    // 用自适应Simpson公式计算积分数值
    double getValue()
    {
        return asr(0, r, 1e-6); // 第一第二个参数为积分区间,第三个参数为精度
    }
  • 相关阅读:
    ZOJ Problem Set
    ZOJ Problem Set
    UVa 11464 偶数矩阵 枚举
    poj 1753 枚举
    Codeforces 637D 模拟
    hdu 5631 并查集
    hdu 5438 并查集
    UVa 10129 单词 (有向欧拉路+并查集)
    hdu 3018 欧拉路定理+并查集
    并查集的初步学习
  • 原文地址:https://www.cnblogs.com/zznulw/p/6755763.html
Copyright © 2011-2022 走看看