gauss.py

#
# Gauss-Seidel iterative scheme
# Vincent Legat - 2018
# Ecole Polytechnique de Louvain
#

from numpy import *
from scipy.linalg import norm

def g(x): 
  x[0] =  (-3*x[1]+6)/5
  x[1] =  -(6*x[0]-4)/8
  return x

def gaussseidel(x,tol,nmax):
  n = 0; delta = tol+1;  
  x = array(x,dtype=float)  
  while (norm(delta) > tol and n < nmax):
    n = n+1
    xold = x.copy()
    x = g(x)
    delta = x - xold
    print(" Estimated error %14.7e at iteration %d" % (norm(delta),n))
  return x

print(gaussseidel([0,0],10e-6,50))