function [ D, x ] = cheb ( N )

%*****************************************************************************80
%
%% cheb() computes the Chebyshev differentiation matrix and grid.
%
%  Modified:
%
%    24 May 2021
%
%  Author:
%
%    Lloyd Trefethen
%
%  Reference:
%
%    Lloyd Trefethen,
%    Spectral methods in MATLAB,
%    SIAM, 2000,
%    ISBN: 0898714656,
%    LC: QA377.T65.
%
%  Input:
%
%    integer N: the number of grid points to use.
%
%  Output:
%
%    real D(N,N): the differentiation matrix.
%
%    real X(N): the Chebyshev gridpoints.
%
  if ( N == 0 )
    D = 0.0;
    x = 1.0;
    return
  end

  x = cos ( pi * ( 0 : N ) / N )'; 

  c = [ 2.0; ones(N-1,1); 2.0 ] .* (-1.0).^(0:N)';
  X = repmat ( x, 1, N + 1 );
  dX = X - X';
%
%  Set the off diagonal entries.
%
  D = ( c * (1.0 ./ c )' ) ./ ( dX + ( eye ( N + 1 ) ) );
%
%  Diagonal entries.
%
  D = D - diag ( sum ( D' ) );

  return
end

