function llsq_test02 ( )

%*****************************************************************************80
%
%% LLSQ_TEST02 calls LLSQ to approximate data that is actually quadratic.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license. 
%
%  Modified:
%
%    08 April 2018
%
%  Author:
%
%    John Burkardt
%
  n = 15;
  x_data = - 0.5 + 3.0 * rand ( n, 1 );
  y_data = 1.0 + 0.5 * x_data .^ 2 + 0.2 * ( 2 * rand ( n, 1 ) - 1 );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'LLSQ_TEST02\n' );
  fprintf ( 1, '  LLSQ 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, '\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_min = min ( x_data );
    x_max = max ( x_data );
    x_line = linspace ( x_min, x_max, 101 );
    y_line = b + a * x_line;
    y_line2 = 1.0 + 0.5 * x_line.^2;
    plot ( x_line, y_line, 'r-', 'Linewidth', 2 );
    plot ( x_data, y_data, 'b.', 'Markersize', 25 );
    plot ( x_line, y_line2, 'g-', 'Linewidth', 2 );
    grid on
    xlabel ( '<-- X -->' );
    ylabel ( '<-- Y -->' );
    title ( '  Quadratic formula (green), perturbed data (blue), linear least squares fit (red).' );
  hold off
%
%  Save a copy of the plot.
%
  filename = 'llsq_test02.png';
  print ( '-dpng', filename );

  return
end
