function count = GS(omega) % This performs Over-Relaxed Gauss-Seidel on the matrix given below % Input: omega = relaxation factor % Output: number of iterations required for convergence A = [ -4 1 1 1; 1 -4 1 1; 1 1 -4 1; 1 1 1 -4]; b = [1 1 1 1 ]'; % actual solution is [-1 -1 -1 -1]' x = [0 0 0 0]'; tol = 10^(-5); [n,m] = size(A); Res = b - A*x; sizeRes = norm(Res,2); count = 0; maxits = 300; while sizeRes > tol for i=1:n Res(i) = b(i) - A(i,:) * x; x(i) = x(i) + omega * Res(i)/A(i,i); end sizeRes = norm(Res,2); count = count+1; if count > maxits disp('Did not converge!!!!!') sizeRes = 0; end end