-
Notifications
You must be signed in to change notification settings - Fork 0
/
integrale.py
41 lines (26 loc) · 886 Bytes
/
integrale.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
"""
This program aim to approximate the value of a specific function using the rectangle method
Credits : @Sulaymane Dagnet
"""
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np
def aproxIntegrale(a,b,p):
f = lambda x: x
I = 0
coef = (b-a)/p
x1 = np.arange(a, b+coef, coef)
fig, ax = plt.subplots()
plt.plot(x1, f(x1), 'bo')
ax.plot(x1, f(x1), c="cyan")
for k in range(p):
x = k*coef
ax.add_patch(Rectangle((k*coef, 0), coef, f(k*coef), color="red", alpha=0.5))
I = I + f(x)*coef
ax.text(b/2, f(b)/2,"I ≃ "+str(round(I,3)), fontsize=20)
plt.title("Approximation d'une intégrale : méthode des rectangles")
ax.set_xlabel("x")
ax.set_ylabel("y")
plt.show()
return I
aproxIntegrale(0,25,100)