-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice7.cpp
More file actions
58 lines (52 loc) · 1.63 KB
/
practice7.cpp
File metadata and controls
58 lines (52 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <iomanip>
void InputEps(float& eps) {
std::cout << "Введите значение точности вычисления интеграла.\n";
while (true) {
std::cout << "eps: ";
std::cin >> eps;
if (eps > pow(10, -4)) break;
else std::cout << "Вы ввели некорректное значение. Попробуйте еще раз." << std::endl;
}
}
float f(float x) {
return (float)pow(x, 2) * exp(-0.5 * x);
}
float SimpsoneIntegration(int a, int b, int n) {
float h = (float)(b - a) / n;
float integral = f(a) + f(b);
short int k;
for (size_t i = 1; i < n - 1; i++) {
k = 2 + 2 * (i % 2);
integral += k * f(a + h * i);
}
integral *= h / 3;
return integral;
}
void Solution(int a, int b, int& n, float eps, float& integrationValue) {
float integral_old = SimpsoneIntegration(a, b, n);
while (true) {
n *= 2;
float integral_new = SimpsoneIntegration(a, b, n);
if (fabs(integral_new - integral_old) <= eps) {
integrationValue = integral_new;
break;
}
integral_old = integral_new;
}
}
int main() {
setlocale(LC_ALL, "ru");
int n = 2;
int startN = n;
int a = 0;
float eps;
float integrationValue;
InputEps(eps);
std::cout << std::setw(5) << "Параметр b" << std::setw(15) << "Интеграл" << std::setw(20) << "Кол-во отрезков" << std::endl;
for (int b = 1; b <= 10; b++) {
Solution(a, b, n, eps, integrationValue);
std::cout << std::setw(5) << b << std::setw(20) << std::fixed << std::setprecision(10) << integrationValue << std::setw(20) << n << std::endl;
n = startN;
}
}