% 
% test SOR method
%clear all;
A = [10 -2 3  0;
     -1 11 -1 3;
      2 -1 10 -1;
      0  3 -1 8];
b = [6;25;-11;15];
xosv = [0;0;0;0];
sln= A \b;
D = diag(A);
L = -tril(A,-1);
U = -triu(A,1);
c = (diag(D)-L)\b;
omega = [1.059, 1.25, 1.5];
for t = 1:length(omega)
    ot = omega(t);
    xo = xosv;
    err = [];
    dif = [];
    c  = ot *((diag(D)-ot*L)\b);
    for k = 1:50
        x = c + (diag(D)-ot*L)\((1-ot)*(D.*xo)+ot*(U*xo));   
        err(k) = norm(x-sln);
        dif(k) = norm(x-xo);
        xo  = x;
    end
    figure(3+t);
    clf;
    semilogy(err,'b.-');
    hold on;
    semilogy(dif,'r.-');
    title(['SOR Iteration Errors, omega = ',num2str(ot)],'FontSize',18);
    legend('Error in Solution','Solution Change in Each Iteration');
    pause;
end
