# Exploring Eigenvalues & Eigenvectors

## Introduction

In this activity we will learn how to calculate the eigenvalues of an matrix A using the characteristic equation. We will also learn how to use the MATLAB function eig() to calculate eigenvalues and eigenvectors. Finally, we will explore a geometric interpretation of matrices as transformations to see its effect on a set of points and also how the eigenvalues and eigenvectors are related to each other.

## Before starting

Use the MATLAB Live Editor to edit and run this Live Script in your browser or your desktop.
2. Run the code within a section before starting to read the next.
3. To run the code from each section, click into the code section and then click on the Run Section button (from the toolstrip) or click on the blue stripe on left side of that section as shown below:
Remark: Run the code of each section from top to bottom, otherwise you may get an error.
- The end of a section is indicated with a thin line, like the next one -

## 1. Eigenvalues & Eigenvectors

#### 1.1 Definition

An eigenvector of an matrix A is a nonzero column vector such that
,
for some number λ (possible complex), called the eigenvalue corresponding to . What the previous expression is saying is that the matrix-vector product gives the same result as just scaling the eigenvector by some value λ.
The eigenvalues of A are the roots of the characteristic polynomial
where I is the identity matrix.
For example, consider the matrix
which can be defined as:
A_1 = [1 3; 4 2]
Now, the characteristic polynomial we can defined using the command syms:
% The next line creates a symbolic variable 'lambda'
syms lambda
% MATLAB can now manipulate expressions using 'lambda'
p = det(A_1 - lambda * eye(2))

The roots of can be calculated with the MATLAB command solve(p). The result will be a array/vector containing the eigenvalues of . Try it for yourself! Add this command in the previous code.

#### 1.2 The eig() function

MATLAB has a built-in function called eig() to calculate the eigenvalues of a matrix and give corresponding eigenvectors. For example, to calculate the eigenvalues of the matrix , we write the following code:
eig(A_1)
which means that the two eigenvalues of A are -2 and 5. Compare this result with the solution of the characteristic polynomial from the previous section.
To find the eigenvectors you need to supply a vector of two variables to record the two outputs of the function eig (here we've called them V and D, but you can give them more meaningful variable names):
[V, D] = eig(A_1)
The columns of the first output, V, gives the eigenvectors of the matrix A_1, the diagonal elements of D give the eigenvalues corresponding to the eigenvectors in V, respectively. Thus, we have that the matrix has the eigenvalues and with respective eigenvectors
and .
Remark: It is important to emphasise that eigenvectors are not unique. It is easy to show this: If v is an eigenvector of the matrix A, then by definition for some scalar eigenvalue λ. Now, if you define for a scalar , then u is also an eigenvector because
.
Thus a multiple of an eigenvector is also an eigenvector.
For example, the vectors
and .
are also eigenvectors of A_1. Verify this in your notebook by showing that and .

#### Fun fact!

V and D are matrices that are sometimes known as the "eigenvalue decomposition of A", and it can be shown that
or .
You can check this fact in MATLAB with the following code:
V * D * V^(-1)
Is the output really equal to the matrix defined above?

### 2. Geometric interpretation of eigenvalues & eigenvectors

When the eigenvectors are 2-dimensional they can be plotted as arrows using the command
quiver(X,Y,U,V)
(sometimes we like to plot vectors with arrows to show the direction). This command plots arrows with directional components U and V at the Cartesian coordinates specified by X and Y. We can easily plot the same vector using the plot function; however it won't have an arrowhead.
For example, let's plot the eigenvectors of the matrix A_1. To do this we write:
clf; % clears the figure of previous plots, if there is any
hold on;
quiver(0, 0, V(1,1), V(2,1),... % 1st eigenvector
'Color','b', 'LineWidth', 1.5); % This adds format
quiver(0, 0, V(1,2), V(2,2),... % 2nd eigenvector
'Color','g', 'LineWidth', 1.5); % This adds format
hold off;
grid on, axis([-1 1 -1 1]);
legend('v_1','v_2', 'location', 'best');
The first eigenvector is shown in blue, and the second eigenvector is shown in green.

#### 2.1 Transformations

Multiplying a vector by a matrix on its left results in a new vector,
.
Therefore a matrix can be thought of as a transformation that takes a vector as an input and outputs a transformed vector.
For example, let's investigate the effect of the matrix
has on its eigenvectors under multiplication.
Run the following code to analyse the plot:
T = [3 1; 1 2]; % Define matrix T.
[evecs,evals] = eig(T) % Compute eigenstuff.
v1 = evecs(:,1); % First eigenvector, using matrix indexing
v2 = evecs(:,2); % Second eigenvector, using matrix indexing
u1 = T * v1; % Apply matrix T to each eigenvector,
u2 = T * v2; % we can also use the command 'mtimes()'.
clf; % clears the figure of previous plots, if there is any
hold on;
% Plot eigenvectors %
quiver(0, 0, v1(1), v1(2), 'Color', 'b', 'LineWidth', 3);
quiver(0, 0, v2(1), v2(2), 'Color', 'r', 'LineWidth', 3);
% Plot eigenvectors transformed by T %
quiver(0, 0, u1(1), u1(2), 'Color', 'm', 'LineWidth', 1.5);
quiver(0, 0, u2(1), u2(2), 'Color', 'k', 'LineWidth', 1.5);
hold off;
grid on, axis equal;
legend('v_1','v_2', 'Tv_1', 'Tv_2', 'location', 'best');
Note: Here we use matrix indexing to get each eigenvector. More information here: Matrix indexing.
The first eigenvector v1 is plotted in blue before being transformed by T, and then in magenta after its transformation, this is u1. The second vector v2 is plotted in red and then its transformation u2 in black. What do you notice about the two sets of vectors? What do you think it happens when you apply the transformation on other vectors (non-eigenvectors) e.g. and ?

## 3. Hands on practice

Let's practice what we just learned.

### Activity 1:

Here you will use the function eig() to find the eigenvalues and eigenvectors of the matrix
.
a) Use matrix indexing to give the eigenvalues of A explicitly. Use different variable names to identify them.

b) Use matrix indexing to the two eigenvectors of A explicitly. Use different variable names to identify them.

c) Use the command quiver() to plot the eigenvectors of A (if you want, plot also the eigenvectors transformed by A and compare them).

### Activity 2:

We can transform a set of points by a matrix T by the following equation
where T is a 2×2 matrix and . To visualise this transformation of points we will use the function matrixTransform defined in the Appendix (You do not need to modify, nor understand it).
Let's see how the matrix
transforms a set of points with the following code:
T = [3 1; 1 2];
matrixTransform(T)
Run this section to see the animation. It might take a few seconds. When the animation is done, you can drag the slider to apply the transformation manually.
Use the function matrixTransform to analyse the effect of the following matrices:
1. ,
2. with ,
3. .
Part 1 - Write your code here:

Part 2 - Write your code here:

Part 3 - Write your code here:

Once you wrote your code, run it to analyse the behaviour of each transformation. What do you notice? What do you wonder? What can you say about the eigenvectors for each case? If necessary, calculate the eigenvalues and eigenvectors?

## Appendix: Pre-defined functions

function [] = matrixTransform(T)

% 'matrixTransform' is a function which will visualise
% the effect of your 2-by-2 transformation matrix T on
% a set of grid points. It will also plot the eigenvectors
% of the matrix T if there are two real eigenvectors.

close all;

minX = -10;
maxX = 10;
minY = -10;
maxY = 10;
n = 10;

corners = [minX, maxX, minX, maxX; maxY, maxY, minY, minY];
corners = [corners, T*corners];
corners = [min(corners(:)) max(corners(:)) min(corners(:)) max(corners(:))] + pad*[-1,1,-1,1];

A = T-eye(2);

[ evec, evals ] = eig(T); % Dont' worry about the warning message :)

axis(corners);
axis equal;
xLim = get(gca, 'XLim');
yLim = get(gca, 'YLim');

hold on;
if trace(T)^2-4*det(T) > 0
nick = 1;
elseif trace(T)^2-4*det(T) == 0
if isreal(eig(R))
nick = 1;
else
nick = 0;
end
else
nick = 0;
end

newXY = zeros(2);

for t = linspace(0,1,100)
cla;
if nick
sevec = (A*t+eye(2))*evec;
quiver(0, 0, sevec(1,1)*10, sevec(2,1)*10, 'LineWidth', 2);
quiver(0, 0, sevec(1,2)*10, sevec(2,2)*10, 'LineWidth', 2);
end
for x = linspace(minX, maxX, n)
index = 1;
for y = linspace(minY, maxY, n)
if nick
newXY(:,index) = (A*t+eye(2))*[x;y];
else
[t1,r1] = cart2pol(x,y);
[t2,r2] = cart2pol(T(1)*x+T(2)*y,T(3)*x+T(4)*y);
if t2 >= t1
t2 = t2-2*pi;
end
[xp, yp] = pol2cart(t1+(t2-t1)*t, r1+(r2-r1)*t);
newXY(:, index) = [xp;yp];
end
index = index+1;
end
plot(newXY(1,:), newXY(2,:), '.', ...
'MarkerEdgeColor',[0.4 (maxX-x)/(maxX-minX) 0.8]);
plot(newXY(1,1), newXY(2,1), '.m');
plot([0, 0],yLim,'-k');
plot(xLim,[0,0],'-k');
end
grid on
if t == 0
pause(1);
else
pause(0.005);
end
end

end