% Modified Gram-Schmidt
function [u]=mgs(v)
   n = length(v);
   for j=1:n
       for i=1:j-1          
           v(:,j)=v(:,j)-(v(:,i)'*v(:,j))/(v(:,i)'*v(:,i))*v(:,i);
       end
       v(:,j)=v(:,j)/sqrt(v(:,j)'*v(:,j));
   end 
   u = v;
end