You are on page 1of 5

How to Calculate Eigenvectors and Eigenvalues in Matlab

Math 102 - Fall, 2007

While calculating things sure is fun, sometimes we need to choose efficiency over
diversion. This note describes the basics of matrix creation, manipulation, and
other matrix operations in Matlab. For more help, you can type: help elmat
in the Command Window for a list of elementary matrix operations, and help
matfun in the Command Window for a list of matrix functions. Clicking on
any entry in these lists takes you to a brief description of the Matlab command.
More help is available from the Help menu in the menu bar.
In what follows, I have copied and pasted what I typed from the Command
Window. You will only type what follows the Command Window prompt:
. Every line is ended by a carriage return. If you see lines that dont start
with the prompt, they were produced by Matlab.
Basics: How to generate a matrix and access its entries.
Make an mn matrix, A, with all zero entries: A = zeros(m,n). For example:
>> A=zeros(3,2);
>> A
A =
0
0
0

0
0
0

>>
The i, jth entry of a matrix A is accessed by typing A(i, j). For example:
>> A(2,1)=5
A =
0
5
0

0
0
0

You can define a matrix by typing in the entries, surrounded by SQUARE


brackets. Semi-colons are used to define a new line. For example:

>> B=[1 2 3; 4 5 6; 7 8 9];


>> B
B =
1
4
7

2
5
8

3
6
9

You can access a single row or a single column by using the short cut: : ,
which means all indices in this row or column. For example, to see the first
row of B or the second column of B:
>> B(1,:)
ans =
1

>> B(:,2)
ans =
2
5
8
>>
To see the second through third entries of the third row of B:
>> B(3,2:3)
ans =
8

To generate a 5 by 6 matrix of ones:


>> C=ones(5,6)
C =

1
1
1
1
1

1
1
1
1
1

1
1
1
1
1

1
1
1
1
1

1
1
1
1
1

1
1
1
1
1

To generate the 5 by 5 identity matrix:


>> D=eye(5)
D =
1
0
0
0
0

0
1
0
0
0

0
0
1
0
0

0
0
0
1
0

0
0
0
0
1

To find the eigenvalues and eigenvectors of a matrix: (I first define the matrix
given in Problem 19, Section 6.3)
>> A=[2 2 1; 1 3 1; 1 2 2]
A =
2
1
1

2
3
2

1
1
2

>> [v,e]=eig(A)
v =
-0.5774
-0.5774
-0.5774

-0.9045
0.3015
0.3015

0.8253
-0.5213
0.2172

0
1.0000
0

0
0
1.0000

e =
5.0000
0
0

The matrix v is a matrix whose columns are the eigenvectors of A corresponding


to the eigenvalues given as the diagonal elements of the matrix e. The eigenvectors are normalized so they have norm one. If you dont want this, you can
use the unbalanced option:
>> [v,e]=eig(A,nobalance)
v =
-1.0000
-1.0000
-1.0000

-1.0000
0.3333
0.3333

1.0000
-0.6316
0.2632

0
1.0000
0

0
0
1.0000

e =
5.0000
0
0

Sometimes the unbalanced version looks more like the numbers you would calculate by hand (if youre checking!). You can extract the eigenvalues from the
matrix and store them in a vector using:
>> evalues=diag(e)
evalues =
5.0000
1.0000
1.0000
The trace, determinant, and inverse of a matrix are calculated using:

>> trace(A)
ans =
7
>> det(A)
ans =

5
>> inv(A)
ans =
0.8000
-0.2000
-0.2000

-0.4000
0.6000
-0.4000

-0.2000
-0.2000
0.8000

Matrix multiplication is performed using the usual * command. Element-byelement multiplication (or division) can be done using the .* command:
>> A
A =
2
1
1

2
3
2

1
1
2

0
1
0

0
0
1

2
3
2

1
1
2

0
3
0

0
0
2

>> D=eye(3)
D =
1
0
0
>> A*D
ans =
2
1
1
>> A.*D
ans =
2
0
0

That should get you started!


5

You might also like