% This function displays Chebyshev polynomials;

function spectr_05
  
echo off, clc
clear all

X = (-1:1.0e-3:1);

figure(1); plot(X,Tk(X,0),X,Tk(X,1),X,Tk(X,2),X,Tk(X,3),X,Tk(X,4),X,Tk(X,50));
legend('T0','T1','T2','T3','T4','T5'); title('Chebyshev Polynomials');

figure(2); plot(X,Ln(X,0),X,Ln(X,1),X,Ln(X,2),X,Ln(X,3),X,Ln(X,4),X,Ln(X,5));
legend('L0','L1','L2','L3','L4','L5'); title('Legendre Polynomials');



function [u] = Tk( x, k )
  if ( min(x) < -1 | max(x) > 1 )
    disp('Argument out of range! Exiting ...')
    return;
  end;
  
  u = cos( k * acos( x ) );
  
  

function pvec = Ln( x, i )
% Function written by John Burkardt (PSU)
%
%  function pvec = legpoly ( i, x )
%
%  Evaluate the I-th NORMALIZED Legendre polynomial at X.
%  X may be a vector of points.
%  The output PVEC will have the same shape as X.
%
nx = length ( x );
%
%  Compute the value of the I-th unnormalized Legendre polynomial.
%
if ( i < 0 )
  pvec = zeros ( size ( x ) );
  return;
elseif ( i == 0 )
  pvec = ones ( size ( x ) );
elseif ( i == 1 ) 
  pvec = x;
else
  p1 = ones ( size ( x ) );
  p2 = x;
  for j = 2 : i
    p0 = p1;
    p1 = p2;
    p2 = ( ( 2*j-1) * x .* p1 - ( j - 1 ) * p0 ) / j;
  end
  pvec = p2;
end
%
%  Normalize.
%
pvec = sqrt ( 0.5 * ( 2 * i + 1 ) ) * pvec;
