function gas ( trial_num )

%*****************************************************************************80
%
%% gas() simulates the diffusion of a gas.
%
%  Discussion:
%
%    This code simulates the diffusion of gas molecules in a sealed
%    container by using the Ehrenfest ball exchange rules. The simulation
%    starts with n molecules (i.e., balls) of one type (i.e., black) on
%    one side of the container, and n more molecules of another type (i.e.,
%    white balls) on the other side. The two urns play the roles of the
%    two sides of the container. To simulate the ball (molecule)
%    movements, the program selects two random numbers from 0 to 1, which
%    are then compared to the current probabilities of selecting a black
%    ball from urn I and a white ball from urn II. If BOTH random numbers
%    are greater than these two probabilities then a white ball has been
%    selected from urn I and a black ball has been selected from urn II,
%    and so the number black balls in urn I is increased by one while the
%    number of white balls in urn II is increased by one. If BOTH random
%    numbers are less than or equal to these two probabilities then a
%    black ball has been selected from urn I and a white ball has been
%    selected from urn II and so the number of black balls in urn I is
%    decreased by one while the number of white balls in urn II is decreased
%    by one. If one of the random numbers is greater than its corresponding
%    probability while the other random number is less than its
%    corresponding probability, then no action is taken because then a
%    white (black) ball moves from urn I to urn II at the same time a white
%    (black) ball moves in the opposite direction. That is, there is no
%    net change. Then, the ball selection probabilities are recalculated and
%    another ball exchange is simulated.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    25 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
%  Input:
%
%    integer trial_num: the number of trials.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'gas():\n' );
  fprintf ( 1, '  Simulate the diffusion of gas molecules in a sealed\n' );
  fprintf ( 1, '  container by using the Ehrenfest ball exchange rules. The simulation\n' );
  fprintf ( 1, '  starts with n molecules (i.e., balls) of one type (i.e., black) on\n' );
  fprintf ( 1, '  one side of the container, and n more molecules of another type (i.e.,\n' );
  fprintf ( 1, '  white balls) on the other side. The two urns play the roles of the\n' );
  fprintf ( 1, '  two sides of the container. To simulate the ball (molecule)\n' );
  fprintf ( 1, '  movements, the program selects two random numbers from 0 to 1, which\n' );
  fprintf ( 1, '  are then compared to the current probabilities of selecting a black\n' );
  fprintf ( 1, '  ball from urn I and a white ball from urn II. If BOTH random numbers\n' );
  fprintf ( 1, '  are greater than these two probabilities then a white ball has been\n' );
  fprintf ( 1, '  selected from urn I and a black ball has been selected from urn II,\n' );
  fprintf ( 1, '  and so the number black balls in urn I is increased by one while the\n' );
  fprintf ( 1, '  number of white balls in urn II is increased by one. If BOTH random\n' );
  fprintf ( 1, '  numbers are less than or equal to these two probabilities then a\n' );
  fprintf ( 1, '  black ball has been selected from urn I and a white ball has been\n' );
  fprintf ( 1, '  selected from urn II and so the number of black balls in urn I is\n' );
  fprintf ( 1, '  decreased by one while the number of white balls in urn II is decreased\n' );
  fprintf ( 1, '  by one. If one of the random numbers is greater than its corresponding\n' );
  fprintf ( 1, '  probability while the other random number is less than its\n' );
  fprintf ( 1, '  corresponding probability, then no action is taken because then a\n' );
  fprintf ( 1, '  white (black) ball moves from urn I to urn II at the same time a white\n' );
  fprintf ( 1, '  (black) ball moves in the opposite direction. That is, there is no\n' );
  fprintf ( 1, '  net change. Then, the ball selection probabilities are recalculated and\n' );
  fprintf ( 1, '  another ball exchange is simulated.\n' );

  n = 50;
  nb1 = n;
  nw2 = n;
  pb1 = nb1 / n;
  pw2 = nw2 / n;

  for trials = 1 : trial_num;

    system(trials) = pb1;
    ball1 = rand ( );
    ball2 = rand ( );

    if ( ball1 > pb1 && ball2 > pw2 )
      nb1 = nb1 + 1;
      nw2 = nw2 + 1;
    elseif ( ball1 <= pb1 && ball2 <= pw2 )
      nb1 = nb1 - 1;
      nw2 = nw2 - 1;
    end
    pb1 = nb1 / n;
    pw2 = nw2 / n;
  end
%
%  Graphics.
%
  clf ( );
  plot ( system, 'linewidth', 3 );
  axis ( [ 1 trials 0 1 ] );
  grid ( 'on' );
  xlabel ( 'time, in arbitrary units' );
  ylabel ( 'fraction of balls in urn I that are black' );
  title ( 'Simulation of the Ehrenfest Ball Exchange Process' );
  filename = 'gas.png';
  print ( '-dpng', filename );
  fprintf ( '  Graphics saved as "%s"\n', filename );

  return
end

