Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import sympy as sp
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. #Defining the matrix
  6.  
  7. A3 = sp.Matrix([[(1/11),(13/28),(0),(0),(14/22)],[(15/22),(1/4),(7/30),(0),(0)],
  8. [(0),(8/28),(1/6),(10/34),(0)],[(0),(0),(18/30),(1/34),(5/22)],[(5/22),(0),(0),(23/34),(3/22)]])
  9. #Finding the eigenvalues and eigenvectors
  10. #note that Python crashes due to the imaginary eigenvectors.
  11. A3.eigenvals()
  12. A3.eigenvects()
  13.  
  14. #Define the population distribution vectors and multiply by a large power of the matrix.
  15. v = (sp.Matrix([[0.25,0.15,0.2,0.2,0.2]])).transpose()
  16. u = (sp.Matrix([[0.1,0.2,0.1,0.5,0.1]])).transpose()
  17. k = (sp.Matrix([[0.2,0.1,0.3,0.4,0]])).transpose()
  18.  
  19. print((A3**10000)*v)
  20. print((A3**10000)*u)
  21. print((A3**10000)*k)
  22.  
  23. """
  24. Question 2
  25. """
  26. #Define the matrix, apply the Gram Schmidt algorithm, and then define Q according to the values output by the Gram Schmidt algorithm.
  27. #Then, find R.
  28. A1 = sp.Matrix([[2,3],[0,-2]])
  29.  
  30. gsA1=[sp.Matrix([[2,0]]),sp.Matrix([[3,-2]])]
  31. print(sp.GramSchmidt(gsA1))
  32.  
  33. Q1 = sp.Matrix([[2,0],[0,-2]])
  34. R1=(Q1.transpose())*A1
  35. (R1)
  36.  
  37. A2 = sp.Matrix([[1,1,0],[1,0,1],[0,1,1]])
  38.  
  39. gsA2=[sp.Matrix([[1,1,0]]),sp.Matrix([[1,0,1]]),sp.Matrix([[0,1,1]])]
  40. print(sp.GramSchmidt(gsA2))
  41.  
  42. Q2 = sp.Matrix([[1,(1/2),(-2/3)],[1,(-1/2),(2/3)],[0,1,(2/3)]])
  43. R2=(Q2.transpose())*A2
  44. (R2)
  45.  
  46. #Code for creating the arrow diagram.
  47.  
  48. V = np.array([[1,0],[0,1],[4,3]])
  49. origin = [0], [0] # origin point
  50.  
  51. plt.quiver(*origin, V[:,0], V[:,1], color=['r','b','g'], scale=10)
  52. plt.show()
  53.  
  54. #Using python to generate the column spaces for the last part.
  55.  
  56. print("A1:",A1.columnspace())
  57. print("Q1:",Q1.columnspace())
  58. print("Q1 Inverse:", (sp.Matrix([[2,0],[0,-2]])).transpose())
  59. print("A2:",A2.columnspace())
  60. print("Q2:",Q2.transpose())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement