📄 积分的近似求法.cpp
字号:
//利用辛普森变步长公式求积分
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
//被积函数的定义
double fun(double x)
{
return exp(x)/(1+x*x);
}
//计算精度
const double eps=1e-4;
void main()
{
int n=1;
double start=0;
double end=1;
double height=end-start;
double I2n=height*(fun(start)+fun(end));
double T2n=I2n;
double Tn,In=0;
while(fabs(I2n-In)>=eps)
{
Tn=T2n;
In=I2n;
double sigma=0;
for(int k=0;k<n;k++)
{
sigma=sigma+(start+(k+0.5)*height);
}
T2n=(Tn+height*sigma)/2.0;
In=(4*T2n-Tn)/3.0;
n=n*2;
height=height/2;
}
cout<<"The integral of fun() from "<<start<<" to "
<<end<<" is : "<<endl;
cout<<setiosflags(ios::fixed)
<<setprecision(10)
<<setw(12)
<<T2n
<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -