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 = 12;
  angle = linspace ( pi/2.0, -3*pi/2.0, n + 1 ); 
  angle = angle(1:n);
  r = 1.0;
  x = [ r * cos(angle); r * sin(angle) ];
%
%  Locate the text labels at a slightly larger radius.
%
  r3 = 1.2;
  x3 = [ r3 * cos(angle); r3 * 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 = 1.2 * min ( min ( min ( ax ) ), min ( min ( x ) ) );
  p_max = 1.2 * 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' );
%
%  Plot the circle.
%
  n2 = 101;
  angle2 = linspace ( pi/2.0, -3*pi/2.0, n2 ); 
  angle2 = angle2(1:n2);
  r2 = 1.0;
  x2 = [ r2 * cos(angle2); r2 * sin(angle2) ];
  plot ( x2(1,:), x2(2,:), 'k-', 'linewidth', 1 );
%
%  Label the points X on the unit circle.
%
  i = 97;
  for j = 1 : n
    text ( x3(1,j), x3(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', 'r', '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, right singular vectors V' );
  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 outline of the ellipse.
%
  ax2 = A * x2;
  plot ( ax2(1,:), ax2(2,:), 'k-', 'linewidth', 1 );
%
%  Plot the images AX of points X on the unit circle.
%
  i = 65;
  for j = 1 : n
    text ( 1.2*ax(1,j), 1.2*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', 'b', 'linewidth', 3 );

  xlabel ( '--X axis--' );
  ylabel ( '--Y axis--' );
  title ( 'A*x and left singular vectors U' );
  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
 
