function [uout,DuoutDx] = pdeval(m,x,ui,xout)
%PDEVAL  Evaluate the solution computed by PDEPE and its spatial derivative.
%   When an array SOL is returned by PDEPE, UI = SOL(j,:,i) approximates 
%   component i of the solution of the PDE at time T(j) and mesh points X.  
%   [UOUT,DUOUTDX] = PDEVAL(M,X,UI,XOUT) evaluates the approximation and 
%   its partial derivative Dui/Dx at the array of points XOUT and returns 
%   them in UOUT and DUOUTDX, respectively.  
%
%   See also PDEPE.

%   Lawrence F. Shampine and Jacek Kierzenka
%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.3 $  $Date: 2000/06/02 00:11:28 $

uout = zeros(size(xout));
DuoutDx = zeros(size(uout));

if (min(xout) < x(1))|(x(end) < max(xout)) % entries of x are strictly increasing
  error('Attempting to evaluate outside [a, b].');
end

evaluated = 0;       % greatest index of xout already processed
bottom = 1;          % index of current subinterval in x

while bottom < length(x)
  % find xout that lie in the current subinterval in x
  index = find( (xout(evaluated+1:end) - x(bottom+1)) <= 0 );
  if ~isempty(index)
    [uout(evaluated+index),DuoutDx(evaluated+index)] = ...
               pdentrp(m,x(bottom),ui(bottom),x(bottom+1),ui(bottom+1),...
                       xout(evaluated+index));
    evaluated = evaluated + length(index);
  end
  bottom = bottom + 1;
end


