% ------------------------------------------------------------------------
% Gauss-Seidelova metoda na riesenie symetrickeho trojdiagonalneho systemu
% ------------------------------------------------------------------------
% 
% gs(a,b,ps,v0,eps)
%
% 3-diagonalna matica systemu ma:
% a - pod a nad diagonalou 
% b - na diagonale
%
% ps - prava strana systemu - stlpcovy vektor
% v0 - zaciatocna iteracia - stlpcovy vektor
%
% v - aproximacia riesenia (prepisuje sa v jednotlivych iteraciach)
% eps - kriterium pre ukoncenie iteracii: norm(Av-ps)<=eps

function [v] = gs(a,b,ps,v0,eps)

% matica A
pom = length(v0);
A(1, 1) = b;
for i = 2 : pom
    A(i, i) = b;
    A(i, i - 1) = a;
    A(i - 1, i) = a;
end

% cyklus bezi, kym nie je splnena podmienka norm(Av-ps)<=eps
v = v0;
while norm(A*v - ps) > eps
    v(1) = (ps(1) - a*v(2))/b; 
    for i = 2:pom-1
        v(i) = (ps(i) - a*(v(i-1) + v(i+1)))/b; 
    end
    v(pom) = (ps(pom) - a*v(pom - 1))/b;
end

