Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. import numpy as np
  2.  
  3. A = np.array([[7, 1, 2], [1, 8, 3], [2, 3, 9]])
  4. b = np.array([10, 8, 6])
  5.  
  6. LU = np.zeros(A.shape)
  7. D = np.zeros(A.shape)
  8. for y in range(A.shape[1]):
  9. for x in range(A.shape[0]):
  10. if x == y:
  11. D[x][y] = A[x][y]
  12. else:
  13. LU[x][y] = A[x][y]
  14.  
  15. x = np.array([1000, 2000, 3000])
  16.  
  17. for n in range(100):
  18. # 1. calculate error
  19. e = np.linalg.norm(b - A @ x)
  20. print(n, x, e)
  21.  
  22. # 2. update
  23. x = np.linalg.inv(D) @ (b - LU @ x)
  24.  
  25. e = np.linalg.norm(b - A @ x)
  26. print("answer is %s (error = %s)" % (x, e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement