function fish_sample2 ( seed )

%*****************************************************************************80
%
%% FISH_SAMPLE2 estimates the mean and variance of a fish population.
%
%  Discussion:
%
%    Suppose a river extends over the range L <= X <= U, and is uniformly
%    populated by an inexhaustible supply of fish.
%
%    Suppose further that the weight W of a fish is determined by its 
%    location X, and in fact, is some linear function
%
%      W = A * X + B
%   
%    We wish to estimate the average weight of the fish.
%
%    We can do this by sampling uniformly over the spatial range X.
%    We can do this by evaluating W at any two distinct points.
%    We can even do this by evaluating W at a single, carefully selected point.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    13 February 2013
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer SEED, a seed for the random number generator.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'FISH_SAMPLE2\n' );
  fprintf ( 1, '  The weight of fish along a river is a function of position X,\n' );
  fprintf ( 1, '    W = A * X + B\n' );
  fprintf ( 1, '  The values of A and B are unknown, but we can measure X and W.\n' );
  fprintf ( 1, '  Determine the average value of W for the fish in the river\n' );
  fprintf ( 1, '  assuming it extends from 0 <= X <= 10.\n' );
%
%  The river extends from L=0 to U=10.
%
  l = 0.0;
  u = 10.0;
%
%  Initialize.
%
  rng ( seed );
%
%  Randomly choose A from [1,3] and B from [10,25].
%
  a = 1.0 + 2.0 * rand ( 1, 1 );
  b = 10.0 + 15.0 * rand ( 1, 1 );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Weight formula is W = %g * x + %g\n', a, b );
%
%  Compute average W:
%
  x_average = ( l + u ) / 2.0;
  w_average = a * x_average + b;
  fprintf ( 1, '  Average weight is exactly %g\n', w_average );
%
%  Use Monte Carlo sampling to estimate A and B.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, '       Samples      Estimate        Error\n' );
  fprintf ( 1, '\n' );
  sample = 1;
  for sample_log = 0 : 10
    x = l + u * rand ( sample, 1 );
    w = a * x + b;
    w_average1 = sum ( w ) / sample;
    e = abs ( w_average1 - w_average );
    fprintf ( 1, '  %12d  %12g  %12g\n', sample, w_average1, e );
    sample = sample * 2;
  end
%
%  A linear function can be averaged over an interval 
%  by evaluating it at two places, and then figuring out its
%  value at the midpoint.
%
  x1 = l + u * rand ( 1, 1 );
  w1 = a + b * x1;
  x2 = l + u * rand ( 1, 1 );
  w2 = a + b * x2;

  b_computed = ( w2 - w1 ) / ( x2 - x1 );
  a_computed = w2 - b_computed * x2;
  x_average = ( l + u ) / 2.0;
  w_average2 = a_computed * x_average + b_computed;
  e = abs ( w_average2 - w_average );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Two point     %12g  %12g\n', w_average2, e );
%
%  A linear function can be averaged by evaluating it at the midpoint.
%
  w_average3 = a * x_average + b;
  e = abs ( w_average2 - w_average );
  fprintf ( 1, '  Midpoint      %12g  %12g\n', w_average3, e );

  return
end

