码迷,mamicode.com
首页 > 其他好文 > 详细

simpson公式求定积分(模板)

时间:2014-10-07 01:02:32      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   sp   div   c   on   log   

 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\n",getValue());
51     }
52     return 0;
53 }

 

simpson公式求定积分(模板)

标签:style   blog   color   io   sp   div   c   on   log   

原文地址:http://www.cnblogs.com/chenhuan001/p/4008776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!