In order to successfully complete this assignment, you must do the required reading, watch the provided videos, and complete all instructions. The embedded survey form must be entirely filled out and submitted on or before 11:59pm on the day before class. Students must come to class the next day prepared to discuss the material covered in this assignment.

Pre-Class Assignment: Diagonalization and Powers

%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import sympy as sym
sym.init_printing()

1. Eigenvalues and eigenvectors review

Definition: A non-zero vector $x$ in $R^n$ is called an eigenvector of a $n\times n$ matrix $A$ if $Ax$ is a scalar multiple of $x$. If $Ax = \lambda x$, then $\lambda$ is called the eigenvalue of $A$ corresponding to $x$.

Steps for finding the eigenvalues and eigenvectors

We want to find $\lambda$ and non-zero vector $x$ such that $Ax=\lambda x$ for a $n\times n$ matrix.

  1. We introduce an identity matrix $I$ of $n\times n$. Then the equation becomes $$Ax = \lambda I x$$ $$Ax-\lambda I x = 0$$ $$(A-\lambda I)x = 0$$
  2. This suggests that we want to find $\lambda$ such that $(A-\lambda I)x=0$ has a non-trivial solution. It is equivalent to that the matrix $A-\lambda I$ is singular, i.e., has a determinant of $0$. $$|A-\lambda I|=0$$
  3. The determinant is polynomial in $\lambda$ (called the characteristic polynomial of $A$) with degree $n$. We solve this equation (called the characteristic equation) for all possible $\lambda$ (eigenvalues).
  4. After finding the eigenvalues, we substitute them back into $$(A-\lambda I)x=0$$ and find the eigenvectors $x$.

Let's calculate eigenvalues for the following matrix:

$$ A=\begin{bmatrix} 0 & 0 & -2 \\ 1 & 2 & 1 \\ 1 & 0 & 3 \end{bmatrix}$$

Find eigenvalues

Looking at the above recipe, let's solve the problem symbollically using sympy. First lets create a matrix $B$ such that:

$$B = A-\lambda I$$
#Most sympy requires defeing the variables as "symbols"
#Once we do this we can use the variables in place of numbers
lam = sym.symbols('lambda')

A = sym.Matrix([[0, 0 ,-2], [1, 2, 1], [1, 0, 3]])
I = sym.eye(3)

B = A - lam*I

B
$$\left[\begin{matrix}- \lambda & 0 & -2\\1 & - \lambda + 2 & 1\\1 & 0 & - \lambda + 3\end{matrix}\right]$$

Now, per step 2, the determinate of $B$ must be zero. Note that sympy calculates the determinate symbollically as follows:

B.det()
$$- \lambda \left(- \lambda + 2\right) \left(- \lambda + 3\right) - 2 \lambda + 4$$

**Do This:** Using the sympy.solve function on the determinate of $B$ to solve for lam ($\lambda$). Verify that the solution to the last question produces the same eigenvalues as above.

# Put your code to solve for det(B) = 0 here

**Do This:** First, let's use the built in funciton eigenvals function in sympy to calculate the eigenvalues. Find out the meaning of the output.

# Put your code here

Explain your output here.

Find eigenvectors

Now we know the eigenvalues, we can substitue them back into the equation to find the eigenvectors.
We solve this symbollically using sympy. First let's make a vector of our eigenvalues (from above):

eig = [1,2]

Now (per step 4 above) we need to solve the equation $(A-\lambda I)x=0$. One way to do this in sympy is as follows:

x1,x2,x3 = sym.symbols(['x_1','x_2','x_3'])

x = sym.Matrix([[x1],[x2],[x3]])
x
$$\left[\begin{matrix}x_{1}\\x_{2}\\x_{3}\end{matrix}\right]$$
for lam in eig:
    vec = sym.solve((A - lam*I)*x,x)
    print(vec)
{x_2: x_3, x_1: -2*x_3}
{x_1: -x_3}

**QUESTION:** Explain your output here. (Hint, you can also try the rref to find the solutions)

Put your answer here

**Do This:** Next, let's use the eigenvects function in sympy to find three linear independent eigenvectors for the matrix $A$?

# Put your answer to the above question here

**QUESTION:** Compare this answer to the eigenvectors we calculated above. Does this answer make sense? What does the syntax tell us?

Put your answer here

**DO THIS:** Find the eigenvalues and eigenvectors of the following matrix: $$ A2=\begin{bmatrix} 2 & 1 \\ 0 & 2 \end{bmatrix}$$

**QUESTION:** What are the eigenvalues for the matrix $A2$?

Put your answer to the above question here

**QUESTION:** What are the eigenvectors for the matrix $A2$?

Put your answer to the above question here


2. Diagonalizable Matrix

In class we will be using matrix diagonalization to solve some problems.

Matrix $A$ is diagonalizable if there exists a diagonal matrix $D$ that is similar similar to $A$:

$$ D = C^{-1}AC$$

If matrix $A$ has linearly independent eigenvectors ($v_1, \ldots v_n$) then $A$ is diagonalizable with the following solution:

$$C = \left[ v_1^T, \ldots, v_n^T \right]$$

In other words, each column of $C$ is a linearly independent eigenvector of $A$. The diagonal matrix $D$ is

$$ D = \left[ \begin{matrix} \lambda_1 & 0 & 0 \\ 0 & \ddots & 0 \\ 0 & 0 & \lambda_n \end{matrix} \right] $$

In other-other words, $D$ consists of the corresponding eigenvalues.

%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import sympy as sym
sym.init_printing(use_unicode=True)

**DO THIS:** Using numpy, Diagonalize (i.e. calculate C and D) the following matrix:

A = np.matrix([[5, -2, 2], [4, -3, 4], [4,-6,7]])
sym.Matrix(A)
$$\left[\begin{matrix}5 & -2 & 2\\4 & -3 & 4\\4 & -6 & 7\end{matrix}\right]$$
# Put your answer here
from answercheck import checkanswer

checkanswer.matrix(D,'56821475223b52e0b6e751da444a1441');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-13-63b252aa348e> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.matrix(D,'56821475223b52e0b6e751da444a1441');

NameError: name 'D' is not defined

**DO THIS:** Verify that $A$ is in fact Diagonalizable by calculating $D2 = C^{-1}AC$ and comparing it to your original $D$ using np.allclose.

#Put your verificaiton code here.
np.allclose(D,D2)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-15-a8c9c2fe1a83> in <module>
----> 1 np.allclose(D,D2)

NameError: name 'D' is not defined

Diagonalization of Symmetric Matrices

One special case is Symmetric Matrices. It can be shown that symmetric Matrices are Diagonalizable and the resulting eigenvectors are not only linearly independent but also orthogonal. Since this is true, the equation changes to:

$$ D = C^{T}AC$$

**QUESTION:** Why do we care if $C$ is orthogonal? What advantages does the above equation give us?

Put your answer to the above question here.


3. Assignment wrap-up

Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credit for the assignment!

Direct Link to Google Form

If you have trouble with the embedded form, please make sure you log on with your MSU google account at googleapps.msu.edu and then click on the direct link above.

**Assignment-Specific QUESTION:** Why do we care if $C$ is orthogonal? What advantages does the above equation give us?

Put your answer to the above question here

**QUESTION:** Summarize what you did in this assignment.

Put your answer to the above question here

**QUESTION:** What questions do you have, if any, about any of the topics discussed in this assignment after working through the jupyter notebook?

Put your answer to the above question here

**QUESTION:** How well do you feel this assignment helped you to achieve a better understanding of the above mentioned topic(s)?

Put your answer to the above question here

**QUESTION:** What was the most challenging part of this assignment for you?

Put your answer to the above question here

**QUESTION:** What was the least challenging part of this assignment for you?

Put your answer to the above question here

**QUESTION:** What kind of additional questions or support, if any, do you feel you need to have a better understanding of the content in this assignment?

Put your answer to the above question here

**QUESTION:** Do you have any further questions or comments about this material, or anything else that's going on in class?

Put your answer to the above question here

**QUESTION:** Approximately how long did this pre-class assignment take?

Put your answer to the above question here

from IPython.display import HTML
HTML(
"""
<iframe 
	src="https://cmse.msu.edu/cmse314-pc-survey" 
	width="100%" 
	height="1000px" 
	frameborder="0" 
	marginheight="0" 
	marginwidth="0">
	Loading...
</iframe>
"""
)

Congratulations, we're done!

To get credits for this assignment, you must fill out and submit the above survey form on or before the assignment due date.

Course Resources:

Written by Dirk Colbry, Michigan State University Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.