-
Notifications
You must be signed in to change notification settings - Fork 0
/
8_geometrical_intergation.py
72 lines (49 loc) · 1.91 KB
/
8_geometrical_intergation.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
import numpy as np
import matplotlib.pyplot as plt
from dill.source import getsource
def rectangleSum(fun, a, b, interv_num):
#integration with use of midpoint
dx = (b - a)/ interv_num
midpoint = np.linspace(dx/2,b - dx/2,interv_num)
result = np.sum(fun(midpoint) * dx)
x_lin = np.linspace(a,b,interv_num+1)
y_lin = fun(x_lin)
X = np.linspace(a,b,interv_num*interv_num+1)
Y = fun(X)
plt.plot(X,Y,'b')
x_mid = (x_lin[:-1] + x_lin[1:])/2
y_mid = fun(x_mid)
plt.plot(x_mid, y_mid,'b.', markersize=8)
plt.bar(x_mid, y_mid, width=dx, alpha=0.3, edgecolor='b')
plt.title('Intervals number = {}'.format(interv_num))
plt.show()
return result
def trapezoidSum(fun, a, b, interv_num):
dx = (b - a)/ interv_num
x_lin = np.linspace(a,b,interv_num+1)
y_lin = fun(x_lin)
y_right = y_lin[1:]
y_left = y_lin[:-1]
result = 0.5 * dx * np.sum(y_right + y_left)
X = np.linspace(a,b,interv_num*interv_num)
Y = fun(X)
plt.plot(X,Y)
for i in range(interv_num):
xs = [x_lin[i],x_lin[i],x_lin[i+1],x_lin[i+1]]
ys = [0,fun(x_lin[i]),fun(x_lin[i+1]),0]
plt.fill(xs,ys,'b',edgecolor='b',alpha=0.3)
plt.title('Intervals number = {}'.format(interv_num))
plt.show()
return result
if __name__ == "__main__":
a = float(input("Provide beginning value: "))
b = float(input("Provide closing value: "))
interv_num = int(input("Provide the precision: "))
print('Interval: [', a, ',', b, '] with partition of: ', interv_num)
#fun = lambda x: np.sin(x) + 1
#fun = lambda x: np.sqrt(x)
#fun = lambda x: np.exp(-x**2)
fun = lambda x : 1/ (1 + x**2)
print('For function: ', getsource(fun), '\nResults:')
print( rectangleSum(fun, a, b, interv_num), 'for rectangles method')
print( trapezoidSum(fun, a, b, interv_num), 'for tradezoids method')