# 15 Pre-Class Assignment: Diagonalization and Powers¶

## Readings for this topic (Recommended in bold)¶

## Goals for today’s pre-class assignment¶

## 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.

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\)$

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\)$

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).

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:

#### Find eigenvalues¶

Looking at the above recipe, let’s solve the problem symbollically using `sympy`

. First lets create a matrix \(B\) such that:

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

```
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-97c04e533e83> in <module>
----> 1 get_ipython().run_line_magic('matplotlib', 'inline')
2 import matplotlib.pylab as plt
3 import numpy as np
4 import sympy as sym
5 sym.init_printing()
~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
2342 kwargs['local_ns'] = self.get_local_scope(stack_depth)
2343 with self.builtin_trap:
-> 2344 result = fn(*args, **kwargs)
2345 return result
2346
~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/decorator.py in fun(*args, **kw)
230 if not kwsyntax:
231 args, kw = fix(args, kw, sig)
--> 232 return caller(func, *(extras + args), **kw)
233 fun.__name__ = func.__name__
234 fun.__doc__ = func.__doc__
~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
185 # but it's overkill for just that one bit of state.
186 def magic_deco(arg):
--> 187 call = lambda f, *a, **k: f(*a, **k)
188
189 if callable(arg):
~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/magics/pylab.py in matplotlib(self, line)
97 print("Available matplotlib backends: %s" % backends_list)
98 else:
---> 99 gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui)
100 self._show_matplotlib_backend(args.gui, backend)
101
~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/interactiveshell.py in enable_matplotlib(self, gui)
3511 """
3512 from IPython.core import pylabtools as pt
-> 3513 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
3514
3515 if gui != 'inline':
~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/pylabtools.py in find_gui_and_backend(gui, gui_select)
278 """
279
--> 280 import matplotlib
281
282 if gui and gui != 'auto':
ModuleNotFoundError: No module named 'matplotlib'
```

```
#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
```

Now, per step 2, the determinate of \(B\) must be zero. Note that `sympy`

calculates the determinate symbollically as follows:

```
B.det()
```

✅ **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
```

```
for lam in eig:
vec = sym.solve((A - lam*I)*x,x)
print(vec)
```

✅ **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\):

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

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

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)
```

```
# Put your answer here
```

```
from answercheck import checkanswer
checkanswer.matrix(D,'56821475223b52e0b6e751da444a1441');
```

✅ **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)
```

### 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:

✅ **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¶

✅ **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

Written by Dr. Dirk Colbry, Michigan State University

This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.