function yout = ntrp113(t,y,tout,klast,phi,psi)
%NTRP113 Interpolation helper function for ODE113.
%	YOUT = NTRP113(T,Y,TOUT,KLAST,PHI,PSI) approximates the solution at
%	TOUT by evaluating the polynomial computed in ODE113 at TOUT.
%	
%	See also ODE113.

%	Mark W. Reichelt and Lawrence F. Shampine, 6-13-94
%	Copyright (c) 1984-95 by The MathWorks, Inc.

hi = tout - t;
ki = klast + 1;
KI = 1:ki;

w = [ 1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13]';
g = [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]';
term = 0;
for j = 2:ki
  gamma = (hi + term) / psi(j-1);
  eta = hi / psi(j-1);
  for i = 1:ki+1-j
    w(i) = gamma * w(i) - eta * w(i+1);
  end
  g(j) = w(1);
  term = psi(j-1);
end

yout = y + hi * phi(:,KI) * g(KI);
