📄 simpson1.cpp
字号:
//Simpson method of Integration
//This is definite step version of c++
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
int N; //define the step:h=(b-a)/N
//-----------integrated fuction-------------
double f(double x)
{
return exp(x)/(1+x*x);
}
//------------------------------------------
//-----------definite step version----------
double Simpson(double a,double b,double (*f)(double))
{
double h=(b-a)/N;
double simp=0.0;
for(int i=1;i<=N;i++)
{
double x1=a+(i-1)*h;
double midx=a+(i-0.5)*h;
double x2=a+i*h;
simp+= f(x1)+4*f(midx)+f(x2);
}
simp*=h/6;
return simp;
}
//------------------------------------------
//-----------------main---------------------
void main()
{
double x,y;
cout<<"enter the lower limit,upper limit and step"<<endl;
cin>>x>>y>>N;
cout<<"The Integration(S method): "<<setprecision(9)<<Simpson(x,y,f)<<endl;
}
//------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -