function brownian ( n )

%*****************************************************************************80
%
%% BROWNIAN models a Brownian walk in two dimensions.
%
%  Discussion:
%
%    BROWNIAN 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.
%
%  Modified:
%
%    27 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
%  Input parameters:
%
%    Input, integer N, the walk duration, in steps.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'BROWNIAN\n' );
  fprintf ( 1, '  BROWNIAN simulates 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(1) = 0;
  ypos(1) = 0;
  for i = 2 : n
    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 )
  grid on
  xlabel ( 'x direction' )
  ylabel ( 'y direction' )
  title ( 'BROWNIAN - Two-Dimensional Brownian Motion' )
  print ( '-dpng', 'brownian.png' )

  return
end
