function [x r2 e]=Grmsch_3(A,b)
% Resuelve Ax=b mediante el método de Gram-Schmidt modificado
%
 [m,n]=size(A); x=zeros(n,1); e=zeros(m,n); u=triu(zeros(n,n));
 for j=1:n
    e(:,j)=A(:,j);
    for i=1:j-1
       u(i,j)=e(:,i)'*e(:,j);
       e(:,j)=e(:,j)-u(i,j)*e(:,i);
    end
    u(j,j)=norm(e(:,j));
    e(:,j)=e(:,j)/u(j,j);
 end
 for i=n:-1:1                                      % Rx=b
    x(i)=(e(:,i)'*b-u(i,i+1:n)*x(i+1:n))/u(i,i);
 end
 r2=norm(abs(A*x-b),2)^2;                          % Residuos
end
