c5.cpp
资源名称:数值分析课程设计.zip [点击查看]
上传用户:zhdd911129
上传日期:2007-05-11
资源大小:722k
文件大小:1k
源码类别:
matlab例程
开发平台:
Matlab
- //C5.cpp
- //Composite Simpson Rule
- #include<iostream.h>
- #include<math.h>
- double f(double x)
- {
- if (x==0)
- return 1;
- else
- return sin(x)/(x);
- }
- double ComSimp(double x0,double xn,int n)
- {
- double s1,s2,h;
- int i;
- h=(xn-x0)/n;s1=f(x0+h/2);s2=0; //Initialization
- for (i=1;i<n;i++)
- {
- s1+=f(x0+i*h+h/2);
- s2+=f(x0+i*h);
- }
- return h/6*(f(x0)+4*s1+2*s2+f(xn));
- }
- void main()
- {
- double x0=0,xn=1;
- int n=2;
- cout<<ComSimp(x0,xn,n)<<endl;
- }
- //When n==100,the answer is:0.946083
- //When n==2,the answer is:0.946087,
- //两次相差不大,可见复化Simpson公式有着极好的计算效果
- //但编写程序时必须注意本题除数会为零的情况