function bk = pwl_basis_1d ( nd, xd, ni, xi )

%*****************************************************************************80
%
%% pwl_basis_1d() evaluates a 1D piecewise linear basis function.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    01 July 2015
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer ND, the number of data points.
%
%    real XD(ND,1), the data points.
%
%    integer NI, the number of interpolation points.
%
%    real XI(NI,1), the interpolation points.
%
%  Output:
%
%    real BK(NI,ND), the basis functions at the interpolation points.
%
%
%  Destroy all row vectors!
%
  xd = xd(:);
  xi = xi(:);

  bk = zeros ( ni, nd );

  for j = 1 : nd

    yd = zeros ( nd, 1 );
    yd(j) = 1.0;
 
    [ k, k ] = histc ( xi, xd );
%
%  Use extrapolation, if necessary.
%
    k ( k == 0 ) = 1;
    k ( k == nd ) = nd - 1;

    t = ( xi - xd(k,1) ) ./ ( xd(k+1,1) - xd(k,1) );
    bk(1:ni,j) = ( 1 - t ) .* yd(k) + t .* yd(k+1);
  
  end

  return
end
