Lecture 1 Example¶

In [19]:
import numpy as np

# Pick an arbitrary 2x2 matrix
A = np.array([[5,3],[2,6]])
print('The matrix is')
print(A)

# Calculate its determinant, eigenvalues, and eigenvectors
print('The determinant of A is',np.linalg.det(A))
eigenvalues, eigenvectors = np.linalg.eig(A)
print('The first eigenvalue is',eigenvalues[0],'and its eigenvector is',eigenvectors[:,0])
print('The second eigenvalue is',eigenvalues[1],'and its eigenvector is',eigenvectors[:,1])
The matrix is
[[5 3]
 [2 6]]
The determine of A is 23.999999999999993
The first eigenvalue is 3.0 and its eigenvector is [-0.83205029  0.5547002 ]
The second eigenvalue is 8.0 and its eigenvector is [-0.70710678 -0.70710678]
In [14]:
import matplotlib.pyplot as plt

# Plot a unit circle in the x-space
thetas = np.linspace(0,2*np.pi,100)
X = np.vstack((np.cos(thetas),np.sin(thetas)))

fig, ax = plt.subplots(1,1,figsize=(4,4))
ax.plot([-1,1],[0,0],'k--',[-1e-6,1e-6],[-1,1],'k--')
ax.plot(X[0,:],X[1,:],'.')
ax.set_title('Unit Circle made up of %d different x vectors'%(X.shape[1]))
Out[14]:
Text(0.5, 1.0, 'Unit Circle made up of 100 different x vectors')
In [15]:
# Show what happens to those vectors after transformation to the Y space
Y = A @ X

fig, ax = plt.subplots(1,1,figsize=(4,4))
ax.plot([-1,1],[0,0],'k--',[-1e-6,1e-6],[-1,1],'k--')
ax.plot(Y[0,:],Y[1,:],'.')
ax.set_title('Unit Circle made up of %d different y vectors'%(Y.shape[1]))
Out[15]:
Text(0.5, 1.0, 'Unit Circle made up of 100 different y vectors')
In [16]:
# Calculate its determinant, eigenvalues, and eigenvectors
print('The determine of A is',np.linalg.det(A))
eigenvalues, eigenvectors = np.linalg.eig(A)
print('The first eigenvalue is',eigenvalues[0],'and its eigenvector is',eigenvectors[:,0])
print('The second eigenvalue is',eigenvalues[1],'and its eigenvector is',eigenvectors[:,1])
The determine of A is 23.999999999999993
The first eigenvalue is 3.0 and its eigenvector is [-0.83205029  0.5547002 ]
The second eigenvalue is 8.0 and its eigenvector is [-0.70710678 -0.70710678]
In [17]:
# Show that Av = lambda v

print('A times v1 gives',A @ eigenvectors[:,0])
print('lambda1 times v1 gives',eigenvalues[0]*eigenvectors[:,0])
print('A times v2 gives',A @ eigenvectors[:,1])
print('lambda2 times v2 gives',eigenvalues[1]*eigenvectors[:,1])
A times v1 gives [-2.49615088  1.66410059]
lambda1 times v1 gives [-2.49615088  1.66410059]
A times v2 gives [-5.65685425 -5.65685425]
lambda2 times v2 gives [-5.65685425 -5.65685425]
In [ ]: