-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegral_calculator.py
142 lines (96 loc) · 3.64 KB
/
integral_calculator.py
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
from math import sin, pi, exp
function = sin # (0, pi) 2
function1 = lambda x: x / (x ** 4 + 16) ** 0.5 # (0, 3**0.5) ~0,.35
function2 = lambda x: exp(1 / x) / x ** 2 # (1, 2) e - e ** 0.5
function3 = lambda x: exp(-x ** 2)
def rectangle_method(lower_bound:float, upper_bound:float,
interval_count: int, function):
if interval_count == 0:
return
if lower_bound > upper_bound:
lower_bound, upper_bound = upper_bound, lower_bound
result = 0
step = (upper_bound - lower_bound) / interval_count
while lower_bound < upper_bound:
result += function(lower_bound) * step
lower_bound += step
return result
def trapezium_method(lower_bound:float, upper_bound:float,
interval_count:int, function):
if interval_count == 0:
return
if lower_bound > upper_bound:
lower_bound, upper_bound = upper_bound, lower_bound
result = 0
step = (upper_bound - lower_bound) / interval_count
while lower_bound < upper_bound:
result += (function(lower_bound) + function(lower_bound + step))\
/ 2 * step
lower_bound += step
return result
def simpson_method(lower_bound:float, upper_bound:float,
interval_count:int, function):
if interval_count == 0:
return
if lower_bound > upper_bound:
lower_bound, upper_bound = upper_bound, lower_bound
result = 0
step = (upper_bound - lower_bound) / interval_count
while lower_bound < upper_bound:
result += step / 3 * (
function(lower_bound) +
4 * function(lower_bound + step) +
function(lower_bound + 2 * step)
)
lower_bound += 2 * step
return result
def floating_quad(lower_bound:float, upper_bound:float,
interval_count:int, function, precision:float=0.01):
if interval_count == 0:
return
if lower_bound >= upper_bound:
lower_bound, upper_bound = upper_bound, lower_bound
result = 0
step = (upper_bound - lower_bound) / interval_count
while lower_bound < upper_bound:
inner_step = step
delta = abs(function(lower_bound + inner_step) - function(lower_bound))
while delta > precision:
inner_step = delta / interval_count
delta = function(lower_bound + inner_step) - function(lower_bound)
inner_upper = lower_bound + step
while lower_bound < lower_bound + inner_step:
result += function(lower_bound) * inner_step
lower_bound += inner_step
return result
if __name__ == "__main__":
rm = rectangle_method(0, pi, 10000, function)
tm = trapezium_method(0, pi, 10000, function)
sm = simpson_method(0, pi, 10000, function)
#fm = floating_quad(0, pi, 10000, function, precision=1e-3)
print(f"Rectangle method: {rm}, delta: {abs(2 - rm)}")
print(f"Trapezium method: {tm}, delta: {abs(2 - tm)}")
print(f"Simpson method: {sm}, delta: {abs(2 - sm)}")
print()
#print(f"Floating quad: {fm}, delta: {abs(2 - fm)}")
rm = rectangle_method(0, 3 ** 0.5, 10000, function1)
tm = trapezium_method(0, 3 ** 0.5, 10000, function1)
sm = simpson_method(0, 3 ** 0.5, 10000, function1)
print(f"Rectangle method: {rm}, delta: {abs(0.35 - rm)}")
print(f"Trapezium method: {tm}, delta: {abs(0.35 - tm)}")
print(f"Simpson method: {sm}, delta: {abs(0.35 - sm)}")
print()
rm = rectangle_method(1, 2, 10000, function2)
tm = trapezium_method(1, 2, 10000, function2)
sm = simpson_method(1, 2, 10000, function2)
print(f"Rectangle method: {rm}, delta: {abs((exp(1) - exp(0.5)) - rm)}")
print(f"Trapezium method: {tm}, delta: {abs((exp(1) - exp(0.5)) - tm)}")
print(f"Simpson method: {sm}, delta: {abs((exp(1) - exp(0.5)) - sm)}")
print()
rm = rectangle_method(0, 2, 10000, function3)
tm = trapezium_method(0, 2, 10000, function3)
sm = simpson_method(0, 2, 10000, function3)
print(f"Rectangle method: {rm}")
print(f"Trapezium method: {tm}")
print(f"Simpson method: {sm}")
print()