function svd_sphere ( A )

%*****************************************************************************80
%
%% svd_sphere() plots the images of sphere points under the 3x3 matrix A.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    16 December 2018
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real A(3,3), the matrix whose mapping of the unit sphere is to be studied.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'svd_sphere():\n' );
  fprintf ( 1, '  Given a matrix A,\n' );
  fprintf ( 1, '  plot points x on the unit sphere, 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, %f and %f\n', svec(1), svec(2), svec(3) );
  fprintf ( 1, '  Right singular vectors are columns of V:\n' );
  disp ( v )
  fprintf ( 1, '  Left singular vectors are columns of U:\n' );
  disp ( u )
%
%  Select points on the surface of the unit sphere.
%
  n = 500;
  x = sphere_sample ( n );
%
%  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' );
  scatter3 ( x(1,:), x(2,:), x(3,:) );
%
%  Plot V1 and V2.
%
  plot3 ( [ 0, v(1,1)], [ 0, v(2,1)], [ 0, v(3,1)], 'color', 'b', 'linewidth', 3 );
  plot3 ( [ 0, v(1,2)], [ 0, v(2,2)], [ 0, v(3,2)], 'color', 'b', 'linewidth', 3 );
  plot3 ( [ 0, v(1,3)], [ 0, v(2,3)], [ 0, v(3,3)], 'color', 'b', 'linewidth', 3 );

  xlabel ( '--X axis--' );
  ylabel ( '--Y axis--' );
  zlabel ( '--Z axis--' );
  title ( 'Sphere points, Right singular vectors', 'Fontsize', 16 );
  axis ( 'equal' );
  axis ( 'tight' );
  axis ( 'square' );
  grid ( 'on' );
  hold ( 'off' );
  filename = 'svd_sphere_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' );
  scatter3 ( ax(1,:), ax(2,:), ax(3,:) );
%
%  Plot the images of V1 and V2.
%
  plot3 ( ...
    [ 0, svec(1)*u(1,1)], ...
    [ 0, svec(1)*u(2,1)], ...
    [ 0, svec(1)*u(3,1)], 'color', 'r', 'linewidth', 3 );
  plot3 ( ...
    [ 0, svec(2)*u(1,2)], ...
    [ 0, svec(2)*u(2,2)], ...
    [ 0, svec(2)*u(3,2)], 'color', 'r', 'linewidth', 3 );
  plot3 ( ...
    [ 0, svec(3)*u(1,3)], ...
    [ 0, svec(3)*u(2,3)], ...
    [ 0, svec(3)*u(3,3)], 'color', 'r', 'linewidth', 3 );

  xlabel ( '--X axis--' );
  ylabel ( '--Y axis--' );
  xlabel ( '--Z axis--' );
  title ( 'Sphere point images, scaled left singular vectors', 'Fontsize', 16 );
  axis ( 'equal' );
  axis ( 'tight' );
  axis ( 'square' );
  grid ( 'on' );
  hold ( 'off' );
  filename = 'svd_sphere_2.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'svd_sphere():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end
