-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegration.cpp
More file actions
52 lines (47 loc) · 1.09 KB
/
Integration.cpp
File metadata and controls
52 lines (47 loc) · 1.09 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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double trap, simp, boole, euler, h, xL = 0, xR = 3.14159265358979323846, arg = xL;
int n;
for (int i = 2; i < 11; i++)
{
arg = xL;
trap = 0;
n = pow(2, i);
h = (xR - xL) / n;
for (int j = 0; j <= n; j++)
{
if (j == 0 || j == n) {
trap += 0.5 * h * sin(arg);
}
else {
trap += h * sin(arg);
}
arg += h;
}
cout << n << " " << trap << endl;
}
for (int i = 2; i < 11; i++)
{
arg = xL;
simp = 0;
n = pow(2, i);
h = (xR - xL) / n;
for (int j = 0; j <= n; j++)
{
if ((j == 0) || (j == n)) {
simp += sin(arg);
}
else if (j % 2 == 0) {
simp += 2.0 * sin(arg);
}
else {
simp += 4.0 * sin(arg);
}
arg += h;
}
cout << n << " " << simp * h / 3 << endl;
}
}