LaDissertation.com - Dissertations, fiches de lectures, exemples du BAC
Recherche

Calcul intégral, méthode des rectangles et trapèzes (programmation)

TD : Calcul intégral, méthode des rectangles et trapèzes (programmation). Recherche parmi 298 000+ dissertations

Par   •  15 Mars 2019  •  TD  •  563 Mots (3 Pages)  •  587 Vues

Page 1 sur 3

%matplotlib inline

import matplotlib.pyplot as plt

import numpy as np

def fonc(x):

return np.cos(x)

xx = np.linspace(0,np.pi/2,100)

plt.plot(xx,fonc(xx),'-r')

plt.title("Cosinus")

plt.xlabel("x")

plt.ylabel("f(x)")

def Irect(f,a,b,n):

h = (b-a)/n

x = a

somme = 0

for i in range(n):

somme = somme + h*f(x)

plt.plot([x,x,x+h,x+h],[0,f(x),f(x),0],'-g')

x = x + h

return somme

%matplotlib inline

import matplotlib.pyplot as plt

import numpy as np

def fonc(x):

return np.cos(x)

xx = np.linspace(0,np.pi/2,100)

plt.plot(xx,fonc(xx),'-r')

plt.title("Cosinus")

plt.xlabel("x")

plt.ylabel("f(x)")

def Itrapeze(f,a,b,n):

h = (b-a)/n # calcul du pas

x = a # valeur initiale de x

S = 0 # initialisation de l'intégrale

for i in range(n): # de 0 à n-1

S = S + h*(f(x)+f(x+h))/2

plt.plot([x,x,x+h,x+h],[0,f(x),f(x+h),0],'g-') # pour les plus rapides

x = x + h # ou x += h

return S

def ItrapezeComposite(f,a,b,n): # pour optimiser le calcul (non demandé ici)

h = (b-a)/n # calcul du pas

x = a # valeur initiale de x

S = (f(a)+f(b))/2 # initialisation de l'intégrale

for i in range(1,n): # de 1 à n-1

x += h

S += f(x)

return S*h

...

Télécharger au format  txt (1.5 Kb)   pdf (25.8 Kb)   docx (7.5 Kb)  
Voir 2 pages de plus »
Uniquement disponible sur LaDissertation.com