Octave for Chapter 8
Examples in this section provide sample Octave code for computing eigenvalues. You can access our code through the link at the bottom of each template. Feel free to modify the code and experiment to learn more!
You can write your own code using Octave software or online Octave cells. To access Octave cells online, go to the Sage Math Cell Webpage, select OCTAVE as the language, enter your code, and press EVALUATE.
To ”save” or share your online code, click on the Share button, select Permalink, then copy the address directly from the browser window. You can store this link to access your work later or share this link with others. You will need to get a new Permalink every time you modify the code.
Octave Tutorial
% Define A
A=[5 0 0;
1 2 -1;
1 3 -2];
% Find eigenvalues of A
eigen_A=eig(A)
% Find eigenvalues and eigenvectors of A
[V,D]=eig(A)
% D is a diagonal matrix whose diagonal entries are the eigenvalues of A
% Columns of V are the eigenvectors of A
% Define A
A=[5 0 0;
1 2 -1;
1 3 -2];
% Find the coefficients of the char poly of A
char_poly = poly(A)
The output looks like this:
char_poly =
1 -5 -1 5
The output is a vector of coefficients, starting with the leading coefficient. In this case, we see that the characteristic polynomial is .
% Define A
A=[sqrt(2)/2 -sqrt(2)/2;
sqrt(2)/2 sqrt(2)/2];
% Find eigenvalues of A
eigen=eig(A)
% Find eigenvalues and eigenvectors of A
[V,D]=eig(A)
% D is a diagonal matrix whose diagonal entries are the eigenvalues of A
% Columns of V are the eigenvectors of A
This is what we get for the output:
eigen =
(0.707107,0.707107)
(0.707107,-0.707107)
V =
(0,0.707107) (0,-0.707107)
(0.707107,0) (0.707107,-0)
D =
(0.707107,0.707107) (0,0)
(0,0) (0.707107,-0.707107)
Each complex number in the output is presented using its real and imaginary parts as and coordinates of a point. For example, the second eigenvalue, written as , is the complex number . Compare this to the eigenvalue in Example ex:eigsrotation. The corresponding eigenvector is the second column of the matrix : . This should be interpreted as . Compare this to the eigenvector in Example ex:eigsrotation. There is a discrepancy! Did we make a mistake? How do we reconcile the two eigenvectors?
Octave Exercises
- (a)
- Use Octave to find the eigenvalues of .
- (b)
- Find the eigenvalues of . How do they compare?
- (c)
- Formulate and prove a conjecture about the relationship between the eigenvalues of and the eigenvalues of , where is a constant.
- (d)
- How do the eigenvectors of compare to the eigenvectors of ? Prove your claim.
- (a)
- Find the eigenvalues of using Octave. List the eigenvalues below in increasing order.
- (b)
- By hand, find the corresponding eigenvectors of . List them below.
- (c)
- Use Octave to find the eigenvectors of . Reconcile your answers from the previous part with the answers you got from Octave.
ans =
16.1168
-1.11684
-1.30368e-15
Should we interpret the last eigenvalue as , or as a very small non-zero number? How can you be sure of your answer in this particular case?