
function V=vandermonde_1D(basis_type,x,n,a,b)

%--------------------------------------------------------------------------
% THE MAIN PURPOSE OF THIS ROUTINE IS TO COMPUTE THE VANDERMONDE MATRIX OF
% DEGREE n WITH RESPECT TO A CERTAIN BASIS OF THE SPACE OF UNIVARIATE
% POLYNOMIALS IN THE INTERVAL [a,b]; THE BASIS IS DETERMINED BY basis_type.
%--------------------------------------------------------------------------
% INPUTS:
%---------
%
% basis_type: IT DETERMINES THE BASIS FOR THE DOMAIN.
%
%    [basis_type = 0] MONOMIAL (NOT SCALED).
%    [basis_type = 1] LEGENDRE (SCALED ON THE SMALLEST RECT. R=[a,b]x[c,d]
%                     CONTAINING THE DOMAIN.
%    [basis_type = 2] CHEBYSHEV (SCALED ON THE SMALLEST RECT. R=[a,b]x[c,d]
%                     CONTAINING THE DOMAIN.
%    [basis_type = 3] MONOMIAL (SCALED ON THE SMALLEST RECT. R=[a,b]x[c,d]
%                     CONTAINING THE DOMAIN.
%
% x : COLUMN VECTOR CONSISTING OF THE POINTS ON WHICH ALL THE POLYNOMIALS 
%     OF THE BASIS MUST BE EVALUATED.
%
% n : DEGREE OF THE VANDERMONDE BASIS.
% a,b: INTERVAL OF THE SPACE OF POLYNOMIALS OF DEGREE AT MOST n.
% 
%---------- 
% OUTPUTS:
%----------
%
% V: VANDERMONDE MATRIX, WRT THE BASIS DETERMINED BY basis_type, EVALUATED
%    IN THE POINTS THAT ARE THE COMPONENTS OF THE VECTOR x.
%
%--------------------------------------------------------------------------
% FUNCTIONS USED BY THIS ROUTINE.
%---------------------------------
% 
% 1. legendre_vandermonde
%
% THE FUNCTION legendre_vandermonde IS ATTACHED HERE. SEE THE END OF THIS
% FILE.
%
%--------------------------------------------------------------------------

%--------------------------------------------------------------------------
%% Copyright (C) 2007-2009 Alvise Sommariva, Marco Vianello.
%%
%% This program is free software; you can redistribute it and/or modify
%% it under the terms of the GNU General Public License as published by
%% the Free Software Foundation; either version 2 of the License, or
%% (at your option) any later version.
%%
%% This program is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%% GNU General Public License for more details.
%%
%% You should have received a copy of the GNU General Public License
%% along with this program; if not, write to the Free Software
%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
%%
%% Author:  Alvise Sommariva <alvise@euler.math.unipd.it>
%%          Marco Vianello   <marcov@euler.math.unipd.it>
%%
%% Date: November 12, 2009
%--------------------------------------------------------------------------

switch basis_type
case 0
    
    % MONOMIAL BASIS. NOT SCALED.
    s=(1:n)';
    
    V(:,1)=ones(size(x));
    for ip=2:n
        V(:,ip)=x.*V(:,ip-1);
    end
    
case 1 
    
    % LEGENDRE BASIS. SCALED.
        
    xx=(x-(a+b)/2)*(2/(b-a)); % SCALING.
    V = legendre_vandermonde (n-1,xx);  % VANDERMONDE.
    
case 2
    
    % CHEBYSHEV BASIS. SCALED.
    xx=(2*x-a-b)/(b-a); % SCALING.
    for index = 1:n
        V(:,index)=cos((index-1)*acos(xx)); % VANDERMONDE.
    end
   
case 3
    
    % MONOMIAL BASIS. SCALED.
    s=(1:n)';
    x=[x-(b+a)/2]*(2/(b-a));
    
    V(:,1)=ones(size(x));
    for ip=2:n
        V(:,ip)=x.*V(:,ip-1);
    end
    
end


%----------------------
% legendre_vandermonde
%----------------------

function V = legendre_vandermonde (n,x)

%--------------------------------------------------------------------------
%
% LEGENDRE_POLY evaluates the Legendre polynomials P(N)(X) at X.
%
%  Differential equation:
%
%    (1-X*X) * P(N)(X)'' - 2 * X * P(N)(X)' + N * (N+1) = 0
%
%  First terms:
%
%  P( 0)(X) =       1
%  P( 1)(X) =       1 X
%  P( 2)(X) =  (    3 X**2 -       1)/2
%  P( 3)(X) =  (    5 X**3 -     3 X)/2
%  P( 4)(X) =  (   35 X**4 -    30 X**2 +     3)/8
%  P( 5)(X) =  (   63 X**5 -    70 X**3 +    15 X)/8
%  P( 6)(X) =  (  231 X**6 -   315 X**4 + 105 X**2 -     5)/16
%  P( 7)(X) =  (  429 X**7 -  693 X**5 + 315 X**3 -    35 X)/16
%  P( 8)(X) =  ( 6435 X**8 -12012 X**6 +6930 X**4 -1260 X**2 +  35)/128
%  P( 9)(X) =  (12155 X**9 -25740 X**7 +18018 X**5 -4620 X**3 + 315 X)/128
%  P(10)(X) =  (46189 X**10-109395 X**8 +90090 X**6 -30030 X**4 +3465 X**2
%                 -63 ) /256
%
%  Recursion:
%
%    P(0)(X) = 1
%    P(1)(X) = X
%    P(N)(X) = ( (2*N-1)*X*P(N-1)(X)-(N-1)*P(N-2)(X) ) / N
%
%--------------------------------------------------------------------------

if ( n < 0 )
    V = [];
    return
end

V(:,1) = ones(size(x));

if ( n < 1 )
    return
end

V(:,2) = x;

for j = 2 : n
    V(:,j+1) = ((2*j-1)*x.* V(:,j)-(j-1)* V(:,j-1))/j;
end




