Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. import numpy as np
  2. a = np.array([[1, -5],
  3. [7, -1]], float)
  4. b = np.array([-4, 6], float)
  5.  
  6. (n,) = np.shape(b)
  7. x = np.full(n, 0, float)
  8. #xnew = np.full(n, 0, float)
  9. #xnew = np.empty(n, float)
  10. xnew = x.copy()
  11. iterlimit = 100
  12. epsilon = 0.001
  13.  
  14. #for j in range(n): #memberi nilai 0 x1, x2, xn pada iterasi ke 0
  15. # x[j] = 0
  16.  
  17. for iteration in range(iterlimit):
  18. for i in range(n):
  19. s = 0
  20. for j in range(n):
  21. if j != i:
  22. s += (-a[i, j]) * x[j]
  23. xnew[i] = 1 / a[i, i] * (s + b[i])
  24.  
  25. print("Iteration ke - %d" % (iteration))
  26. print(x)
  27. print("")
  28.  
  29. if(abs(xnew - x) <= epsilon).all():
  30. iteration = iteration + 1
  31. print("Iteration ke - %d" % iteration)
  32. x = np.copy(xnew)
  33. print(x)
  34. print("")
  35. break
  36. else:
  37. x = np.copy(xnew)
  38.  
  39. print("The solution: ")
  40. print(x)
  41. print("Number of iteration: %d" % (iteration))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement