%problem P4.1 with sor
clear;format short e
a=[10 -2 1;-2 10 -2;-2 -5 10];
b=[9;12;18];
%solve with sor using different values of w, the relaxation factor
i=0;
for w=.2:.2:1.6
   i=i+1;
   [x1,c1]=sor2(a,b,w);
   x(:,i)=x1;
   c(i)=c1;
   r(i)=w;
%compare results
%calculate iteration matrix
l=tril(a,-1);d=diag(diag(a));u=triu(a,1);
%SOR
cgs=inv(d+w*l)*((1-w)*d-w*u);specgs(i)=max(abs(eig(cgs)));
end
disp('relaxation factor , no of iterations and spectral radius')
[r(1:i)' c(1:i)' specgs(1:i)']
disp(' solution and error')
[x(:,1:i) b-a*x(:,1) ]
