function px = legendre_polynomials ( n, x)

% INPUT:
% n: maximum degree.
% x: column vector of dimansion M.

% OUTPUT:
% px: is the Legendre polynomial of degree "n" evaluated at x.


%  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


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

px(:,1) = ones(size(x));
if ( n < 1 )
    return
end

px(:,2) = x;

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

px=px(:,end);

