Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sympy as sp
- import numpy as np
- import matplotlib.pyplot as plt
- #Defining the matrix
- A3 = sp.Matrix([[(1/11),(13/28),(0),(0),(14/22)],[(15/22),(1/4),(7/30),(0),(0)],
- [(0),(8/28),(1/6),(10/34),(0)],[(0),(0),(18/30),(1/34),(5/22)],[(5/22),(0),(0),(23/34),(3/22)]])
- #Finding the eigenvalues and eigenvectors
- #note that Python crashes due to the imaginary eigenvectors.
- A3.eigenvals()
- A3.eigenvects()
- #Define the population distribution vectors and multiply by a large power of the matrix.
- v = (sp.Matrix([[0.25,0.15,0.2,0.2,0.2]])).transpose()
- u = (sp.Matrix([[0.1,0.2,0.1,0.5,0.1]])).transpose()
- k = (sp.Matrix([[0.2,0.1,0.3,0.4,0]])).transpose()
- print((A3**10000)*v)
- print((A3**10000)*u)
- print((A3**10000)*k)
- """
- Question 2
- """
- #Define the matrix, apply the Gram Schmidt algorithm, and then define Q according to the values output by the Gram Schmidt algorithm.
- #Then, find R.
- A1 = sp.Matrix([[2,3],[0,-2]])
- gsA1=[sp.Matrix([[2,0]]),sp.Matrix([[3,-2]])]
- print(sp.GramSchmidt(gsA1))
- Q1 = sp.Matrix([[2,0],[0,-2]])
- R1=(Q1.transpose())*A1
- (R1)
- A2 = sp.Matrix([[1,1,0],[1,0,1],[0,1,1]])
- gsA2=[sp.Matrix([[1,1,0]]),sp.Matrix([[1,0,1]]),sp.Matrix([[0,1,1]])]
- print(sp.GramSchmidt(gsA2))
- Q2 = sp.Matrix([[1,(1/2),(-2/3)],[1,(-1/2),(2/3)],[0,1,(2/3)]])
- R2=(Q2.transpose())*A2
- (R2)
- #Code for creating the arrow diagram.
- V = np.array([[1,0],[0,1],[4,3]])
- origin = [0], [0] # origin point
- plt.quiver(*origin, V[:,0], V[:,1], color=['r','b','g'], scale=10)
- plt.show()
- #Using python to generate the column spaces for the last part.
- print("A1:",A1.columnspace())
- print("Q1:",Q1.columnspace())
- print("Q1 Inverse:", (sp.Matrix([[2,0],[0,-2]])).transpose())
- print("A2:",A2.columnspace())
- print("Q2:",Q2.transpose())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement