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 GNU LGPL license.
#
#  Modified:
#
#    08 February 2023
#
#  Author:
#
#    Original MATLAB version by Paul Nahin.
#    This version by John Burkardt.
#
#  Reference:
#
#    Paul Nahin,
#    Dueling Idiots and Other Probability Puzzlers,
#    Princeton, 2012,
#    ISBN: 978-0691155005.
#
  print ( '' )
  print ( 'stirling1():' )
  print ( '  Compare the logarithms of n! and Stirling\'s approximation' )
  print ( '  Present the results graphically.' )

  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 )
  grid ( 'on' )
  xlabel ( 'n' )
  ylabel ( 'Absolute Error' )
  title ( 'ln(n!) - [nln(n)-n]' )

  subplot ( 1, 2, 2 )
  semilogx ( f ./ s )
  grid ( 'on' )
  xlabel ( 'n' )
  ylabel ( 'Relative Error' )
  title ( 'ln(n!)/[nln(n)-n]' )
  filename = 'stirling1.png'
  plt.savefig ( filename )
  print ( '  Graphics saved as "' + filename + '"' )
  plt.close ( )

  return
end
