function p1_ex1 ( )

%*****************************************************************************80
%
%% P1_EX1 carries out project 1 exercise 1.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    20 February 2019
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'P1_EX1\n' );
  fprintf ( 1, '  Read dataset M.\n' );
  fprintf ( 1, '  Create order n=3 polynomial models\n' );
  fprintf ( 1, '  using normal equations, QR, and SVD\n' );
%
%  Read data set.
%
  data = load ( 'mexico.txt' );
  x = data(:,1);
  y = data(:,2);
  m = length ( x );
%
%  Plot the data.
%
  plot ( x, y, 'bo-', 'linewidth', 3 );
  xlabel ( '<-- Year -->' );
  ylabel ( '<-- Population -->' );
  title ( 'Mexican Population History', 'fontsize', 16 );
  grid ( 'on' )
  filename = 'p1_ex1_data.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Set up the linear system A * c = b
%
  n = 3;
  A = [ x.^0, x.^1, x.^2 ];
  b = y;
%
%  1) Set up and solve the normal equations.
%
  Anormal = A' * A;
  bnormal = A' * b;
  c1 = Anormal \ bnormal;
  r1 = A * c1 - b;
%
%  2) Use QR factorization.
%
  [ Q, R ] = qr ( A );
  Aqr = R;
  bqr = Q' * b;
  c2 = R \ bqr;
  r2 = A * c2 - b;
%
%  3) Use SVD pseudoinverse.
%
  c3 = pinv ( A ) * b;
  r3 = A * c3 - b;
%
%  Tabulate results.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, '  TABLE 1:\n' );
  fprintf ( 1, '  Method  ----------------c-------------     rms(r)\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '     1    %8.4g  %8.4g  %8.4g  %8.4g\n', c1, rms ( r1 ) );
  fprintf ( 1, '     2    %8.4g  %8.4g  %8.4g  %8.4g\n', c2, rms ( r2 ) );
  fprintf ( 1, '     3    %8.4g  %8.4g  %8.4g  %8.4g\n', c3, rms ( r3 ) );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'P1_EX1\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end