% EVAL_POL: Evaluates a given vector of nodes 's' or 
% the Chebyschev nodes for the (n-1)th-order polynomial
% over a bounded interval [a,b].
%
% USAGE: [Phi,s]=eval_pol(a,b,n,s);
%
% input:
%       a: inferior extreme of the interval
%       b: superior extreme of the interval
%       n: number of nodes
%       s: a column vector with 'n' given nodes,
%          if 's' is not given then Chebyschev nodes 
%          are computed by default.
% output:
%       Phi: matrix with the Chebyschev polynomials 
%            evaluated at 's' or at Chevyschev nodes.
%       s : Chebyshev nodes, only computed when 's'
%           is not given.
%
% Angel Luis Lopez
% URL: http://www.angelluislopez.net Email: alopezr [at] iese [edu]

function [Phi,s]=eval_pol(a,b,n,s);

        if nargin<5 & ~exist('s')
            
        % Chebyshev Nodes
    
            aa=(a+b)/2;
            bb=(b-a)/2;
            ind=(pi/n)*((n-0.5):-1:0.5)';
            s=aa+bb*cos(ind);
            
        % Chebyshev Polynomials
        
            ind_i=((n-0.5):-1:0.5)'; 
            ind_j=(0:n-1);
            Phi=cos(ind_i*ind_j*(pi/n));
            
        else
            
            w = 2*((s-a)/(b-a))-1;
            Phi = ones(size(s,1),n);
            Phi(:,2) = w;
            w=2*w;
            for j=3:n
                Phi(:,j)=w.*Phi(:,j-1)-Phi(:,j-2);
            end
        end
     

