ENSA-Tetouan 2024-2025
GC2- GM2 [Link]
TP: Méthodes itératives de résolution AX = b
1 Documentations et installation de Python avec Anaconda
Anaconda est un outil de distribution Python open source utilisé pour la gestion de packages et d ?environnement
de développement. Il sert à la réalisation de projets Python et de langages de programmation connexes, tel que
R et Julia. Anaconda simplifie le processus de configuration et de gestion de différents packages et bibliothèques
Python, ce qui en fait un outil indispensable pour les développeurs et les data scientist. Son importance en
data science est marquée par les packages qu ?il propose comme : Numpy, Panda, Jupyter et Python. On peut
aussi directement gérer ses environnements de développement et ses packages depuis son outil ?conda ?. Il est
également multi-plateforme, ce qui permet de l ?installer sur Linux, Windows ou MacOS. Pour l’installation de
Anaconda consulter le site suivant :
[Link]
2 Résolution des systèmes linéaires par les méthodes itératives
Exercice 1. 1. Écrire le code de la méthode itérative de Jacobi avec un test d’arrêt des itérations :
Variables d’entrées: (n, A, b, X (0) , ε, N max)
Résultats obtenus: k, X (k)
2. Application :
4 1 2 4
(1) A =
3 5 1
b=
7
1 1 3 3
10 −1 2 0 6
−1 11 3 −1
b = 25
(2) A=
2
−1 10 −1
−11
0 3 0 8 15
1 0.5 0.5 4
(3) A=
0.5 1 0.5 b = −1
0.5 0.5 1 1
Exercice 2. Répondre aux même questions pour la méthode itérative de Gauss-Seidel avec un test d’arrêt des
itérations.
Exercice 3. Écrire le code de la méthode itérative de Relaxation avec un test d’arrêt des itérations.
1
Code Python :Méthode itérative de Jacobi
import numpy a s np
def j a c o b i (A, b , x0 , t o l , i t m a x ) :
n=len ( b ) # t a i l l e de s y s t e m e
x=x0 . copy ( ) # i n i t i a l i s a t i o n
i t e r =0
i n f o =0 # c r i t e r e d ' a r r e t | | Ax−b | | p r o c h e de 0
while i t e r <i t m a x and np . l i n a l g . norm ( np . dot (A, x)−b)> t o l :
i t e r+=1
x=np . dot ( np . l i n a l g . i n v (M) , np . dot (N, x)+b )
i f i t e r <i t m a x :
i n f o =0
return [ x , i t e r , i n f o ]
# Example u s a g e
A = np . a r r a y ( [ [ 4 , 1 , 2 ] ,
[3 , 5 , 1] ,
[1 , 1 , 3]])
b = np . a r r a y ( [ 4 , 7 , 3 ] )
M=np . d i a g ( np . d i a g (A) )
N=M−A
x0 = np . z e r o s ( 3 )
t o l = 1 e−6
i t m a x= 100
[ x , i t e r , i n f o ]= j a c o b i (A, b , x0 , t o l , i t m a x )
print ( f ' S o l u t i o n : { x} ' )
print ( f ' l a c o n v e r g e n c e e s t a t t e i n t e a p r e s { i t e r } I t e r a t i o n s . ' )
t e s t=np . dot (A, x)−b
print ( f ' Test : { t e s t } ' )
2
import numpy a s np
def j a c o b i (A, b , x0 , t o l , m a x i t e r ) :
n = len ( b )
x = x0 . copy ( )
x new = x0 . copy ( )
it = 0
while i t < m a x i t e r :
f o r i in range ( n ) :
s = sum(A[ i ] [ j ] ∗ x [ j ] f o r j in range ( n ) i f j != i )
x new [ i ] = ( b [ i ] − s ) / A[ i ] [ i ]
i f np . l i n a l g . norm ( x new − x , ord=np . i n f ) < t o l : # t e s t a r r e t
return x new , i t + 1
x = x new . copy ( )
i t += 1
return [ x , i t ]
# Example u s a g e
A = np . a r r a y ( [ [ 4 , 1 , 2 ] ,
[3 , 5 , 1] ,
[1 , 1 , 3]])
b = np . a r r a y ( [ 4 , 7 , 3 ] )
M=np . d i a g ( np . d i a g (A) )
N=M−A
Bj=np . dot ( np . l i n a l g . i n v (M) ,N)
#t e s t de c o n v e r g e n c e
r h o j=np .max( abs ( np . l i n a l g . e i g v a l s ( Bj ) ) )
print ( f ' l e rayon s p e c t r a l de l a m t r i c e de j a c o b i vaut ' , r h o j )
#parametre de l a f o n c t i o n j a c o b i
x0 = np . z e r o s ( 3 )
t o l = 1 e−6
m a x i t e r = 100
#a f f i c h e r l a s o l u t i o n e t nombre d ' i t e r a t i o n s
[ xj , i t ]= j a c o b i (A, b , x0 , t o l , m a x i t e r )
t e s t=np . dot (A, x j )−b
print ( f ' S o l u t i o n : { x j } ' )
print ( f ' l a c o n v e r g e n c e e s t a t t e i n t e a p r e s { i t } i t e r a t i o n s . ' )
print ( f ' Test : { t e s t } ' )
3
Code Python : Méthode de Gauss-Seidel
import numpy a s np
def G a u s s S e i d e l (A, b , x0 , t o l , i t m a x ) :
n=len ( b ) # t a i l l e de s y s t e m e
x=x0 . copy ( ) # i n i t i a l i s a t i o n
i t e r =0
i n f o =0 # c r i t e r e d ' a r r e t | | Ax−b | | p r o c h e de 0
while i t e r <i t m a x and np . l i n a l g . norm ( np . dot (A, x)−b)> t o l :
i t e r+=1
x=np . dot ( np . l i n a l g . i n v (M) , np . dot (N, x)+b )
i f i t e r <i t m a x :
i n f o =0
return [ x , i t e r , i n f o ]
# Example u s a g e
A = np . a r r a y ( [ [ 4 , 1 , 2 ] ,
[3 , 5 , 1] ,
[1 , 1 , 3]])
b = np . a r r a y ( [ 4 , 7 , 3 ] )
M=np . t r i l (A)# m a t r i c e t r i a n g u l a i r e i n f e r i e u r e x t r a i t e de A
N=M−A
x0 = np . z e r o s ( 3 )
t o l = 1 e−6
i t m a x= 100
[ x , i t e r , i n f o ]= G a u s s S e i d e l (A, b , x0 , t o l , i t m a x )
print ( f ' S o l u t i o n : { x} ' )
print ( f ' l a c o n v e r g e n c e e s t a t t e i n t e a p r e s { i t e r } I t e r a t i o n s . ' )
t e s t=np . dot (A, x)−b
print ( f ' Test : { t e s t } ' )