%  Reseni soustavy rovnic metodou SOR
%
%  Jan Nejedly FAV2
%
%  10.4.97
%
%
%  Vstup:  A - matice soustavy
%          b - prava strana
%          x0 - pocatecni aproximace 

function x = mja(A, b,x0);

clear x_k;
clear x_k1;
clear U;
clear D;
clear L;
clear cas;
clear H;
clear g;

iterace = 0;
presnost = 1*10^(-6);
dim = size(A);
norma = 1;

tic;

D = diag(diag(A));
U = triu(A) - D;
L = tril(A) - D;

x_k = x0;                   % pocatecni aproximace je prava strana

H = -inv(D)*(U+L);
g = inv(D) * b;

% Vlastni vypocet
while ((norma >= presnost) & (iterace<10))
   iterace = iterace +1;
   x_k1 = H * x_k + g;
   norma = 0;
   for i = 1 : dim
     norma = norma + (x_k1(i) - x_k(i))^2;
   end;
   norma = sqrt(norma);

   x_k = x_k1
end;

cas = toc;

x = x_k1;
disp('Reseni vypoctene programem MJA: ');
disp(x);
disp('Doba vypoctu:                   ');
disp(cas);
disp('Pocet iteraci:                  ');
disp(iterace);

%KONEC
