
hold off;

max_it=200; tol=10^(-8);

siz=5;
A = makefish(siz);     % MATRICE DI POISSON.
b=ones(size(A,1),1);   % TERMINE NOTO.

x_sol=A\b;             % SOLUZIONE ESATTA. METODO LU.

x=zeros(size(b));      % VALORE INIZIALE.

% JACOBI.
[x_j, error_j, iter_j, flag_j]  = jacobi(A, x, b, max_it, tol);

fprintf('\t \n [JACOBI  ] [STEP REL., NORMA 2]: %2.2e [REL.ERR.]: %2.2e',error_j,norm(x_j-x_sol)/norm_x_sol);
fprintf('\t \n            [ITER.]: %3.0f [FLAG]: %1.0f \n',iter_j,flag_j);


% GAUSS-SEIDEL.
w=1;
[x_gs, error_gs, iter_gs, flag_gs]  = sor(A, x, b, w, max_it, tol);

fprintf('\t \n [GAU.SEI.] [STEP REL., NORMA 2]: %2.2e [REL.ERR.]: %2.2e',error_gs,norm(x_gs-x_sol)/norm_x_sol);
fprintf('\t \n            [ITER.]: %3.0f [FLAG]: %1.0f \n',iter_gs,flag_gs);


% SOR.
w_vett=0.8:0.025:2;

for index=1:length(w_vett)
    w=w_vett(index);
   [x_sor, error_sor(index), iter_sor(index), flag_sor(index)]  = sor(A, x, b, w, max_it, tol);
   relerr(index)=norm(x_sor-x_sol)/norm_x_sol;
end

[min_iter_sor, min_index]=min(iter_sor);

fprintf('\t \n [SOR OTT.] [STEP REL., NORMA 2]: %2.2e [REL.ERR.]: %2.2e',error_sor(min_index),relerr(min_index));
fprintf('\t \n            [ITER.]: %3.0f [FLAG]: %1.0f [w]: %2.3f \n',min_iter_sor,flag_sor(min_index),w_vett(min_index));
 
plot(w_vett,iter_sor,'r-');

% GRADIENTE CONIUGATO.
M=eye(size(A));
[x_gc, error_gc, iter_gc, flag_gc] = cg(A, x, b, M, max_it, tol);

fprintf('\t \n [GRA.CON.] [STEP REL., NORMA 2]: %2.2e [REL.ERR.]: %2.2e',error_gc,norm(x_gc-x_sol)/norm_x_sol);
fprintf('\t \n            [ITER.]: %3.0f [FLAG]: %1.0f \n',iter_gc,flag_gc);



