Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. A = np.array([[2,-1,0,0,0,0,0,0,0,0],
  2.               [-1,2,-1,0,0,0,0,0,0,0],
  3.               [0,-1,2,-1,0,0,0,0,0,0],
  4.               [0,0,-1,2,-1,0,0,0,0,0],
  5.               [0,0,0,-1,2,-1,0,0,0,0],
  6.               [0,0,0,0,-1,2,-1,0,0,0],
  7.               [0,0,0,0,0,-1,2,-1,0,0],
  8.               [0,0,0,0,0,0,-1,2,-1,0],
  9.               [0,0,0,0,0,0,0,-1,2,-1],
  10.               [0,0,0,0,0,0,0,0,-1,2]])
  11.  
  12.  
  13. B = np.array([[1,0,0,0,0,0,0,0,0,0],
  14.               [1,1,0,0,0,0,0,0,0,0],
  15.               [1,1,1,0,0,0,0,0,0,0],
  16.               [1,1,1,1,0,0,0,0,0,0],
  17.               [1,1,1,1,1,0,0,0,0,0],
  18.               [1,1,1,1,1,1,0,0,0,0],
  19.               [1,1,1,1,1,1,1,0,0,0],
  20.               [1,1,1,1,1,1,1,1,0,0],
  21.               [1,1,1,1,1,1,1,1,1,0],
  22.               [1,1,1,1,1,1,1,1,1,1]])
  23.  
  24. q = np.array([1,1,1,1,1,1,1,1,1,1]).reshape((10,1))
  25. d = np.array([1,2,3,4,5,6,7,8,9,10]).reshape((10,1))
  26.  
  27. def count_x(lam):
  28.     #x = A^-1(q-(lam^T*B)^T)/2
  29.     x = np.linalg.inv(A)
  30.     x = x.dot(q - np.dot(B.T, lam))
  31.     return x / 2
  32.  
  33. def count_lam(x, lam, r):
  34.     #lami = lami + r(fi(x))
  35.     lam = lam + r * (np.dot(B, x) - d)
  36.     lam = np.maximum(np.array([0,0,0,0,0,0,0,0,0,0]).reshape((10,1)), lam)
  37.     return lam
  38.  
  39. def Problem(r=1, eps=0.01):
  40.     x = np.array([0,0,0,0,0,0,0,0,0,0]).reshape((10,1))
  41.     lam = np.array([1,1,1,1,1,1,1,1,1,1]).reshape((10,1))
  42.     lam = count_lam(x, lam, r)
  43.     x_new = count_x(lam)
  44.     #print (x_new)
  45.     #print(x)
  46.     #print(np.linalg.norm(x - x_new))
  47.     while (np.linalg.norm(x - x_new) > eps):
  48.         x = x_new
  49.         lam = count_lam(x, lam, r)
  50.         x_new = count_x(lam)
  51.     return x
  52.  
  53. Problem(r=0.0001, eps=0.0000001)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement