function chebyshev_differentiation_test ( )

%*****************************************************************************80
%
%% chebyshev_differentiation_test() plots error in derivative estimates.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    13 March 2024
%
%  Author:
%
%    Original MATLAB version by Lloyd Trefethen.
%    This version by John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'chebyshev_differentiation_test():\n' );
  fprintf ( 1, '  Use the  Chebyshev differentiation matrix to estimate\n' );
  fprintf ( 1, '  the derivative of a smooth function.\n' );

  xx = linspace ( -1.0, +1.0, 201 );
  uu = exp ( xx ) .* sin ( 5.0 * xx );

  clf ( );

  for n = [ 10, 20 ]

    x = chebyshev_grid ( n );
    D = chebyshev_matrix ( n ); 

    u = exp ( x ) .* sin ( 5.0 * x );

    subplot ( 'position', [ 0.15, 0.66-0.4*(n==20), 0.31, 0.28 ] );
    plot ( x, u, '.', 'markersize', 14 );
    grid ( 'on' );
    line ( xx, uu );
    title ( [ 'u(x),  n=' int2str(n) ] );

    subplot ( 'position', [ 0.55, 0.66-0.4*(n==20), 0.31, 0.28 ] );
    err = D * u - exp(x) .* ( sin ( 5.0 * x ) + 5.0 * cos ( 5.0 * x ) );
    plot ( x, err, '.', 'markersize', 14 );
    grid ( 'on' );
    line ( x, err );
    title ( ['  error in u''(x),  n=' int2str(n) ] );

  end

  filename = 'chebyshev_differentiation_test.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end

