%GS a J pro matici (3x3)
%vstupy: a, b, X0
A=[1 -0.5 -0.5; -0.5 2 1.5; 0.5 1.5 2]   %zadana matice (3x3)
b=[-1.25; -7; 0.75]               %zadana prava strana
X0= [1; 1.5; -7];              %zadané X0

%Iterační matice a spektralni polomer
LD=tril(A); U = triu(A) - diag(diag(A));
UG = -inv(LD)*U;  %vG = inv(LD)*b;
spektralni_polomer_G= max(abs(eig(UG))); %zaokrouhlovací chyby!
UJ =[0 -A(1,2)/A(1,1) -A(1,3)/A(1,1)
       -A(2,1)/A(2,2) 0  -A(2,3)/A(2,2)
       -A(3,1)/A(3,3) -A(3,2)/A(3,3) 0];
spektralni_polomer_J= max(abs(eig(UJ)));
fprintf("Spektralni polomer UG = %5.4f\n",spektralni_polomer_G)
fprintf("Spektralni polomer UJ = %5.4f\n",spektralni_polomer_J)


%Vypocet pro soustavu (3x3)
%Gauss - Seidel

X1_GS(1) = (b(1) - A(1,2) * X0(2)    - A(1,3) * X0(3)) /A(1,1);
X1_GS(2) = (b(2) - A(2,1) * X1_GS(1) - A(2,3) * X0(3)) /A(2,2);
X1_GS(3) = (b(3) - A(3,1) * X1_GS(1) - A(3,2) * X1_GS(2))/A(3,3);

X2_GS(1) = (b(1) - A(1,2) * X1_GS(2) - A(1,3) * X1_GS(3)) /A(1,1);
X2_GS(2) = (b(2) - A(2,1) * X2_GS(1) - A(2,3) * X1_GS(3)) /A(2,2);
X2_GS(3) = (b(3) - A(3,1) * X2_GS(1) - A(3,2) * X2_GS(2)) /A(3,3);
fprintf("\n%s\n","Gauss-Seidel: 1. iterace");
fprintf("%f   ",X1_GS)
fprintf("\n%s\n","Gauss-Seidel: 2. iterace");
fprintf("%f   ",X2_GS)

%Jacobi
X1_J(1) = (b(1) - A(1,2) * X0(2) - A(1,3) * X0(3)) / A(1,1);
X1_J(2) = (b(2) - A(2,1) * X0(1) - A(2,3) * X0(3)) / A(2,2);
X1_J(3) = (b(3) - A(3,1) * X0(1) - A(3,2) * X0(2)) / A(3,3);

X2_J(1) = (b(1) - A(1,2) * X1_J(2) - A(1,3) * X1_J(3)) / A(1,1);
X2_J(2) = (b(2) - A(2,1) * X1_J(1) - A(2,3) * X1_J(3)) / A(2,2);
X2_J(3) = (b(3) - A(3,1) * X1_J(1) - A(3,2) * X1_J(2)) / A(3,3);

fprintf("\n\n%s\n","Jacobi: 1. iterace");
fprintf("%f   ",X1_J)
fprintf("\n%s\n","Jacobi: 2. iterace");
fprintf("%f   ",X2_J)
fprintf("\n")
