# -*- coding: utf-8 -*- """ Created on Tue Dec 23 16:17:33 2014 @author: grivet Listing 6.6 : Résolution d'un système d'équations linéaires par la méthode itérative de surrelaxation omg environ 1.3 pour une convergence rapide """ import numpy as np def affiche_mat(titre,M): print (titre) for row in M: for val in row: print( ' %10.5f' %val,end = '') print() seuil = 10**(-6) N = 5; A = np.random.rand(N,N)-0.5 A = A + A.transpose()+2.0*np.eye(N,N) affiche_mat('A = ',A) print () b = range(N) x = np.ones(N); y = np.ones(N) kmax = 20 omg = float(input('parametre de relaxation: ')) for k in range(kmax): for i in range(N): s = np.dot(A[i,:],x) x[i] = x[i] + omg*(b[i]-s)/A[i,i] if max(abs(y-x)) < seuil: break y = np.copy(x) print ('nombre d iterations: ',k) print ('solution: ', x) print ('vecteur d erreurs') print (np.dot(A,x) - b)