📄 c4.cpp
字号:
//C4.cpp
//Composite Simpson Rule
#include<iostream.h>
double f(double x)
{
return 1/(1+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=100;
cout<<ComSimp(x0,xn,n)<<endl;
}
//The answer:0.785398,
//与精度达e-15的精确值0.78539816339745已经相当接近
//算法耗时短,Simpson的确是一种相当理想的的求积方法
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -