function theory ( )

%*****************************************************************************80
%
%% THEORY plots the theoretical distribution for the PATHS problem.
%
%  Discussion:
%
%    A line is drawn through the unit square by choosing a point x 
%    in [0,1] and then choosing a random angle between 0 and 180 degrees.
%    This m-file plots the theoretical distribution of the length of
%    the line between x and the intersection with the square's boundary.
%
%  Modified:
%
%    29 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'THEORY:\n' );
  fprintf ( 1, '  A line is drawn through the unit square by choosing a point x \n' );
  fprintf ( 1, '  in [0,1] and then choosing a random angle between 0 and 180 degrees.\n' );
  fprintf ( 1, '  This m-file plots the theoretical distribution of the length of\n' );
  fprintf ( 1, '  the line between x and the intersection with the square''s boundary.\n' );

  factor = 2 / pi;
  for l = 1 : 1414
    lscale = l / 1000;
    if ( l < 1000 )
      F(l) = factor * lscale;
    else
      F(l) = factor * ( 1 + 2 * acos ( 1 / lscale ) - sqrt ( lscale ^ 2 - 1 ) );
    end
  end
%
%  Graphics.
%
  clf
  x = .001 : .001 : 1.414;
  plot(x,F)
  grid
  xlabel('length')
  ylabel('probability')
  title('Fig.17.7 - Theoretical Distribution Function')
  print ( '-dpng', 'theory.png' );

  return
end

   
