龙贝格求积法

#include<iostream>
using namespace std;
#include"math.h"
#define e  1e-5
double  f(double x)
{
    double y;
    if (x==0)
        return y=1.0;//f(0)=1
    else y=sin(x)/x;
    return y;
}
void romberg(double a,double b)
{
    int n=1,k=0,i;
    double h,T2,S2=0,C2=0,R2=0,T1,C1,S1,R1;
    double x;
    h=b-a;
    T2=h/2*(f(a)+f(b));
    do//在要求精度之下
    {
        x=a+h/2;
        R1=R2;
        T1=T2;
        S1=S2;
        C1=C2;
        double sum=0;
        for(i=0;i<=pow(2,k)-1;i++)
           {
               sum=sum+f(x);///都是公式
               x+=h;
           }
        T2=(T1+sum*h)/2;
        S2=(4*T2-T1)/3;
        C2=(16*S2-S1)/15;
        R2=(64*C2-C1)/63;
        k++;
        h=h/2;
    }while (fabs((R2-R1))>e);
    cout<<R2<<endl;
}
int main()
{
    double a,b;
    cout<<"输入上下限a,b的值用空格隔开"<<endl;
    cin>>a>>b;
    cout<<"下限a="<<a<<endl;
    cout<<"上限b="<<b<<endl;
    cout<<"被积函数为:y=sin(x)/x"<<endl;
    romberg(a,b);
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值