function exercise2 ( )

%*****************************************************************************80
%
%% exercise2 compares divergences of direct and indirect velocity fields.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    17 June 2020
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'exercise2:\n' );
  fprintf ( 1, '  Generate velocity field from uv_square.\n' );
  fprintf ( 1, '  Check that divergence is zero or "goes to" zero.\n' );
  fprintf ( 1, '  D1 is divergence of (U,V) from formula.\n' );
  fprintf ( 1, '  D2 is divergence of (U,V) from stream function.\n' );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  nr  nc  ||D1||  ||D2||\n' );
  fprintf ( 1, '\n' );
  for nr = [ 11, 21, 41, 81 ]
    nc = nr;
    [ d1norm, d2norm ] = divergence_square ( nr, nc );
    fprintf ( 1, '  %2d  %2d  %g  %g\n', nr, nc, d1norm, d2norm );
  end

  return
end
function [ d1norm, d2norm ] = divergence_square ( nr, nc )

%*****************************************************************************80
%
%% divergence_square checks the divergence of the square region velocities.
%
%  Modified:
%
%    17 June 2020
%
%  Input:
%
%    integer nr, nc: the number of rows and columns in the grid.
%
%  Output:
%
%    real d1norm: the RMS norm of the divergence of the velocity field
%    as defined by uv_square().
%
%    real d2norm: the RMS norm of the divergence of the velocity field
%    as defined by psi_square() followed by uv_from_psi().
%

%
%  Set up geometry.
%
  [ X, Y ] = xy_square ( nr, nc );
%
%  Get velocity field directly.
%
  [ U1, V1 ] = uv_square ( X, Y );
  D1 = divergence ( X, Y, U1, V1 );
  d1norm = rms_norm ( D1 );
%
%  Get velocity field indirectly, from stream function.
%
  PSI = psi_square ( X, Y );
  [ U2, V2 ] = uv_from_psi ( X, Y, PSI );
  D2 = divergence ( X, Y, U2, V2 );
  d2norm = rms_norm ( D2 );

  return
end

