%
% HW6_matlab_tip.m
%
% This script calculates the eigenvalues and eigenvectors as we would
% calculate them by hand (different from the generalized eigenvectors
% calculated by Matlab's 'eig' function)
%
% Author:  J Cardell
% Date:  March 2005
%

% Example on page 82 of Luenberger
disp('Calculating eigenvalues and eigenvectors the long way:');
A = [2 1; 2 3]      % System matrix from example

p = poly(A);        % the characteristic polynomial of matrix A
r = roots(p)        % the roots of p are the eigenvalues of A

% Calculate the eigenvectors using the standard characteristic equation.
% The eigenspace for each eigenvalue is captured by finding the nullspace
% of (A - lambda*I).  The 'r' switch causes Matlab to compute the
% eigenvector similar to the typical hand approach.  Without the 'r'
% switch, Matlab computes an orthonormal basis for the eigen space.
[m,n] = size(A);
e1 = null((A - r(1)*eye(m)), 'r');
e2 = null((A - r(2)*eye(m)), 'r');
M = [e1, e2]

% Alternatively:  Calculate the eigenvectors and eigenvalues using the builtin Matlab
% function. You can use the help command to learn more about this function
disp('Compared to calculating the eigenvectors with Matlabs builtin eig function');
[V,D] = eig(A)
