-
Notifications
You must be signed in to change notification settings - Fork 0
/
numerical_integration.cpp
69 lines (65 loc) · 1.42 KB
/
numerical_integration.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
#include "numerical_integration.h"
/*
* Given function f and limits a and b, estimate integral which gives area
* under f from a to b.
*
* Methods implemented:
* 1. Midpoint rule
* 2. Trapezoidal rule
* 3. composite Simpson's 1/3 rule
*/
double mid_point_rule(std::function<double(double)> f,
double a,
double b,
int intervals /*= 1001*/)
{
auto interval_size = (b - a) / intervals;
auto riemann_sum = 0.0;
for (auto i = 0; i < intervals; i++)
{
riemann_sum += f((a + interval_size / 2) +
i * interval_size);
}
riemann_sum *= interval_size;
return riemann_sum;
}
double trapezoidal_rule(std::function<double(double)> f,
double a,
double b,
int intervals /*= 1001*/)
{
auto interval_size = (b - a) / intervals;
auto trapezoidal_sum = 0.0;
for (auto i = 1; i < intervals; i++)
{
trapezoidal_sum += f(a + i * interval_size);
}
trapezoidal_sum += (f(a) + f(b)) / 2;
trapezoidal_sum *= interval_size;
return trapezoidal_sum;
}
double simpsons_rule(std::function<double(double)> f,
double a,
double b,
int intervals /*= 1001*/)
{
if (intervals % 2 == 0)
{
intervals += 1;
}
auto interval_size = (b - a) / intervals;
auto simpsons_sum = 0.0;
for (auto i = 1; i < intervals; i++)
{
if (i % 2 == 0)
{
simpsons_sum += 4 * f(a + i * interval_size);
}
else {
simpsons_sum += 2 * f(a + i * interval_size);
}
}
simpsons_sum += (f(a) + f(b));
simpsons_sum *= (interval_size / 3);
return simpsons_sum;
}