function svd_circle ( a )

%*****************************************************************************80
%
%% svd_circle() plots the image of the unit circle under the 2x2 matrix A.
%
%  Discussion:
%
%    A typical 2x2 matrix will map the unit circle to some kind of tilted 
%    ellipse.  The aspect ratio of the ellipse (ratio of major to minor axes) 
%    is a measure of the conditioning of the matrix.
%
%    A singular matrix maps the unit circle to a line.
%
%    A diagonal matrix maps the unit circle to an ellipse with no tilting.
%    (There is no rotation.)
%
%    An orthogonal matrix maps the unit circle to the unit circle (but points
%    may have rotated.)
%
%    The identity matrix maps the unit circle to itself.
%
%    The singular value decomposition can predict the shape of the mapping:
%
%    * The vector V(1,1:2) maps to S(1)*U(1:2,1),
%    * The vector V(2,1:2) maps to S(2)*U(1:2,2).
%
%    The ellipse has an aspect ratio of S(1)/S(2).
%
%    The "tilt" or slope of the ellipse is U(2,1)/U(1,1).
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    16 December 2018
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real A(2,2), the matrix whose mapping of the unit circle 
%    is to be studied.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'SVD_CIRCLE\n' );
  fprintf ( 1, '  Given a matrix A,\n' );
  fprintf ( 1, '  plot points x on the unit circle, and\n' );
  fprintf ( 1, '  the images A*x of those points.\n' );
  fprintf ( 1, '  Show right singular vectors V\n' );
  fprintf ( 1, '  and their images, the scaled left singular vectors U.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Matrix A:\n' );
  fprintf ( 1, '\n' );
  disp ( a );
%
%  This call produces matrices U, S and V.
%
  [ u, s, v ] = svd ( a );
%
%  This call produces just the singular values, and in a vector, not a matrix.
%
  svec = svd ( a );

  fprintf ( 1, '  Singular values of A are %f and %f\n', svec(1), svec(2) );
  fprintf ( 1, '  Right singular vectors are columns of V:\n' );
  disp ( v )
  fprintf ( 1, '  Left singular vectors are columns of U:\n' );
  disp ( u )
  fprintf ( 1, '  Aspect ratio of ellipse will be %g\n', svec(1) / svec(2) );
  fprintf ( 1, '  Slope of ellipse will be %g\n', u(2,1) / u(1,1) );
%
%  Select N evenly spaced points X on the unit circle.
%
  n = 20;

  i = [ 0 : n - 1 ];

  angle = 2 * pi * i / n;

  r = 1;

  x = [ r * cos(angle); r * sin(angle) ];
%
%  AX contains the image of each point under the mapping X -> A*X.
%
  ax =  a * x;
%
%  Determine a common plot range.
%
  p_min = min ( min ( min ( ax ) ), min ( min ( x ) ) );
  p_max = max ( max ( max ( ax ) ), max ( max ( x ) ) );
%
%  Plot the corners of the region.  This is a trick to force MATLAB
%  to plot in a square with equal axes.  
%
  figure ( 1 );
  clf ( );
  hold ( 'on' );
  area = [];
  scatter ( [ p_min, p_max, p_max, p_min ], ...
            [ p_min, p_min, p_max, p_max ], area, 'k' );
%
%  Label the points X on the unit circle.
%
  i = 97;
  for j = 1 : n
    text ( x(1,j), x(2,j), char(i) );
    i = i + 1;
    if ( 122 < i )
      i = 97;
    end
  end
%
%  Plot V1 and V2.
%
  line ( [ 0, v(1,1)], [ 0, v(2,1)], 'color', 'b', 'linewidth', 3 );
  line ( [ 0, v(1,2)], [ 0, v(2,2)], 'color', 'b', 'linewidth', 3 );

  xlabel ( '--X axis--' );
  ylabel ( '--Y axis--' );
  title ( 'Points X on unit circle, and right singular vectors', ...
    'Fontsize', 16 );
  axis ( [ p_min, p_max, p_min, p_max ] );
  axis ( 'equal' );
  axis ( 'tight' );
  axis ( 'square' );
  grid ( 'on' );
  hold ( 'off' );
  filename = 'svd_circle_1.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Label the points AX on the image of the unit circle.
%
  figure ( 2 );
  clf ( );
  hold ( 'on' );
  area = [];
  scatter ( [ p_min, p_max, p_max, p_min ], ...
            [ p_min, p_min, p_max, p_max ], area, 'k' );
%
%  Plot the images AX of points X on the unit circle.
%
  i = 65;
  for j = 1 : n
    text ( ax(1,j), ax(2,j), char(i) )
    i = i + 1;
    if ( 90 < i )
      i = 65;
    end
  end
%
%  Plot the images of V1 and V2.
%
  line ( [ 0, svec(1)*u(1,1)], [ 0, svec(1)*u(2,1)], 'color', 'r', 'linewidth', 3 );
  line ( [ 0, svec(2)*u(1,2)], [ 0, svec(2)*u(2,2)], 'color', 'r', 'linewidth', 3 );

  xlabel ( '--X axis--' )
  ylabel ( '--Y axis--' )
  title ( 'Images A*X of points on unit circle and scaled left singular vectors', 'Fontsize', 16 );
  axis ( [ p_min, p_max, p_min, p_max ] );
  axis equal
  axis tight
  axis square
  grid ( 'on' )
  hold off
  filename = 'svd_circle_2.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'SVD_CIRCLE:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end
 
