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

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


  clear all 
  close all

% Number of Chebyshev polynomials to be computed (from 0 to L)
  L = 4 ;
 
% Grid generation
  N = 100 ; x = -1 + (2/N)*[0:N]' ;
% We first create T_0 and T_1
  T0 = 1 + 0*x ; T1 = x ;

% We store from T_0 to T_L in a (N+1) x (L+1) matrix
  TM = zeros(N+1,L+1) ;

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