function yi = pwl_interp_1d ( nd, xd, yd, ni, xi )

%*****************************************************************************80
%
%% pwl_interp_1d() evaluates the piecewise linear interpolant.
%
%  Discussion:
%
%    The piecewise linear interpolant L(ND,XD,YD)(X) is the piecewise
%    linear function which interpolates the data (XD(I),YD(I)) for I = 1
%    to ND.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    18 August 2012
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer ND, the number of data points.
%    ND must be at least 1.
%
%    real XD(ND,1), the data points.
%
%    real YD(ND,1), the data values.
%
%    integer NI, the number of interpolation points.
%
%    real XI(NI,1), the interpolation points.
%
%  Output:
%
%    real YI(NI,1), the interpolated values.
%

%
%  Destroy all row vectors!
%
  xd = xd(:);
  yd = yd(:);
  xi = xi(:);

  if ( nd == 1 )
    yi(1:ni,1) = yd;
    return
  end

  [ k, k ] = histc ( xi, xd );
%
%  Force data to the left of the interval to use subinterval [1,2],
%  and to the right of the subinterval, [nc-1,nc].
%
  k ( k == 0 ) = 1;
  k ( k == nd ) = nd - 1;
%
%  Assign T, the relative location of the data within its subinterval.
%
  t = ( xi - xd(k,1) ) ./ ( xd(k+1,1) - xd(k,1) );
%
%  The i-th data value uses 1-t times the left control value,
%  and t times the right control value.
%
  yi = ( 1.0 - t ) .* yd(k) + t .* yd(k+1);
  
  return
end
