You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have discussed the trajectory of the vector $v$ after being transformed by $A$.
962
+
963
+
Consider the matrix $A = \begin{bmatrix} 1 & 2 \\ 1 & 1 \end{bmatrix}$ and the vector $v = \begin{bmatrix} 2 \\ -2 \end{bmatrix}$.
964
+
965
+
Try to compute the trajectory of $v$ after being transformed by $A$ for $n=6$ iterations and plot the result.
966
+
967
+
```
968
+
969
+
970
+
```{solution-start} eig1_ex2
971
+
:class: dropdown
972
+
```
973
+
974
+
```{code-cell} ipython3
975
+
A = np.array([[1, 2],
976
+
[1, 1]])
977
+
v = (0.4, -0.4)
978
+
n = 11
979
+
980
+
# Compute right eigenvectors and eigenvalues
981
+
eigenvalues, eigenvectors = np.linalg.eig(A)
982
+
983
+
print(f"eigenvalues:\n {eigenvalues}")
984
+
print(f"eigenvectors:\n {eigenvectors}")
985
+
986
+
plot_series(A, v, n)
987
+
```
988
+
989
+
We find the trajectory of the vector $v$ after being transformed by $A$ for $n=6$ iterations and plot the result seems to converge to the eigenvector of $A$ with the largest eigenvalue.
990
+
991
+
Let's use a vector field to visualize the transformation brought by A.
992
+
993
+
```{code-cell} ipython3
994
+
# Create a grid of points (vector field)
995
+
x, y = np.meshgrid(np.linspace(-5, 5, 15), np.linspace(-5, 5, 20))
996
+
997
+
# Apply the matrix A to each point in the vector field
998
+
vec_field = np.stack([x, y])
999
+
u, v = np.tensordot(A, vec_field, axes=1)
1000
+
1001
+
1002
+
# Plot the transformed vector field
1003
+
c = plt.streamplot(x, y, u - x, v - y, density=1, linewidth=None, color='#A23BEC')
1004
+
c.lines.set_alpha(0.5)
1005
+
c.arrows.set_alpha(0.5)
1006
+
1007
+
# Plot the eigenvectors as long blue and green arrows
plt.title("Convergence/Divergence towards Eigenvectors")
1018
+
plt.xlabel("x-axis")
1019
+
plt.ylabel("y-axis")
1020
+
plt.grid()
1021
+
plt.gca().set_aspect('equal', adjustable='box')
1022
+
plt.show()
1023
+
```
1024
+
1025
+
Note that the vector field converges to the eigenvector of $A$ with the largest eigenvalue and diverges from the eigenvector of $A$ with the smallest eigenvalue.
1026
+
1027
+
In fact, the eigenvectors are also the directions in which the matrix $A$ stretches or shrinks the space.
1028
+
1029
+
Specifically, the eigenvector with the largest eigenvalue is the direction in which the matrix $A$ stretches the space the most.
1030
+
1031
+
We will see more intriguing examples of eigenvectors in the following exercise.
1032
+
1033
+
```{solution-end}
1034
+
```
1035
+
1036
+
```{exercise}
1037
+
:label: eig1_ex3
1038
+
1039
+
{ref}`Previously <plot_series>`, we demonstrated the trajectory of the vector $v$ after being transformed by $A$ for three different matrices.
1040
+
1041
+
Use the visualization in the previous exercise to explain why the trajectory of the vector $v$ after being transformed by $A$ for the three different matrices.
1042
+
1043
+
```
1044
+
1045
+
1046
+
```{solution-start} eig1_ex3
1047
+
:class: dropdown
1048
+
```
1049
+
1050
+
Here is one solution
1051
+
1052
+
```{code-cell} ipython3
1053
+
import numpy as np
1054
+
import matplotlib.pyplot as plt
1055
+
from matplotlib.lines import Line2D
1056
+
1057
+
figure, ax = plt.subplots(1,3, figsize = (15,5))
1058
+
A = np.array([[sqrt(3) + 1, -2],
1059
+
[1, sqrt(3) - 1]])
1060
+
A = (1/(2*sqrt(2))) * A
1061
+
1062
+
B = np.array([[sqrt(3) + 1, -2],
1063
+
[1, sqrt(3) - 1]])
1064
+
B = (1/2) * B
1065
+
1066
+
C = np.array([[sqrt(3) + 1, -2],
1067
+
[1, sqrt(3) - 1]])
1068
+
C = (1/sqrt(2)) * C
1069
+
1070
+
examples = [A, B, C]
1071
+
1072
+
for i, example in enumerate(examples):
1073
+
M = example
1074
+
1075
+
# Compute right eigenvectors and eigenvalues
1076
+
eigenvalues, eigenvectors = np.linalg.eig(M)
1077
+
print(f'Example {i+1}:\n')
1078
+
print(f'eigenvalues:\n {eigenvalues}')
1079
+
print(f'eigenvectors:\n {eigenvectors}\n')
1080
+
1081
+
eigenvalues_real = eigenvalues.real
1082
+
eigenvectors_real = eigenvectors.real
1083
+
1084
+
# Create a grid of points (vector field)
1085
+
x, y = np.meshgrid(np.linspace(-20, 20, 15), np.linspace(-20, 20, 20))
1086
+
1087
+
# Apply the matrix A to each point in the vector field
1088
+
vec_field = np.stack([x, y])
1089
+
u, v = np.tensordot(M, vec_field, axes=1)
1090
+
1091
+
# Plot the transformed vector field
1092
+
c = ax[i].streamplot(x, y, u - x, v - y, density=1, linewidth=None, color='#A23BEC')
1093
+
c.lines.set_alpha(0.5)
1094
+
c.arrows.set_alpha(0.5)
1095
+
1096
+
# Plot the eigenvectors as long blue and green arrows
The pattern demonstrated here is because we have complex eigenvalues and eigenvectors.
1111
+
1112
+
It is important to acknowledge that there is a complex plane.
1113
+
1114
+
If we add the complex axis for the plot, the plot will be more complicated.
1115
+
1116
+
Here we used the real part of the eigenvalues and eigenvectors.
1117
+
1118
+
We can try to plot the complex plane for one of the matrix using `Arrow3D` class retrieved from [stackoverflow](https://stackoverflow.com/questions/22867620/putting-arrowheads-on-vectors-in-matplotlibs-3d-plot).
0 commit comments