Vous êtes sur la page 1sur 2

1 import numpy as np

3 def gauss_seidel(A, B, Eps):


4 D = np. linalg. det (A)
5 if D == 0:
6 print ("Pas de solution, déterminant égal à zéro!")
7 return None, None
8
9 M, N = A.shape
10 if M != N:
11 print("Pas de solution, il faut introduire une matrice carrée!")
12 return None, None
13
14 for i in range (N) :
15 SommeI = np.sum (np.abs (A[i, :i])) + np. sum(np.abs(A[i,i+1:]))
16
17
if np.abs(A[i, i]) < SommeI
print ("Lamatrice n'est pas
: à diagonale dominante!")
18 return None, None
19
20 x = np. zeros(N)
21
22 erreur = Eps + 1
24 while erreur > Eps:
25 m = 1
26 y = (x)
np.copy
27 for i in range(N):
28 x[i] = B[i]
29 for j in range(i):
30 x[i] -= A[i, j] * x[j]
31 for j in range(i+1, N):
32 x[i] -= A[i, j] * x[j]
32 x[i] /= A[i, i]
34 erreur = np.max (np.abs (y - x))
35
36 return x, m
37
38 # Exemple d 'utilisation
39 A = np.array([[5, 1, 2, -1],
40 [0, 4, 1, -1],
41 [-1, 1, 5, 1],
42 [1, 2, 0, 6]])
43 B = np.array([7, -2, 2, -7])
44 x, iterations = gauss_seidel (A, B, 1E-3)
45 print ("Solution obtenue:", x)
46 print ("Nombre d'itérations :", iterations)
47

Vous aimerez peut-être aussi