% Programming and Numerical Methods
% MSc in Computational and Applied Physics

% Code 6: 
% Generation of the first L Legendre polynomials 
% using recurrence   formulae
% A. Meseguer (2007)

  clear all 
  close all

% Number of Lagrange polynomials to be computed
  L = 6 ;
 
% Grid generation
  N = 100 ; x = -1 + (2/N)*[0:N]' ;
% We first create L_0 and L_1
  L0 = 1 + 0*x ; L1 = x ;

% We store from L_0 to L_L in a (N+1) x (L+1) matrix
  LM = zeros(N+1,L+1) ;

  LM(:,1) = L0 ; LM(:,2) = L1 ;
  for k = 1:L-1
   icol = k + 1 ;
   LM(:,icol+1) = (2*k+1)*x.*LM(:,icol)/(k+1) - (k/(k+1))*LM(:,icol-1) ;
  end
   
 ['for next polynomial press any key']
  
  for icol = 1:L+1
   cc = rand(3,1) ;
   plot(x,LM(:,icol),'color',cc,'linewidth',2.5) ; hold on
   text(x(12),LM(12,icol)-.1,['L',int2str(icol-1)],'color',cc,'fontsize',14)
   axis([-1 1 -1 1])
   grid on
   drawnow
   
   pause
  end
