function U = mgs2 ( A )

%*****************************************************************************80
%
%% mgs2() applies the modified Gram-Schmidt algorithm with normalization.
%
%  Discussion:
%
%    The output of this function will be a set of orthonormal vectors,
%    such that U(:,i) dot U(:,j) = delta(i,j).
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    07 December 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real A(M,N), a matrix of N vectors.
%
%  Output:
%
%    real U(M,N), a matrix with orthonormal columns.
%
  [ m, n ] = size ( A );

  U = A;

  for j = 1 : n

    v = U(:,j);
    v = v / norm ( v );
    U(:,j) = v;

    for j2 = j + 1 : n
      v2 = U(:,j2);
      p = v' * v2;
      v2 = v2 - p * v;
      U(:,j2) = v2;
    end
 
  end

  return
end
