Académique Documents
Professionnel Documents
Culture Documents
Champollion 2013-2014
Méthodes d’Analyse Numériques
Implémentation et Application en Python
Intégration Numérique
A. HASSAN
23 avril 2014
A. Hassan@Champollion PCSI-MPSI – 1
Intégration Numérique
(Quadrature)
L’intégration : une histoire
d’Aire
L’intégration : une
Histoire d’Aire
Introduction
Pourquoi l’intégration
numérique?
Le problème
Le Problème (suite)
a
rectangle
b
triangle rectangle
carrée a.b 1
2 ab
a2
A. Hassan@Champollion PCSI-MPSI – 3
L’intégration : une Histoire d’Aire
2 b
R
Cf Aire = f(x) dx
1
a
b
-2
a -1 -1 1 2 3 Cf
−n→+∞
−−−−→
b
a
Cf
b
a
A. Hassan@Champollion PCSI-MPSI – 4
Introduction
Intégrale (de Riemann) sur un segment : f est intégrable sur [a; b], ssi pour toute
subdivision σn = ( x0 < x1 < · · · < xn ) les sommes ci-dessous convergent
|{z} |{z}
=a =b
x=b
i=n−1 Z
X ∀ti ∈[xi ;xi+1 ]
−−−−−−→ L noté
(xi+1 − xi )f(ti ) −−−n→+∞ f(x) dx
i=0 x=a
Zb
f(t) dx = F(b) − F(a)
a
A. Hassan@Champollion PCSI-MPSI – 5
Pourquoi l’intégration numérique ?
1 + t4 8 2
x −x 2+1 Méthodes d’ordre zéro:
Exemple des Rectangles
0 gauches
√ √ √ Méthodes d’ordre zéro:
2
h i Rectangles Droits
+ arctan x 2 + 1 + arctan x 2 − 1 Méthodes des Rectangles
4 Milieux
Implémentation en
Python (à compléter)
3. Expression analytique de f non disponible : f est donnée Implémentation en Python
sous forme d’une table de valeurs i.e. (xi ; f(xi )) (Exemple : Méthode des rectangles:
Calcul de l’erreur
mesures physiques). Rectangle des milieux:
Calcul de l’erreur
Méthodes d’ordre un:
méthode des Trapèzes
A. Hassan@Champollion Méthode des Trapèzes:
PCSI-MPSI –6
Calcul de l’erreur
Méthode d’ordre 2:
Le problème
b
R i=n
P
Problème : Trouver (ωi )06i6n tels que I(f) = f(x) dx ≈ S = ωi f(xi )
a i=0
A. Hassan@Champollion PCSI-MPSI – 8
Polynômes d’interpolation de Lagrange
Soit les n + 1 points (xk , f(xk ))06k6n tels que : x0 < x1 < x2 < · · · < xn
Trouver P ∈ Kn [X] tel que P(xk ) = f(xk ) pour k ∈ [[ 0, n ]] ?.
Une solution(polynômes de Lagrange) :
n
Q
(X − xj )
16j6n
k6=j 1 si k = i
Lk (X) = ⇒ Lk (xi ) = δki =
n
Q 0 sinon
(xk − xj )
16j6n
k6=j
Les (Lk )16k6n forment une base de Kn [X] (le démontrer ? ?),
k=n
X
P(X) = f(x0 ).L0 (X) + f1 (x1 ).L1 (X) + · · · + f(xn ).Ln (X) = f(xk ).Lk (X)
k=0
A. Hassan@Champollion PCSI-MPSI – 9
Méthodes Élémentaires de Quadrature (de Newton-Cotes)
P0 (x) = f( (xi + xi+1 )), Rectangles milieux . . . . . . . . . . . . . Ordre 1
2
A. Hassan@Champollion PCSI-MPSI – 10
(Suite)
On verra que cette méthode est d’ordre 1, i.e. elle est exacte pour les fonctions
affines (polynômes de degré 1)
A. Hassan@Champollion PCSI-MPSI – 11
(suite)
Méthode d’ordre 2, ou de S IMPSON :
Polynôme interpolateur de degré 2 (parabole). On interpole les points
(xi , f(xi )), (xi+1 ; f(xi+1 )) et (xi+2 ; f(xi+2 ))
A. Hassan@Champollion PCSI-MPSI – 12
Méthodes d’ordre zéro : Exemple des Rectangles gauches
Cf
xi+1
R xi+1
R
Intervalle élémentaire : Ie = f(t) dt ≈ P0 (t) dt ≈ (xi+1 − xi )f(xi ) (i.e.
xi xi
évaluation de f à gauche).
Zb n−1
X n−1
X
Il vient : I(f) = f(x)dx ≈ SR = (xi+1 − xi )f(xi ) = h yi
a i=0 i=0
A. Hassan@Champollion PCSI-MPSI – 13
Méthodes d’ordre zéro : Rectangles Droits
Cf
xi+1
R
Intervalle élémentaire : f(t) dt ≈ (xi+1 − xi )f(xi+1 ) (i.e. évaluation de f à
xi
gauche).
Zb xZ
i+1
n−1
X n−1
X n−1
X
Il vient : f(t)dt ≈ P0 (t) dt = (xi+1 − xi )f(xi+1 ) = h yi+1
a i=0 xi i=0 i=0
A. Hassan@Champollion PCSI-MPSI – 14
Méthodes des Rectangles Milieux
bc
Affine⇒Méthode exacte
bc bc
bc
bc bc
bc bc bc bc bc bc
1
xi 2 (xi + xi+1 ) xi+1
xi+1
R
Intervalle élémentaire f(t)dt ≈ (xi+1 − xi )f( 12 (xi + xi+1 ))
xi
b n−1 n−1
X
R P h
f(t)dt ≈ (xi+1 − xi )f( 12 (xi + xi+1 )) = h f(xi + )
a i=0 2
i=0
A. Hassan@Champollion PCSI-MPSI – 15
Implémentation en Python (à compléter)
Intégration Numérique
Rectangles Gauches : Rectangles Droits : (Quadrature)
L’intégration : une histoire
1 def LeftBox (f ,a ,b , N ): 1 def RightBox (f ,a ,b , n ): d’Aire
L’intégration : une
2 s ,x ,h =0 ,a ,( b - a )/ n 2 s ,x , h =............ Histoire d’Aire
3 .................. 3 .................. Introduction
4 .................. 4 .................. Pourquoi l’intégration
5 .................. 5 .................. numérique?
6 .................. 6 .................. Le problème
Le Problème (suite)
7 return s 7 return s
Polynômes d’interpolation
de Lagrange
Rectangles des Milieux : Méthodes Élémentaires de
Quadrature (de
1 def MiddleBox (f ,a ,b , n ): Newton-Cotes)
2 s ,x , h =0 ,a ,( b -a )/ n (Suite)
3 .................. (suite)
Méthodes d’ordre zéro:
4 .................. Exemple des Rectangles
5 .................. gauches
6 .................. Méthodes d’ordre zéro:
7 .................. Rectangles Droits
8 return s Méthodes des Rectangles
Milieux
Implémentation en
Python (à compléter)
Implémentation en Python
Méthode des rectangles:
Calcul de l’erreur
Rectangle des milieux:
Calcul de l’erreur
Méthodes d’ordre un:
méthode des Trapèzes
A. Hassan@Champollion Méthode des Trapèzes:
PCSI-MPSI – 16
Calcul de l’erreur
Méthode d’ordre 2:
Implémentation en Python
2n
a i=0
(b − a)2
Pour une précision de ε on prend n > M
2ε
Remarque : La méthode des rectangles-milieux est d’ordre 1.
A. Hassan@Champollion PCSI-MPSI – 18
Rectangle des milieux : Calcul de l’erreur
h h h 1 h
f(t) = f(xi + ) + (t − xi − )f ′ (xi + ) + (t − xi − )2 f ′′ (ci )
2 2 2 2 2
xiZ+h xiZ+h
h h h 1 h
f(t) dt − hf(xi + ) = (t − xi − )f ′ (xi + ) + (t − xi − )2 f ′′ (ci )dt
2 2 2 2 2
xi xi
1
xiZ+h 2h
Z
chang. var h
===============⇒ (t − xi − ) dt = u du = 0 et si sup |f ′′ (x)| 6 M
u=t−xi − 2 h1
2 x∈[a;b]
xi − 12 h
x
i+1
Z
h M 3
f(x) dx − hf(xi + ) 6
h
2 3
xi
b
Z n−1 3
X h M 3 (b − a)
Par sommation : f(x) dx − h
f(xi + ) 6 × nh = M
2 3 3n2
a i=0
A. Hassan@Champollion PCSI-MPSI – 19
Méthodes d’ordre un : méthode des Trapèzes
bc
bc
Cf
h
xi xi+1
xi+1
R 1 h
Ie = f(t)dt ≈ (xi+1 − xi ) .(yi + yi+1 ) = (y + yi+1 )
xi 2 2 i
Zb
h
f(t) dt ≈ S = [(y0 + y1 ) + (y1 + y2 ) + · · · + (yn−1 + yn )]
2
a
n
" #
n−1
1 X h 1 P
S = h − (y0 + yn ) + yi ⇒ S = (y + yn ) + yi
2 2 2 0 1
0
A. Hassan@Champollion PCSI-MPSI – 20
Méthode des Trapèzes : Calcul de l’erreur
(b − a)3
Théorème : Si f est C2 telle que sup |f ′′ (t)|< M alors |I(f) − S| 6 M
12n2
1
Preuve : Soit [xi ; xi+1 ] = [xi ; xi + h], L2 (x) = [(x − xi )yi+1 − (x − xi+1 )yi ]
h
1
ϕ(t) = f(t) − M(t − xi )(xi+1 − t) ⇒ ϕ ′′ (t) = f ′′ (t) + M > 0 ⇒ ϕ convexe
2
1
ψ(t) = f(t) − M(t − xi )(xi+1 − t) ⇒ ψ est concave
2
ϕ en dessous de L2 et ψ en dessus
1 1 t∈[xi ;xi+1 ]
f(t) + M(t − xi )(xi+1 − t) 6 L2 < f(t) − M(t − xi )(xi+1 − t)⇐===========⇒
2 2
x +h
iZ
1
par M
f(t) − L2 (t) dt 6 (xi+1 − xi )3
|f(t) − L2 (t)| 6 M(t − xi )(xi+1 − t) ================ ⇒
2 intégration
12
xi
x=b
Z i=n−1X M M
En sommant : f(x) dx 6 (xi+1 − xi )3 ⇒ |I(f) − S| 6 3
(b − a)
12 | {z } 12n2
x=a
i=0 1
=h= n (b−a)
A. Hassan@Champollion PCSI-MPSI – 21
Méthode d’ordre 2 : S IMPSON
bc bc
bc
CL2 bc CP
bc bc
Cf Cf
h h
Ie = f(x) dx ≈ L2 (x) dx
xi xi
X
I≈ ()
Erreur : ε 6 h3
A. Hassan@Champollion PCSI-MPSI – 22
Méthode Simpson : Démonstration
deg(L2 ) = 2
yi = f(xi ) = L2 (xi )
Preuve : Soit [xi ; xi+2 ] tel que
yi+1 = f(xi+1 ) = L2 (xi+1 )
yi+2 = f(xi+2 ) = L2 (xi+2 )
xZ
i+2 xZ
i+2
Ie = f(x) dx ≈ L2 (x) dx
xi xi
Zh
Par 2
⇒ Ie ≈ 2
======== at2 + c dt = a.h3 + 2ch
Parité 3
t=0
A. Hassan@Champollion PCSI-MPSI – 23
Méthode de Simpson : (Suite)
h
2 yi +yi+2 =2ah2 +2c
h
Ie ≈ 2ah + 6c ====================
===
==⇒ Ie ≈ (yi + 4yi+1 + yi+2 )
3 2
yi +yi+2 =2ah +2yi+1 3
Il vient :
h
S= [y + 4y1 + y2
3 0
+ y2 + 4y3 + y4
+ y4 + 4y5 + y6
.. .. ..
+ . + . + .
+y2n−2 + 4y2n−1 + y2n ]
b−a
Avec un nombre pair de subdivisions, h = :
2n
Zb " n−1
X
#
h
f(x) dx ≈= y2n − y0 + 2y2i + 4y2i+1
3
a i=0
A. Hassan@Champollion PCSI-MPSI – 24
Mise en œuvre de la méthode de S IMPSON(à compléter)
(b − a)
On choisit un nombre pair d’intervalles ⇒ h = (donc 2n + 1 points).
2n
1
SINON : l’interpolation est faite sur les points : xi ; mi = (xi + xi+1 ) et xi+1 et
2
xi+1
R h
Ie = f(t) dt ≈ (yi + 4f(mi ) + yi+1 )
xi 6
1 ##
2 def Simpson (f ,a ,b , n ):
3 n2 =2* n # nombre pair de points
4 s ,h =0 ,( b -a )/ n2 # initialisation
5 x1 = a # abscisses d ’ ordre pair
6 f o r :::::::::::::
7 .................
8 .................
9 .................
10 .................
11 .................
12 r e t u r n s *h /3
A. Hassan@Champollion PCSI-MPSI – 25
Mise en œuvre de la méthode de S IMPSON
(b − a)
On choisit un nombre pair d’intervalles ⇒ h = (donc 2n + 1 points).
2n
1
SINON : l’interpolation est faite sur les points : xi ; mi = (xi + xi+1 ) et xi+1 et
2
xi+1
R h
Ie = f(t) dt ≈ (yi + 4f(mi ) + yi+1 )
xi 6
1 ##
2 def Simpson (f ,a ,b , n ):
3 n2 =2* n # nombre pair de points
4 s ,h =0 ,( b -a )/ n2 # initialisation
5 x1 = a # abscisses d ’ ordre pair
6 f o r i in range (0 , n ):
7 s += 2* f ( x1 )+4* f ( x1 + h)
8 x1 = x1 +2* h
9 s += f( b ) -f ( a)
10 r e t u r n s *h /3
A. Hassan@Champollion PCSI-MPSI – 26
Erreur de la méthode de Simpson, méthodes de
Newton-Cotes
Théorème[admis] : Si la dérivée d’ordre 4 de f vérifie sup |f(4) (x)| 6 M alors
x∈[a;b]
(b − a)5 b−a
|I(f) − Sp | 6 M avec h =
n4 n
A. Hassan@Champollion PCSI-MPSI – 27
Comparaison des méthodes
A. Hassan@Champollion PCSI-MPSI – 28
Résultats
A. Hassan@Champollion PCSI-MPSI – 29
Erreurs d’intégration : Tableau comparatif
Définition : On dit que f = O(g) (ou O), f est "grand Oh" de g au voisinage de a,
a a
ssi il existe K > 0 tel que
Méthode Erreur
1
Rectangles Gauches (ou Droits) O
+∞ n
1
Rectangles Milieux O
+∞ n2
1
Trapèzes O
+∞ n2
1
Simpson O 4
+∞ n
A. Hassan@Champollion PCSI-MPSI – 30
L’instruction quad du module scipy.integrate
A. Hassan@Champollion PCSI-MPSI – 31
Exemple : Période du pendule simple
π
R2du
La fonction x 7→ K(x) = q , s’appelle fonction intégrale elliptique, elle
0 1 − x2 sin2 (u)
ne peut s’exprimer à l’aide des fonctions usuelles(sinon on l’aurait su).
π
du 2
r
L R2
T =4 = T .K(sin(θ0 /2))
π 0
q
g 0
1 − sin2 ( θ20 ) sin2 (u)
Quelques approximations :
r
L
(•) θ0 ≈ 0 ⇒ T ≈ T0 = 2π (formule connue)
g
DL 1 1
(•) θ0 petit ========= ⇒ (1 − sin2 (θ0 /2) sin2 (u))− 2 ≈ 1 +
========= sin2 (θ0 /2) sin2 (u)
α
(1+u) ≈1+αu 2 | {z }
1 2
2 θ0
En intégrant 1 2
=========
R= ⇒ T ≈ T0 (1 +
=================== θ ) [Formule de Bordas]
avec 0
π/2
sin2 (u) du= π
4
16 0
A. Hassan@Champollion PCSI-MPSI – 33
Implémentation en Python et comparaison
1 #
2 import numpy as np
3 import scipy . integrate as spi
4 import matplotlib . pyplot as mp
5 # ------------ -- -- - -- -- -- -- -- - --
q
2
6 def fellip (x ,u ): #x 7→ 1/ 1 − u2 . sin (x)
2 θ20
Courbe de T (θ0 ) = T0 K(sin(θ0 /2)) et Ta (θ0 ) = T0 (1 + ) (en pointillé)
π 16
A. Hassan@Champollion PCSI-MPSI – 35