function llsq_test04 ( )

%*****************************************************************************80
%
%% llsq_test04() calls llsq0() to match Playfair wheat data.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    22 June 2019
%
%  Author:
%
%    John Burkardt
%
  data = load ( 'playfair_data.txt' );

  x_data = data(:,1);
  xmin = min ( x_data );
  xmax = max ( x_data );
  y_data = data(:,2) ./ data(:,3);
  n = length ( x_data );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'llsq_test04():\n' );
  fprintf ( 1, '  llsq0() can compute the formula for a line of the form\n' );
  fprintf ( 1, '    y = A * x + B\n' );
  fprintf ( 1, '  which minimizes the RMS error to a set of N data values.\n' );

  [ a, b ] = llsq ( n, x_data, y_data );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Estimated relationship is y = %g * x + %g\n', a, b );
  fprintf ( 1, '  Expected value is         y = 61.272 * x - 39.062\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '     I      X       Y        B+A*X      |error|\n' );
  fprintf ( 1, '\n' );
  error = 0.0;
  for i = 1 : n
    fprintf ( 1, '  %4d  %7f  %7f  %7f  %7f\n', ...
      i, x_data(i), y_data(i), b + a * x_data(i), b + a * x_data(i) - y_data(i) );
    error = error + ( b + a * x_data(i) - y_data(i) ).^2;
  end
  error = sqrt ( error / n );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  RMS error =                           %g\n', error );
%
%  Plot the data versus the approximate linear relationship.
%
  clf ( );
  hold ( 'on' );
    x_line = linspace ( xmin, xmax, 101 );
    y_line = b + a * x_line;
    plot ( x_line, y_line, 'b-', 'Linewidth', 3 );
    plot ( x_data, y_data, 'r.', 'Markersize', 25 );
    grid ( 'on' );
    xlabel ( '<-- Year -->' );
    ylabel ( '<-- Weeks of Work -->' );
    title ( 'Labor required to buy one measure of wheat.' );
  hold ( 'off' );
%
%  Save a copy of the plot.
%
  filename = 'llsq_test04.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end
