function value = ortval ( po , X )

%*****************************************************************************80
%
%MATLAB version of SUBROUTINE ORTVAL on page 266 of conte/deboor
%  returns the value at  X  of the polynomial of degree < NTERMS
%  given by
%     D(1)*P0(x) + D(2)*P1(x) + ... + D(NTERMS)*PNTERMSm1(x)
%  with the sequence  PO, P1, ..., of orthogonal polynomials generated
%  by the three-term recurrence
%     PJp1(x) = (X - B(J+1))*PJ(X) - C(J+1)*PJm1(X),   all J
%  with the sequences  B , C, D provided by the structure  po.
%  The  X  may be a sequence or even a higher-dimensional array, 
%  in which case  value  is the array of corresponding function values
%
%  Reference:
%
%    Samuel Conte, Carl de Boor,
%    Elementary Numerical Analysis,
%    Third Edition,
%    SIAM, 2017,
%    ISBN: 978-1-611975-19-2.
%
PREV = 0; value = repmat(po.D(end),size(X));

for k = length(po.D)-1:-1:1
   PREV2 = PREV; 
   PREV = value; value = po.D(k) + (X - po.B(k)).*PREV - po.C(k+1)*PREV2;
end
return, end
 

