📄 simpson.cpp
字号:
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
//#define f(x) sin(x)
//#define I(x) cos(x)
#define f(x) log(x)
#define I(x) 1/x
double simpson(double up,double down,int n)
{
int i,m = n/2;
double h = (up - down)/n;
double S,temp1,temp2;
temp1 = temp2 = 0;
for(i=0;i<=m-1;i++)
{
temp1 += f(down+(2*i+1)*h);
}
for(i=1;i<=m-1;i++)
{
temp2 += f(down+2*i*h);
}
S = h * ( f(down) + 4*temp1 + 2*temp2 +f(up) )/3;
return S;
}
double error(double up,double down,double S)
{
double P = I(down) - I(up);
return P - S;
}
int main()
{
double up,down;
cout<<"Please input the up boundary and the down boundary:"<<endl;
cin>>up;
cin>>down;
cout.precision(18);
cout.setf(ios::fixed);
for(int k=0;k<=12;k++)
{
int n = pow(2,k);
double S = simpson(up,down,n);
cout<<"When k = "<<k<<":"<<endl;
cout<<"复化Simpson积分可得结果为: "<<S<<endl;
cout<<" *********** AND 误差为: "<<error(up,down,S)<<endl;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -