📄 d3r5.cpp
字号:
#include "iostream.h"
#include "math.h"
double func2(double x)
{
return 1.0 / sqrt(x);
}
double fint2(double x)
{
//integral of func2
return 2.0 * sqrt(x);
}
void midpnt(double a, double b, double& s, int& n)
{
double del,ddel,sum,x;
int it,tnm,j;
if (n == 1)
{
s = (b - a) * func2(0.5 * (a + b));
it = 1;
}
else
{
it = (int)pow(3, n - 2);
tnm = it;
del = (b - a) / (3.0 * tnm);
ddel = del + del;
x = a + 0.5 * del;
sum = 0.0;
for (j = 1; j<=it; j++)
{
sum = sum + func2(x);
x = x + ddel;
sum = sum + func2(x);
x = x + del;
}
s = (s + (b - a) * sum / tnm) / 3.0;
}
}
void main()
{
//program d3r5
//driver for routine midpnt
const int nmax = 10;
double s,a = 0.0;
double b = 1.0;
cout<<endl;
cout<<"integral of func2 computed with midpnt"<<endl;
cout.precision(6);
cout<<endl;
cout<<"actual value of integral is"<<" ";
cout<<(fint2(b) - fint2(a))<<endl;
cout<<endl;
cout<<" n approx.integral"<<endl;
cout.setf(ios::fixed|ios::right);
for (int i = 1; i<=nmax; i++)
{
midpnt(a, b, s, i);
cout.width(5);
cout<<i;
cout.width(15);
cout<<s<<endl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -