function [x,iter] = GSLR(A,b,L,es,maxit)
%   Linear Gauss Seidel method  with relaxation L. 
%   (L=1 corresponds to no relaxation)
%   x=solution to A*x=b
%   A = coefficient matrix
%   If A is diagonally dominant the method will converge.
%   b =  vector
%   es = stop criterion (default = 0.00001%)
%   maxit = max iterations (default = 50)
% output:
%   x = solution vector
if nargin<2,error('at least 2 input arguments required'),end
if nargin<5|isempty(maxit),maxit=50;end
if nargin<4|isempty(es),es=0.00001;end
if nargin<3|isempty(L),L=1;end
iter=0;

x=zeros(length(A),1);
for i=1:length(x)
b(i)=b(i)/A(i,i);
A(i,:) = -A(i,:)/A(i,i);
A(i,i)=0;
end

while(1)

xold=x;

for i=1:length(A)
x(i)=A(i,:)*x+b(i);
x(i)=L*x(i)+(1-L)*xold(i);
if x(i) ~= 0
ea(i) = abs((x(i) - xold(i))/x(i)) * 100;
end

end

  iter = iter+1;
  if max(ea)<=es | iter >= maxit, break, end
end