zoukankan      html  css  js  c++  java
  • simpson公式求定积分(模板)

     1 #include<cstdio>
     2 #include<cmath>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 double r,R1;//公式需要用到的变量
     7 
     8 // simpson公式用到的函数,就是待积分函数
     9 double F(double x)
    10 {
    11     //return sqrt(r*r-x*x)*sqrt(R1*R1-x*x);
    12     return f(x);
    13 }
    14 
    15 
    16 // 三点simpson法。这里要求F是一个全局函数
    17 double simpson(double a, double b)
    18 {
    19     double c = a + (b-a)/2;
    20     return (F(a)+4*F(c)+F(b))*(b-a)/6;
    21 }
    22 
    23 // 自适应Simpson公式(递归过程)。已知整个区间[a,b]上的三点simpson值A
    24 double asr(double a, double b, double eps, double A)
    25 {
    26     double c = a + (b-a)/2;
    27     double L = simpson(a, c), R = simpson(c, b);
    28     if(fabs(L+R-A) <= 15*eps) return L+R+(L+R-A)/15.0;
    29     return asr(a, c, eps/2, L) + asr(c, b, eps/2, R);
    30 }
    31 
    32 // 自适应Simpson公式(主过程)
    33 double asr(double a, double b, double eps)
    34 {
    35     return asr(a, b, eps, simpson(a, b));
    36 }
    37 
    38 // 用自适应Simpson公式计算积分数值
    39 double getValue()
    40 {
    41     return asr(0, r, 1e-5)*8; // 第一第二个参数为积分区间,第三个参数为精度
    42 }
    43 
    44 int main()
    45 {
    46     while(~scanf("%lf%lf",&r,&R1))
    47     {
    48         if(r>R1)
    49             swap(r,R1);
    50         printf("%.10f
    ",getValue());
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    QAction使用
    QT学后感
    设置背景图片
    获取句柄的方法总结(尤其是对于dll而言)
    unixbench小试
    Java命令行之jar命令
    Java应用程序安装包制作工具简介
    Eclipse使用ant编译时的乱码问题
    Jboss4中使用配置发送邮件
    Exe4j注册码
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/4008776.html
Copyright © 2011-2022 走看看