% Numerical Mathematics and Computing, Fifth Edition
% Ward Cheney & David Kincaid
% Brooks/Cole Publ. Co.
% (c) 2004
%
% file: cpb8_3_1d.m

% Purpose: to test the eigenvalue command in Matlab
% on the family of tridiagonal matrices that come up in PDE,

% create a n x n matrix with tridiagonal
% structure and elements -1 2 -1 in each row. 
% We know a formula for the eigenvalues (NA3, p. 621). 
% Specifically, they are 2 -2 cos(j*pi/21). 

n=5
for i=1:n
  for j=1:n
    if abs(i-j)==0
       A(i,j)=2;
    elseif  abs(i-j)==1 
       A(i,j)=-1;
    else 
    A(i,j)=0;
    end
  end
end
A
eig(A)
for j=1:n
   lam(j)=2-2*cos(j*pi/(n+1))
end

