function least_squares_approximant_test01 ( prob, degree )

%*****************************************************************************80
%
%% least_squares_approximant_test01() tests the test_interp() examples.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    15 November 2022
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'least_squares_approximant_test01():\n' );
  fprintf ( 1, '  Approximate data from test_interp() problem #%d\n', prob );
  fprintf ( 1, '  using a least squares polynomial of degree %d\n', degree );

  dim_num = p00_dim_num ( prob );

  if ( dim_num ~= 2 )
    fprintf ( 1, '\n' );
    fprintf ( 1, '  This is not a 1D interpolation problem.\n' );
    return
  end

  nd = p00_data_num ( prob );
  fprintf ( 1, '  Number of data points = %d\n', nd );

  xy = p00_data ( prob, dim_num, nd );
  
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Data array:\n' );
  disp ( xy' );
 
  xd = reshape ( xy(1,1:nd), nd, 1 );
  yd = reshape ( xy(2,1:nd), nd, 1 );

  order = degree + 1;
  c = least_squares_approximant_coef ( nd, xd, yd, order );
%
%  #1:  Compare approximant to data.
%
  ni = nd;
  xi = xd;
  yi = poly_value ( order, c, ni, xi );

  int_error = norm ( yi - yd ) / ni;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Averaged L2 approximation error at data nodes = %g\n', int_error );
%
%  #2: Compare estimated curve length to piecewise linear (minimal) curve length.
%  Assume data is sorted, and normalize X and Y dimensions by (XMAX-XMIN) and
%  (YMAX-YMIN).
%
  xmin = min ( xd(1:nd) );
  xmax = max ( xd(1:nd) );
  ymin = min ( yd(1:nd) );
  ymax = max ( yd(1:nd) );

  ni = 501;
  xi = linspace ( xmin, xmax, ni );
  xi = xi';
  yi = poly_value ( order, c, ni, xi );

  ld = sum ( sqrt ( ( ( xd(2:nd) - xd(1:nd-1) ) / ( xmax - xmin ) ).^2 ...
                  + ( ( yd(2:nd) - yd(1:nd-1) ) / ( ymax - ymin ) ).^2 ) );

  li = sum ( sqrt ( ( ( xi(2:ni) - xi(1:ni-1) ) / ( xmax - xmin ) ).^2 ...
                  + ( ( yi(2:ni) - yi(1:ni-1) ) / ( ymax - ymin ) ).^2 ) );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Normalized length of piecewise linear interpolant = %g\n', ld );
  fprintf ( 1, '  Normalized length of least squares approximant    = %g\n', li );
%
%  #3: Plot the data.
%
  figure ( 1 );
  clf ( );
  hold ( 'on' );
  plot ( xd, yd, 'b-', 'Linewidth', 3 );
  plot ( xd, yd, 'k.', 'Markersize', 20 );
  xlabel ( '<--- X --->' );
  ylabel ( '<--- Y --->' );
  title ( 'Piecewise Linear Interpolant' );
  grid ( 'on' );
  hold ( 'off' );
  filename = sprintf ( 'p%02d_data.png', prob );
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s".\n', filename );
%
%  #4: Plot the piecewise linear and polynomial interpolants.
%
  figure ( 2 );
  clf ( );
  ni = 101;
  xmin = min ( xd );
  xmax = max ( xd );
  xi = linspace ( xmin, xmax, ni );
  yi = poly_value ( order, c, ni, xi );

  hold ( 'on' );
  plot ( xi, yi, 'r-', 'Linewidth', 3 );
  plot ( xd, yd, 'b-', 'Linewidth', 3 );
  plot ( xd, yd, 'k.', 'Markersize', 20 );
  xlabel ( '<--- X --->' );
  ylabel ( '<--- Y --->' );
  title ( sprintf ( 'Least Squares Polynomial Approximant of Degree %d', degree ) );
  grid ( 'on' );
  hold ( 'off' );
  filename = sprintf ( 'p%02d_d%02d.png', prob, degree );
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s".\n', filename );

  return
end
