function gradient_descent_vector_x_test ( )

%*****************************************************************************80
%
%% gradient_descent_vector_x_test() uses gradient descent on a vector problem.
%
%  Discussion:
%
%    We apply gradient descent to minimize the L2 norm of a scalar function 
%    f(x) of a vector argument x.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    28 July 2022
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'gradient_descent_vector_x_test():\n' );
  fprintf ( 1, '  Seek minimizer of a function z(x,y).\n' );
%
%  Choose an initial guess.
% 
  x0 = [ 1.0, 1.5 ];
%
%  Choose a small learning rate.
%
  alpha = 0.15;
%
%  Maximum number of iterations
%
  it_max = 10000;

  x = gradient_descent_vector_x ( ...
    @(x)hex2(x), @(x)hex2_df(x), x0, it_max, alpha );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Initial x,y = (%g,%g),  f(x,y) = %g, f''(x,y) = (%g,%g)\n', ...
    x0, hex2 ( x0 ), hex2_df ( x0 ) )
  fprintf ( 1, '  Final   x,y = (%g,%g),  f(x,y) = %g, f''(x,y) = (%g,%g)\n', ...
    x, hex2 ( x ), hex2_df ( x ) )

  return
end

function value = hex2 ( xy )

%*****************************************************************************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.
%
  x = xy(1);
  y = xy(2);
  value = 2.0 * x.^2 - 1.05 * x.^4 + x.^6 / 6.0 + x .* y + y.^2;

  return
end
function value = hex2_df ( xy )

%*****************************************************************************80
%
%% hex2_df() evaluates the hex2 gradient.
%
%  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(2): the gradient vector.
%
  x = xy(1);
  y = xy(2);

  value(1) = 4.0 * x - 4.2 * x.^3 + x.^5 + y;
  value(2) = x + 2.0 * y;

  return
end


