龙贝格算法实现

#include<stdio.h>
#include<math.h>

double f(double x)
{
 double h;
 h=sin(x)/x;
 return(h);
}

void main()
{
 double T1,T2;
 double S,S1,S2;
 double C1,C2;
 double R1,R2;
 double e=1e-6;
 double a,b,h,x;
 int k;
 printf("Input a and b:/n");
 scanf("%lf %lf",&a,&b);
 if(a==0)
  a=0.0000001;
 h=b-a;
 T1=(f(a)+f(b))*h/2;
    for(k=1;k<=3;k++)
 {
  S=0;
  x=a+h/2;
  while(x<b)
  {
    S=S+f(x);
    x=x+h;
  }
       T2=T1/2+S*(h/2);
    S2=T2+1/3*(T2-T1);
    if(k==1){h=h/2;T1=T2;S1=S2; continue;}
    C2=S2+1/15*(S2-S1);
    if(k==2){C1=C2;h=h/2;T1=T2;S1=S2; continue;}
    R2=C2+1/63*(C2-C1);
    if(k==3){R1=R2;C1=C2;h=h/2;T1=T2;S1=S2; continue;}
 }
 do{
  if(fabs(R2-R1)<e)
   break;
  R1=R2;
  C1=C2;
  h=h/2;
  T1=T2;
  S1=S2;
  S=0;
  x=a+h/2;
  while(x<b)
  {
   S=S+f(x);
   x=x+h;
  }
  T2=T1/2+S*(h/2);
  S2=T2+1/3*(T2-T1);
  C2=S2+1/15*(S2-S1);
  R2=C2+1/63*(C2-C1);
 }while(1);
 printf("result:%lf ",R2);
 printf("/n");
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值