function quartic_gradient1 ( )

%*****************************************************************************80
%
%% quartic_gradient1 applies gradient descent to the quartic function.
%
%  Discussion:
%
%    The quartic function is
%    f(x) = 2.0 * x ** 4 - 4.0 * x ** 2 + x + 20.0
%
%    We seek a minimizer of f(x) in the range -2 <= x <= 2.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    02 October 2019
%
%  Author:
%
%    John Burkardt
%
  a = -2.0;
  b = +2.0;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'quartic_gradient1:\n' );
  fprintf ( 1, '  Seek local minimizer of a scalar function f(x) in [%g,%g]\n', a, b );
%
%  Plot the function.
%
  figure ( )
  xvec = linspace ( -2.0, 2.0, 101 );
  fxvec = quartic ( xvec );
  hold ( 'on' );
  plot ( xvec, fxvec, 'linewidth', 3 );
  plot ( [ a, b ], [ 0, 0 ], 'k', 'linewidth', 3 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- X -->' );
  ylabel ( '<-- F(X) -->' );
  title ( 'Quartic function', 'fontsize', 16 );
  filename = 'quartic.png';
  print ( '-dpng', filename );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Graphics saved in "%s"\n', filename );
%
%  Choose a random initial guess, or choose x = -1.5.
%
  x0 = a + ( b - a ) * rand ( );
  x0 = - 1.5;
%
%  Choose a small learning rate.
%
  r = 0.05;
%
%  Choose a small tolerance for the stepsize.
%
  dxtol = 0.001;
%
%  Choose a small tolerance for the derivative.
%
  dftol = 0.001;
%
%  Maximum number of iterations.
%
  itmax = 100;

  [ x, it ] = gradient_descent1 ( @(x)quartic(x), @(x)quartic_df(x), x0, r, dxtol, dftol, itmax );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  %d gradient descent steps were taken.\n', it );
  fprintf ( 1, '  Initial x = %g,  f(x) = %g, f''(x) = %g\n', ...
    x0, quartic ( x0 ), quartic_df(x0) );
  fprintf ( 1, '  Final x = %g,  f(x) = %g, f''(x) = %g\n', ...
    x, quartic ( x ), quartic_df(x) );
%
%  Plot the function with initial point and local minimizer
%
  figure ( )
  xvec = linspace ( a, b, 101 );
  fxvec = quartic ( xvec );
  hold ( 'on' )
  plot ( xvec, fxvec, 'linewidth', 3 );
  plot ( [ x0, x0 ], [ 0, quartic(x0) ], 'b', 'linewidth', 3 );
  plot ( x0,  0.0, 'bo', 'markersize', 10 );
  plot ( x0,  quartic ( x0 ), 'bo', 'markersize', 10 );
  plot ( [ x, x ], [ 0, quartic(x) ], 'r', 'linewidth', 3 );
  plot ( x,  0.0, 'ro', 'markersize', 10 );
  plot ( x,  quartic ( x ), 'ro', 'markersize', 10 );
  plot ( [ a, b ], [ 0, 0 ], 'k', 'linewidth', 3 );
  hold ( 'off' )
  grid ( 'on' );
  xlabel ( '<-- X -->' );
  ylabel ( '<-- F(X) -->' );
  title ( 'Initial point in blue, local minimizer in red', 'fontsize', 16 );
  filename = 'quartic_minimizer.png';
  print ( '-dpng', filename );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Graphics saved in "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'quartic_gradient1:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end

