function underdog2 ( )

%*****************************************************************************80
%
%% underdog2() estimates the duration of a World Series.
%
%  Discussion:
%
%    The World Series of Baseball involves up to 7 games between two teams.
%    Suppose the stronger team has probability P of winning any one game.
%    What is the average duration of the World Series?
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    28 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'underdog2():\n' );
  fprintf ( 1, '  The World Series of Baseball involves up to 7 games between two teams.\n' );
  fprintf ( 1, '  Suppose the stronger team has probability P of winning any one game.\n' );
  fprintf ( 1, '  What is the average duration of the World Series?\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Report the results graphically.\n' );

  duration = zeros ( 1, 100 );

  for i = 1 : 100

    p = i / 100;
    common1 = ( 1 - p ) ^ 4;
    common2 = p ^ 4;

    duration(i) = ...
        4 *  1 * ( common1         + common2 ) ...
      + 5 *  4 * ( common1 * p     + common2 * ( 1 - p ) ) ...
      + 6 * 10 * ( common1 * p ^ 2 + common2 * ( 1 - p ) ^ 2 ) ...
      + 7 * 20 * ( common1 * p ^ 3 + common2 * ( 1 - p ) ^ 3 );

  end
%
%  Graphics.
%
  clf ( );
  p = 0.01 : 0.01 : 1.0;
  plot ( p, duration, 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( 'Probability, p, of a team winning an individual game' );
  ylabel ( 'Duration, in games' );
  title ( 'Expected Duration of the World Series' );
  filename = 'underdog2.png';
  print ( '-dpng', filename );
  fprintf ( '  Graphics saved as "%s"\n', filename );

  return
end
