function brownian ( step_num )

%*****************************************************************************80
%
%% brownian() models a Brownian walk in two dimensions.
%
%  Discussion:
%
%    This code simulates Brownian motion, a random walk by a particle
%    suspended in a fluid and being hit by molecules.  At successive
%    increments of time, the particle performs independent motions of random
%    length, uniform from -1 to 1, in arbitrary units, in both the x and the y
%    directions.  The walk duration, that is, the total number of steps, 
%    where a step is a delta-x and a delta-y pair, is defined by the user. 
%    A walk always starts at the origin.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    27 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
%  Input:
%
%    integer step_num: the walk duration, in steps.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'brownian()\n' );
  fprintf ( 1, '  Simulate Brownian motion, a random walk by a particle\n' );
  fprintf ( 1, '  suspended in a fluid and being hit by molecules.  At successive\n' );
  fprintf ( 1, '  increments of time, the particle performs independent motions of random\n' );
  fprintf ( 1, '  length, uniform from -1 to 1, in arbitrary units, in both the x and the y\n' );
  fprintf ( 1, '  directions.  The walk duration, that is, the total number of steps, \n' );
  fprintf ( 1, '  where a step is a delta-x and a delta-y pair, is defined by the user. \n' );
  fprintf ( 1, '  A walk always starts at the origin.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Display results graphically.\n' );

  xpos = zeros ( step_num, 1 );
  ypos = zeros ( step_num, 1 );

  for i = 2 : step_num
    deltax = - 1 + 2 * rand ( );
    deltay = - 1 + 2 * rand ( );
    xpos(i) = xpos(i-1) + deltax;
    ypos(i) = ypos(i-1) + deltay;
  end
%
%  Graphics.
%
  clf ( );
  plot ( xpos, ypos, 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( 'x direction' );
  ylabel ( 'y direction' );
  title ( 'Two-Dimensional Brownian Motion' );
  filename = 'brownian.png';
  print ( '-dpng', filename );
  fprintf ( '  Graphics saved as "%s"\n', filename );

  return
end
