正文

romberg 算法求数值积分2006-03-29 21:45:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/bakers/11603.html

分享到:

目标:       用龙贝格(romberg)算法,计算  sin(x)/x 的积分; 要求:      输入,积分区间,误差限;      输出,序列T,S,C,及积分结果   实现:             流程图(略);   时间:3.29  晚7.0-8.0   代码(C++实现): #include"stdlib.h"#include"math.h"#include"iostream" using namespace std; double function( double x){ return x==0?1:sin(x)/x;} int print_romberg(double a,double b,double e){   int k=1;   double h=b-a;   double t1,t2,s1,s2,c1,c2,s,x,r2,r1;   t1=h*(function(a)+function(b))/2;   goto loop;      loop:   s=0;   x=a+h/2;   do{           s=s+function(x);           x=x+h;           }while(x<b);   t2=t1/2+h*s/2;   s2=t2+(t2-t1)/3;   if(k==1){            k=k+1;            h=h/2;            t1=t2;            s1=s2;            goto loop;            }   c2=s2+(s2-s1)/15;   if(k==2){            c1=c2;            k=k+1;            h=h/2;            t1=t2;            s1=s2;             goto loop;            }   r2=c2+(c2-c1)/63;   if(k==3){            r1=r2;            c2=c1;            k=k+1;            h=h/2;            t1=t2;            s1=s2;             goto loop;            }   if(fabs(r2-r1)<e){                 cout<<"the result is :"<<r2<<endl;                 return 1;                 }   else{           r1=r2;            c2=c1;            k=k+1;            h=h/2;            t1=t2;            s1=s2;            goto loop;           }            }                        int main(){ double a,b; double e,y; y=function(0.2); char flag;    do{         cout<<"enter the left area: ";      cin>>a;      cout<<endl;     cout<<"enter the right area: ";     cin>>b;      cout<<endl;      cout<<"enter the precision: ";     cin>>e;     cout<<endl;      print_romberg(a,b,e);     cout<<"do you want to replay this process!"<<endl;     cout<<"enter y to continue,any others to exit?"<<endl;        cin>>flag; } while(flag=='y'); return 1;} 以上程序在DEV C++中调试通过,由于使用了GOTO 语句,尚需改进地方,暂时搁浅。

阅读(6379) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册