-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcubatura.cpp
106 lines (76 loc) · 2.06 KB
/
cubatura.cpp
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
#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
double f(float x, float y) {
return exp(y - x);
}
void simpson() {
//intervalos de integracao
float a1 = 0, b1 = 0.5;
float a2 = 0, b2 = 0.5;
float hx = 0.25;
int nx = (b1 - a1) / hx; // = 2
float hy = 0.25;
int ny = (b2 - a2) / hy; // = 2
/*
0.5 _______________________
| | |
| | |
| | |
| | |
0.25 |-----------+-----------|
| | |
| | |
| | |
|___________|___________|
0 0.25 0.5
*/
//Soma dos vertices
double E0 = f(0, 0) + f(0.5, 0.5) + f(0, 0.5) + f(0.5, 0);
//Soma dos pontos intermedios
double E1 = f(0, 0.25) + f(0.25, 0) + f(0.5, 0.25) + f(0.25, 0.5);
//Central
double E2 = f(0.25, 0.25);
double res = (hx*hy / 9) * (E0 + 4 * E1 + 16 * E2);
cout << "Res = " << res << endl;
}
void trapezios() {
//intervalos de integracao
float a1 = 0, b1 = 0.5;
float a2 = 0, b2 = 0.5;
float hx = 0.25;
int nx = (b1 - a1) / hx; // = 2
float hy = 0.25;
int ny = (b2 - a2) / hy; // = 2
/*
0.5 _______________________
| | |
| | |
| | |
| | |
0.25|-----------+-----------|
| | |
| | |
| | |
|___________|___________|
0 0.25 0.5
*/
//Soma dos vertices
double E0 = f(0, 0) + f(0.5, 0.5) + f(0, 0.5) + f(0.5, 0);
//Soma dos pontos intermedios
double E1 = f(0, 0.25) + f(0.25, 0) + f(0.5, 0.25) + f(0.25, 0.5);
//Central
double E2 = f(0.25, 0.25);
double res = (hx*hy / 4) * (E0 + 2 * E1 + 4 * E2);
cout << "Res = " << res << endl;
}
int main() {
cout << "-- Metodo dos trapezios --\n\n";
trapezios();
cout << "\n-- Metodo de Simpson -- \n\n";
simpson();
cout << endl;
cin.get();
return 0;
}