function stirling1 ( )

%*****************************************************************************80
%
%% stirling1() compares the logarithms of n! and Stirling's approximation.
%
%  Discussion:
%
%    Compare the logarithms of n! and Stirling's approximation.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    30 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'stirling1():\n' );
  fprintf ( 1, '  Compare the logarithms of n! and Stirling''s approximation\n' );
  fprintf ( 1, '  Present the results graphically.\n' );

  for n = 1 : 170
    f(n) = log ( gamma ( n + 1 ) );
    s(n) = n * log ( n ) - n;
  end
%
%  Graphics.
%
  clf ( );

  subplot ( 1, 2, 1 );
  plot ( f - s, 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( 'n' );
  ylabel ( 'Absolute Error' );
  title ( 'ln(n!) - [nln(n)-n]' );

  subplot ( 1, 2, 2 );
  semilogx ( f ./ s, 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( 'n' );
  ylabel ( 'Relative Error' );
  title ( 'ln(n!)/[nln(n)-n]' );

  filename = 'stirling1.png';
  print ( '-dpng', filename );
  fprintf ( '  Graphics saved as "%s"\n', filename );

  return
end
