function gradient_vector ( )

%*****************************************************************************80
%
%% gradient_vector makes a vector plot of the gradient of a function f(x,y).
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    09 October 2019
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'gradient_vector:\n' );
  fprintf ( 1, '  MATLAB/Octave version %s:\n', version ( ) );
  fprintf ( 1, '  Plot the gradient vector field of a function f(x,y).\n' );
%
%  Create the mesh vectors.
%
  a = -2.0;
  b = +2.0;
  x = linspace ( a, b, 31 );
  y = linspace ( a, b, 31 );
%
%  Create the mesh matrices.
%
  [ X, Y ] = meshgrid ( x, y );
%
%  Evaluate the function at the mesh points.
%
  Z = hex2 ( X, Y );
%
%  Evaluate the gradient using differences.
%
  h = 0.01;
  [ dZdX, dZdY ] = gradient ( Z, h );
%
%  Get ready to plot.
%
  figure ( );
  clf ( );
  hold ( 'on' );
%
%  Plot the contours.
%
  contour_num = 25;
  contour ( X, Y, Z, contour_num );
%
%  Plot the gradient vectors.
%
  scale = 2.0;
  quiver ( X, Y, -dZdX, -dZdY, scale );
%
%  Finish the plot.
%
  hold ( 'off' );
  xlabel ( '<--X-->' );
  ylabel ( '<--Y-->' );
  title ( 'Contours and Negative Gradients' );
  filename = 'gradient_vector.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'gradient_vector:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end
function value = hex2 ( x, y )

%*****************************************************************************80
%
%% hex2 evaluates the hex2 function.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    02 October 2019
%
%  Input:
%
%    real xy(2): the x and y arguments.
%
%  Output:
%
%    real value: the function value.
%
  value = 2.0 * x.^2 - 1.05 * x.^4 + x.^6 / 6.0 + x .* y + y.^2;

  return
end

