function [x, iter]=mygs(a,xexact,x0,tol)

    b=a*xexact;
    
    n=length(x0);
    x=x0;
    
    iter = 0;
    error=1;
   
    while error > tol
        for i=1:n
            x(i) = a(i,1:i-1)*x(1:i-1)+a(i,i+1:n)*x(i+1:n);
            x(i)=(b(i)-x(i))/a(i,i);
        end
        error=norm(x-xexact)/norm(x0-xexact);
        iter = iter + 1;
     end
end