Сообщение было отмечено DemDV как решение
Решение
| C++ | 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
| #include <cstdlib>
#include <cstdio>
#include <cmath>
const double PI = acos(-1);
//число разбиений области интегрирования
const int N = 20000;
//подынтегральная функция
double f (double x)
{
return pow(exp(1), -tan(0.84*x)) / (0.35 + cos(x));
}
int main()
{
double a = 0.0, b = PI / 2;
//переменные для хранения суммы, значения аргумента и счетчик цикла
double sum = 0.0, x;
int i;
for (i = 0; i < N; i++)
{
x = a + (b - a) * (i - 0.5) / N;
sum += f(x);
}
//вычисление итогового результата и печать на экран
sum = sum / N;
printf("result = %lf (eval error: not found)\n", sum);
getchar();
return 0;
} |
|
интернет говорит, что неправильно считает... у вас же были лекции, были методички, неужели ничего не умеете?
Добавлено через 23 минуты
| C++ | 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
| #pragma once
#include <iostream>
#include <cmath>
#define parabola(x) x*x
#define sinus(x) sin(acos(-1)*x)
#define poly7(x) 16 + pow((x-16), 7)
#define poly10(x) 16 + pow((x-16), 10)
#define function_CybFor(x) 1.0 / (pow(exp(1), tan(0.84 * x)) * (0.35 + cos(x)))
#define NEXT system("pause"); system("cls");
#define EXIT system("pause"); system("cls"); return
using namespace std;
const double IntPar = 1.0 / 3, IntSin = (-cos(acos(-1) * 1) + cos(0)) / acos(-1), PI = acos(-1);
double simple(double a, double b);
double simpleN(double a, double b, int n);
double Int(double a, double b, int n = 1, int ind = 1);
double Int_add(double a, double b, double Int, int n = 1, int ind = 1);
double Simpson(double a, double b, int n = 1, int ind = 1);
//double Simpson_add(double a, double b, double Int, int n = 1, int ind = 1);
//double* find_x(double a, double b, double n);
int n0;
int main()
{
double a(0), b(PI / 2);
int n0 = 1, n;
//Your
cout << "XXX. Your\nInt = ~" << 0.6647669623949106 << endl;
for (int i = -3; i > -6; i--)
{
n = n0;
double integ, eps = pow(10, i);
do
{
n *= 2;
integ = Simpson(a, b, n);
} while (abs(Simpson(a, b, n * 2) - integ) > eps);
n0 = n;
printf("n = %d\tI = %lf\tdelta = %e\teps = %e\n", n, integ, abs(0.6647669623949106 - integ), eps);
}
system("pause>nul");
return 0;
//1.A
cout << "1.A\n";
cout << "I = " << IntPar << " Ih = " << simple(a, b) << " deltah: " << abs(simple(a, b) - IntPar) << endl;
//1.B
cout << "\n\n1.B\n";
for (int i = 0; i < 4; i++)
{
cout << "I = " << IntPar << " Ih = " << simpleN(a, b, n) << " deltah: " << abs(simpleN(a, b, n) - IntPar) << " n = " << n << endl;
n *= 2;
}
//1.C + 1.D
cout << "\n\n1.C+1.D\n";
for (int i = 0, n = n0; i < 4; i++)
{
cout << "I = " << IntPar << " Ih = " << Int(a, b, n) << " deltah: " << abs(Int(a, b, n) - IntPar) << " n = " << n << endl;
n *= 2;
}
//1.E
cout << "\n\n1.E\n";
cout << "I = " << Int(a, b, 4) << " n = 4\n";
cout << "I = " << Int_add(a, b, Int(a, b, 4), 4) << " n = 8\n";
NEXT;
/*//2.A
cout << "2.A\nInt = " << IntPar << endl;
for (int i = -3; i > -6; i--)
{
n = 1;
double integ = Int(a, b, n), eps = pow(10, i), integ0;
while (abs(integ - (integ0 = Int_add(a, b, integ, n))) / 3 > eps)
{
integ = Int_add(a, b, integ, n);
n *= 2;
}
printf("n = %d\tI = %lf\tdelta = %e\teps = %e\n", n, integ0, abs(integ - integ0) / 3, eps);
}
//2.B
cout << "2.B\nInt = " << IntSin << endl;
for (int i = -3; i > -6; i--)
{
n = 1;
double integ = Int(a, b, n, 2), eps = pow(10, i), integ0;
while (abs(integ - (integ0 = Int_add(a, b, integ, n, 2))) / 3 > eps)
{
integ = Int_add(a, b, integ, n, 2);
n *= 2;
}
printf("n = %d\tI = %lf\tdelta = %e\teps = %e\n", n, integ0, abs(integ - integ0) / 3, eps);
}*/
//2.A
cout << "2.A\nInt = " << IntPar << endl;
for (int i = -3; i > -6; i--)
{
n = 0;
double integ, eps = pow(10, i);
do
{
n++;
integ = Int(a, b, n);
} while (abs(Int(a, b, n * 2) - integ)/3 > eps);
printf("n = %d\tI = %lf\tdelta = %e\teps = %e\n", n * 2, Int(a, b, n * 2), abs(Int(a, b, n * 2) - integ) / 3, eps);
}
//2.B
cout << "2.B\nInt = " << IntSin << endl;
for (int i = -3; i > -6; i--)
{
n = 0;
double integ, eps = pow(10, i);
do
{
n++;
integ = Int(a, b, n, 2);
} while (abs(Int(a, b, n * 2, 2) - integ)/3 > eps);
printf("n = %d\tI = %lf\tdelta = %e\teps = %e\n", n * 2, Int(a, b, n * 2, 2), abs(Int(a, b, n * 2, 2) - integ)/3, eps);
}
//2.C
cout << "2.C\nInt = " << IntPar << endl;
for (int i = -3; i > -6; i--)
{
n = 0;
double integ, eps = pow(10, i);
do
{
n++;
integ = Simpson(a, b, n);
} while (abs(Simpson(a, b, n * 2) - integ) > eps);
printf("n = %d\tI = %lf\tdelta = %e\teps = %e\n", n, integ, abs(IntPar - integ), eps);
}
//2.D
cout << "2.D\nInt = " << IntSin << endl;
for (int i = -3; i > -6; i--)
{
n = 0;
double integ, eps = pow(10, i);
do
{
n++;
integ = Simpson(a, b, n, 2);
} while (abs(Simpson(a, b, n * 2, 2) - integ) > eps);
printf("n = %d\tI = %lf\tdelta = %e\teps = %e\n", n, integ, abs(IntSin - integ), eps);
}
//NEXT;
//3.A
int l = 16;
a = l; b = l + 1;
EXIT 0;
}
double simple(double a, double b)
{
return (parabola(a)+parabola(b))/2 * (b - a);
}
double simpleN(double a, double b, int n)
{
double integ = 0, h = (b - a) / n, x0 = a;
for (int i = 0; i < n; i++)
{
integ += simple(x0, x0 + h);
x0 += h;
}
return integ;
}
double Int(double a, double b, int n, int ind)
{
double integ, h = (b - a) / n, x0 = a;
if (ind == 1)
{
integ = parabola(b) + parabola(a);
for (int i = 1; i < n; i++)
{
x0 += h;
integ += 2 * parabola(x0);
}
}
else
{
integ = sinus(b) + sinus(a);
for (int i = 1; i < n; i++)
{
x0 += h;
integ += 2 * sinus(x0);
}
}
return integ * h / 2;
}
double Int_add(double a, double b, double Int, int n, int ind)
{
double integ = 0, h = (b - a) / n, x0 = a + h / 2;
if (ind == 1)
for (int i = 0; i < n; i++)
{
integ += 2 * parabola(x0);
x0 += h;
}
else
for (int i = 0; i < n; i++)
{
integ += 2 * sinus(x0);
x0 += h;
}
return (integ + Int * 2 / h) * h / 4;
}
double Simpson(double a, double b, int n, int ind)//h*(f0+f2n+2*(f2k)+4*(f2k-1))/3
{
double integ = 0, h = (b - a) / (2 * n), x0 = a;
if (ind == 1)
{
integ = function_CybFor(b) + parabola(a);
for (int i = 1; i < 2 * n; i++)
{
x0 += h;
integ += 2 * function_CybFor(x0) * ((i + 1) % 2) + 4 * function_CybFor(x0) * (i % 2);
}
}
else
{
integ = sinus(b) + sinus(a);
for (int i = 1; i < 2*n; i++)
{
x0 += h;
integ += 2 * sinus(x0) * ((i + 1) % 2) + 4 * sinus(x0) * (i % 2);
}
}
return integ * h / 3;
}
/*
double Simpson_add(double a, double b, double Int, int n, int ind)
{
double integ = 0, h = (b - a) / (2 * n), x0 = a + h / 2;
if (ind == 1)
for (int i = 0; i < n; i++)
{
integ += 4 * parabola(x0) * (i % 2);
x0 += h;
}
else
for (int i = 0; i < n; i++)
{
integ += 4 * sinus(x0) * (i % 2);
x0 += h;
}
return (integ)*h / 6 + Int / 2;
}*/ |
|
лишнее можете убрать, ну и разбить на файлы
Добавлено через 8 минут
Кликните здесь для просмотра всего текста
| C++ | 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
| #include <iostream>
#include <cmath>
#define parabola(x) x*x
#define sinus(x) sin(acos(-1)*x)
#define poly7(x) 16 + pow((x-16), 7)
#define poly10(x) 16 + pow((x-16), 10)
#define function_CybFor(x) 1.0 / (pow(exp(1), tan(0.84 * x)) * (0.35 + cos(x)))
#define NEXT system("pause"); system("cls");
#define EXIT system("pause"); system("cls"); return
using namespace std;
const double IntPar = 1.0 / 3, IntSin = (-cos(acos(-1) * 1) + cos(0)) / acos(-1), PI = acos(-1);
double simple(double a, double b);
double simpleN(double a, double b, int n);
double Int(double a, double b, int n = 1, int ind = 1);
double Int_add(double a, double b, double Int, int n = 1, int ind = 1);
double Simpson(double a, double b, int n = 1, int ind = 1);
//double Simpson_add(double a, double b, double Int, int n = 1, int ind = 1);
//double* find_x(double a, double b, double n);
int n0;
int main()
{
double a(0), b(PI / 2), eps = 1.e-2, prev = 0.6647669623949106;
int n0 = 1, n;
//Your
cout << "XXX. Your\nInt = ~" << 0.6647669623949106 << endl;
for (int i = -3; i >= -6; i--)
{
n = n0;
double integ;
eps /= 10;
do
{
n *= 2;
integ = Simpson(a, b, n);
} while ((0.6647669623949106 - integ) > eps);
n0 = n;
printf("n = %d\tI = %lf\tdelta = %e\teps = %e\n", n, integ, abs(0.6647669623949106 - integ), eps);
}
system("pause>nul");
return 0;
//1.A
cout << "1.A\n";
cout << "I = " << IntPar << " Ih = " << simple(a, b) << " deltah: " << abs(simple(a, b) - IntPar) << endl;
//1.B
cout << "\n\n1.B\n";
for (int i = 0; i < 4; i++)
{
cout << "I = " << IntPar << " Ih = " << simpleN(a, b, n) << " deltah: " << abs(simpleN(a, b, n) - IntPar) << " n = " << n << endl;
n *= 2;
}
//1.C + 1.D
cout << "\n\n1.C+1.D\n";
for (int i = 0, n = n0; i < 4; i++)
{
cout << "I = " << IntPar << " Ih = " << Int(a, b, n) << " deltah: " << abs(Int(a, b, n) - IntPar) << " n = " << n << endl;
n *= 2;
}
//1.E
cout << "\n\n1.E\n";
cout << "I = " << Int(a, b, 4) << " n = 4\n";
cout << "I = " << Int_add(a, b, Int(a, b, 4), 4) << " n = 8\n";
NEXT;
/*//2.A
cout << "2.A\nInt = " << IntPar << endl;
for (int i = -3; i > -6; i--)
{
n = 1;
double integ = Int(a, b, n), eps = pow(10, i), integ0;
while (abs(integ - (integ0 = Int_add(a, b, integ, n))) / 3 > eps)
{
integ = Int_add(a, b, integ, n);
n *= 2;
}
printf("n = %d\tI = %lf\tdelta = %e\teps = %e\n", n, integ0, abs(integ - integ0) / 3, eps);
}
//2.B
cout << "2.B\nInt = " << IntSin << endl;
for (int i = -3; i > -6; i--)
{
n = 1;
double integ = Int(a, b, n, 2), eps = pow(10, i), integ0;
while (abs(integ - (integ0 = Int_add(a, b, integ, n, 2))) / 3 > eps)
{
integ = Int_add(a, b, integ, n, 2);
n *= 2;
}
printf("n = %d\tI = %lf\tdelta = %e\teps = %e\n", n, integ0, abs(integ - integ0) / 3, eps);
}*/
//2.A
cout << "2.A\nInt = " << IntPar << endl;
for (int i = -3; i > -6; i--)
{
n = 0;
double integ, eps = pow(10, i);
do
{
n++;
integ = Int(a, b, n);
} while (abs(Int(a, b, n * 2) - integ)/3 > eps);
printf("n = %d\tI = %lf\tdelta = %e\teps = %e\n", n * 2, Int(a, b, n * 2), abs(Int(a, b, n * 2) - integ) / 3, eps);
}
//2.B
cout << "2.B\nInt = " << IntSin << endl;
for (int i = -3; i > -6; i--)
{
n = 0;
double integ, eps = pow(10, i);
do
{
n++;
integ = Int(a, b, n, 2);
} while (abs(Int(a, b, n * 2, 2) - integ)/3 > eps);
printf("n = %d\tI = %lf\tdelta = %e\teps = %e\n", n * 2, Int(a, b, n * 2, 2), abs(Int(a, b, n * 2, 2) - integ)/3, eps);
}
//2.C
cout << "2.C\nInt = " << IntPar << endl;
for (int i = -3; i > -6; i--)
{
n = 0;
double integ, eps = pow(10, i);
do
{
n++;
integ = Simpson(a, b, n);
} while (abs(Simpson(a, b, n * 2) - integ) > eps);
printf("n = %d\tI = %lf\tdelta = %e\teps = %e\n", n, integ, abs(IntPar - integ), eps);
}
//2.D
cout << "2.D\nInt = " << IntSin << endl;
for (int i = -3; i > -6; i--)
{
n = 0;
double integ, eps = pow(10, i);
do
{
n++;
integ = Simpson(a, b, n, 2);
} while (abs(Simpson(a, b, n * 2, 2) - integ) > eps);
printf("n = %d\tI = %lf\tdelta = %e\teps = %e\n", n, integ, abs(IntSin - integ), eps);
}
//NEXT;
//3.A
int l = 16;
a = l; b = l + 1;
EXIT 0;
}
double simple(double a, double b)
{
return (parabola(a)+parabola(b))/2 * (b - a);
}
double simpleN(double a, double b, int n)
{
double integ = 0, h = (b - a) / n, x0 = a;
for (int i = 0; i < n; i++)
{
integ += simple(x0, x0 + h);
x0 += h;
}
return integ;
}
double Int(double a, double b, int n, int ind)
{
double integ, h = (b - a) / n, x0 = a;
if (ind == 1)
{
integ = parabola(b) + parabola(a);
for (int i = 1; i < n; i++)
{
x0 += h;
integ += 2 * parabola(x0);
}
}
else
{
integ = sinus(b) + sinus(a);
for (int i = 1; i < n; i++)
{
x0 += h;
integ += 2 * sinus(x0);
}
}
return integ * h / 2;
}
double Int_add(double a, double b, double Int, int n, int ind)
{
double integ = 0, h = (b - a) / n, x0 = a + h / 2;
if (ind == 1)
for (int i = 0; i < n; i++)
{
integ += 2 * parabola(x0);
x0 += h;
}
else
for (int i = 0; i < n; i++)
{
integ += 2 * sinus(x0);
x0 += h;
}
return (integ + Int * 2 / h) * h / 4;
}
double Simpson(double a, double b, int n, int ind)//h*(f0+f2n+2*(f2k)+4*(f2k-1))/3
{
double integ = 0, h = (b - a) / (2 * n), x0 = a;
if (ind == 1)
{
integ = function_CybFor(b) + parabola(a);
for (int i = 1; i < 2 * n; i++)
{
x0 += h;
integ += 2 * function_CybFor(x0) * ((i + 1) % 2) + 4 * function_CybFor(x0) * (i % 2);
}
}
else
{
integ = sinus(b) + sinus(a);
for (int i = 1; i < 2*n; i++)
{
x0 += h;
integ += 2 * sinus(x0) * ((i + 1) % 2) + 4 * sinus(x0) * (i % 2);
}
}
return integ * h / 3;
}
/*
double Simpson_add(double a, double b, double Int, int n, int ind)
{
double integ = 0, h = (b - a) / (2 * n), x0 = a + h / 2;
if (ind == 1)
for (int i = 0; i < n; i++)
{
integ += 4 * parabola(x0) * (i % 2);
x0 += h;
}
else
for (int i = 0; i < n; i++)
{
integ += 4 * sinus(x0) * (i % 2);
x0 += h;
}
return (integ)*h / 6 + Int / 2;
}*/ |
|
чуть-чуть ускорил
0
|