Ronberg算法计算积分

语法:

result=integral(double a,double b);

参数:

a:积分上限

b:积分下限

function:

f()积分函数

返回值:

f在a、b之间的返回值

注意:

function f(x)需要自行修改。程序中用的是 x*x;

需要 math.h;

默认精度要求是

1e-5

源程序:

#include<iostream>
#include<math.h>
#define epsilon 0.00001
#define COUNT 100
using namespace std;

double fun(double x)
{
    return x*x;
}

double Romberg(double a,double b)
{
    int m ,n;
    double h,x,s,q,ep;
    double p,*R =new double[COUNT];

            h=b-a;
            R[0]= h*(fun(a)+ fun(b))/2.0;
            m=1;
            n=1;
            ep=epsilon+1.0;
            while ((ep >= epsilon)&& (m <COUNT))
            {
                p = 0.0;
            {
                for(int i=0;i<n;i++)
                {
                    x = a+ (i+0.5)*h ;
                    p= p + fun(x);
                }
                    p= (R[0]+ h*p)/2.0;
                    s = 1.0;
                    for(int k=1;k<=m;k++)
                    {
                        s = 4.0*s;
                        q= (s*p-R[k-1])/(s-1.0);
                            R[k-1]= p;
                            p =q;
                    }
                    p=fabs(q -R[m-1]);
                    m =m + 1;
                    R[m-1]= q;
                    n = n + n;
                    h = h/2.0;
                }
                return (q);
            }
}

int main()
{
    double a,b;
    cout<<"Input a,b:a为下限,b为上限"<<endl;
    cin>>a>>b;
    cout<<"Romberg="<<Romberg(a,b)<<endl;
    system("pause");
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值