function p1_ex5 ( )

%*****************************************************************************80
%
%% P1_EX5 carries out project 1 exercise 5.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    21 March 2019
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'P1_EX5\n' );
  fprintf ( 1, '  Create datasets S1 and S2.\n' );
  fprintf ( 1, '  S1 will be of sizes m1 = 10, 20, 40, 80, 160\n' );
  fprintf ( 1, '  S2 will be of size m2 = 25\n' );
  fprintf ( 1, '  Create polynomial models for S1, always of order n = 10.\n' );
  fprintf ( 1, '  Test them against the data in S2.\n' );
%
%  Create the S2 data set.
%
  m2 = 25;
  data2 = sinus ( m2 );
  x2 = data2(:,1);
  y2 = data2(:,2);
%
%  Fill in the table for m = 10, 20, 40, 80, 160
%
  fprintf ( 1, '\n' );
  fprintf ( 1, '  TABLE 5:\n' );
  fprintf ( 1, '   M    rms(r1)    rms(r2)\n' );
  fprintf ( 1, '\n' );
%
%  Set up the linear system A * c = b for [1,..,x^(n-1)]:
%
  n = 10;

  A = [];

  for log2 = 0 : 4
    m1 = 10 * 2^log2;
    data1 = sinus ( m1 );
    x1 = data1(:,1);
    y1 = data1(:,2);
    A = [];
    for j = 1 : n
      A = [ A, x1.^(j-1) ];
    end
    b = y1;
    c = pinv ( A ) * b;
    r1 = A * c - b;
    r2 = y2 - polyval ( c(n:-1:1), x2 );
    fprintf ( 1, '  %3d  %8.5f  %8.5f\n', m1, rms ( r1 ), rms ( r2 ) );
  end
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'P1_EX5\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end