import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Fichier Geogebra : somme des rectangles pour la parabole $y=x^2$sur [0;1]
Que représentent les variables u
et v
dans la fonction ci-dessous ?
def f(x):
return x ** 2
def sommeRectangle(a, b, n):
h = (b - a) / n
x = a
u = 0
v = 0
for k in range(0, n - 1): #n subdivisions => n tours
u = u + h * f(x)
x = x + h
v = v + h * f(x)
return (u, v)
sommeRectangle(0, 1, 10)
(0.20399999999999996, 0.2849999999999999)
Compléter les fonctions Python ci-dessous pour qu'elles retournent une approximation de $\int_{a}^{b}f(x) \mathrm{dx}$ par la somme de rectangles à gauche construits sur $n$ subdivisions régulières de l'intervalle $[a;b]$.
def rectangleGauche(f, a, b, n):
s = 0
pas = (b - a)/n
x = a
for k in range(n):
s = '..............'
x = '..............'
return s
def rectangleGauche2(f, a, b, n):
s = 0
pas = (b - a) / n
for k in range(n):
s = '..............'
return s
def rectangleGaucheDessin(f, a, b, n):
s = 0
pas = (b - a)/n
x = a
for k in range(n):
s = s + f(x) * pas
plt.fill([x, x + pas, x + pas, x, x], [0] * 2 + [f(x)] * 2 + [0], hatch='/',edgecolor='black', facecolor='none')
x = x + pas
lesx = np.linspace(a, b, 1000)
fvect = np.vectorize(f)
ax = plt.gca()
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',a))
plt.plot(lesx, fvect(lesx), color='black')
plt.title(r"Rectangles à gauche $\int_{%d}^{%d}f(x)dx\approx$%1.3f"%(a,b,s))
plt.savefig('methodeRectangleGauche-{}-{}-{}-{}subdivisions.eps'.format(f.__name__,a,b,n))
plt.show()
return s
Écrire une fonction rectangleDroite(f, a, b, n)
qui retourne une approximation de $\int_{a}^{b}f(x) \mathrm{dx}$ par la somme de rectangles à droite construits sur $n$ subdivisions régulières de l'intervalle $[a;b]$.
def rectangleDroite(f, a, b, n):
s = 0
pas = (b - a)/n
x = a + pas
for k in range(n):
s = '..............'
x = '..............'
return s
def rectangleDroite2(f, a, b, n):
s = 0
pas = (b - a) / n
for k in range(1, n + 1):
s = '..............'
return s
def rectangleDroiteDessin(f, a, b, n):
s = 0
pas = (b - a)/n
x = a
for k in range(n):
s = s + f(x + pas) * pas
plt.fill([x, x + pas, x + pas, x, x], [0] * 2 + [f(x+pas)] * 2 + [0], hatch='/',edgecolor='black', facecolor='none')
x = x + pas
lesx = np.linspace(a, b, 1000)
fvect = np.vectorize(f)
ax = plt.gca()
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',a))
plt.plot(lesx, fvect(lesx), color='black')
plt.title(r"Rectangles à droite $\int_{%d}^{%d}f(x)dx\approx$%1.3f"%(a,b,s))
plt.savefig('methodeRectangleDroite-{}-{}-{}-{}subdivisions.eps'.format(f.__name__,a,b,n))
plt.show()
return s
def inverse(x):
return 1 / x
inverse.__name__
'inverse'
rectangleGauche(inverse, 1, 2, 10)
'..............'
rectangleGauche2(inverse, 1, 2, 10)
'..............'
rectangleGaucheDessin(inverse,1,2,10)
0.7187714031754279
def carre(x):
return x ** 2
rectangleGaucheDessin(carre,0,1,10)
0.2849999999999999
rectangleDroiteDessin(carre, 0, 1, 10)
0.3849999999999999