% algorithme de Gauss-Seidel pour les matrices tridiagonales
%----------------------------------------------------------
function [err, x_new] = gaussseidel(A, b, max_iter)

% parametres:
%  - A:             matrice tridiagonal
%  - b:             rhs
%  - max_iter:      nombre maximal d'itérations

% initialization
N = length(A);
x_old = zeros(N,1);
x_new = zeros(N,1);


for n = 1 : max_iter
    x_new(1) = (??? - ??? * x_old(2)) / A(1,1);
    for i = 2 : N-1
        x_new(i) =(b(i) - ??? * ??? - ??? * ???) / A(i,i);
    end
    x_new(N) = (b(N) - A(N,N-1) * x_new(N-1)) / A(N,N);

    discrepance = norm(x_new - x_old) / norm(x_new);
  
    if discrepance < 0.0001
        fprintf(' convergence obtenue a l''iteration %d \n',n)
        break
    else
        x_old = x_new;
        fprintf(' iteration %d discrepance %e \n', n, discrepance)
    end
end
end

dim = 10;

A = ???;
b = ones(dim, 1);

[err, x] = gaussseidel(A, b, 1000)

% Calcul erreur final
for i = 1 : N
    err(i) = x(i) - i * (N + 1 - i) / 2;
end

fprintf(' erreur relative %e \n', norm(err) / norm(x))
