% plot_eigenvectors
%    helper function to plot eigenvectors of a matrix
%    requires availability of arrow.m
% 
% by Gavin LaRose, 10/31/2012
% 
% (c)2012 Regents of the University of Michigan
% This work is licensed under the
% Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
% To view a copy of this license, visit
% http://creativecommons.org/licenses/by-nc-sa/3.0/
% or send a letter to
% Creative Commons,
% 444 Castro Street, Suite 900,
% Mountain View, CA, 94041, USA.
%
function plot_eigenvectors( varargin )

% required input are the axes and matrix
    figaxes = varargin{1};
    mat     = varargin{2};
% optional are the arrow length and color
    if length( varargin ) > 2
        arrow_length = varargin{3};
    else
        arrow_length = 0.5;
    end
    if length( varargin ) > 3
        arrow_color = varargin{4};
    else
        arrow_color = 'k';
    end
    
% find the eigenvalues and vectors
    [ evecs, lambdas ] = eig( mat );
    
% plot them
    axes( figaxes );
    if imag(lambdas(1,1)) == 0
        vec1 = evecs(1:2,1);
        vec2 = evecs(1:2,2);
        nvec1 = (arrow_length/norm(vec1))*vec1';
        nvec2 = (arrow_length/norm(vec2))*vec2';
        if lambdas(1,1) > 0
            arrow( [0 0], nvec1, 'Width', 2, 'EdgeColor', arrow_color, ...
                    'FaceColor', arrow_color );
            arrow( [0 0], -nvec1, 'Width', 2, 'EdgeColor', arrow_color, ...
                    'FaceColor', arrow_color );
        else
            arrow( nvec1, [0 0], 'Width', 2, 'EdgeColor', arrow_color, ...
                    'FaceColor', arrow_color );
            arrow( -nvec1, [0 0], 'Width', 2, 'EdgeColor', arrow_color, ...
                    'FaceColor', arrow_color );
        end
        if lambdas(2,2) > 0
            arrow( [0 0], nvec2, 'Width', 2, 'EdgeColor', arrow_color, ...
                    'FaceColor', arrow_color );
            arrow( [0 0], -nvec2, 'Width', 2, 'EdgeColor', arrow_color, ...
                    'FaceColor', arrow_color );
        else
            arrow( nvec2, [0 0], 'Width', 2, 'EdgeColor', arrow_color, ...
                    'FaceColor', arrow_color );
            arrow( -nvec2, [0 0], 'Width', 2, 'EdgeColor', arrow_color, ...
                    'FaceColor', arrow_color );
        end
    end
end
