Advertisement
mirosh111000

2.27

Mar 18th, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.27 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import random
  4.  
  5. def plot(x1, x2, point_x1, point_x2, color='blue', label=r'$x_1 + 3x_2 ≥ 3$', upper=1):
  6.     plt.plot(x1, x2, c=color)
  7.     plt.scatter(point_x1, point_x2, marker='o', c=color)
  8.     if upper == 1:
  9.         plt.fill_between(x1, x2, max(x2), color=color, alpha=0.15, label=label)
  10.     else:
  11.         plt.fill_between(x1, x2, min(x2), color=color, alpha=0.15, label=label)
  12.  
  13. colors = ['blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'black', 'white', 'orange', 'purple', 'pink', 'brown', 'gray']
  14.  
  15. scale = 10
  16.  
  17. x1 = np.linspace(-100, 100, 1000)
  18.  
  19. plt.figure(figsize=(10, 5))
  20. plt.title(r'$L = 3x_1 + 0x_2 + 4\rightarrow max$')
  21.  
  22. plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
  23. plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
  24.  
  25. random_x1_f1 = random.uniform(-2, scale)
  26. random_x2_f1 = random.uniform(-2, scale)
  27. print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
  28. def f1(x1):
  29.     return 4 + 2*x1
  30.  
  31. x2 = f1(x1)
  32. print(f'(-2*{round(random_x1_f1, 2)}) + ({round(random_x2_f1, 2)}) < 4')
  33. print(f'{round(-2*random_x1_f1 + random_x2_f1, 2)} < 4')
  34. print(f'{-2*random_x1_f1 + random_x2_f1 < 4}\n')
  35. if -2*random_x1_f1 + random_x2_f1 < 4:
  36.     upper = 0
  37. else:
  38.     if random_x2_f1 > f1(random_x1_f1):
  39.         upper = 0
  40.     else:
  41.         upper = 1
  42. plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
  43. plot(x1, x2, np.array([0, 2]), np.array([4, 8]), colors[0], label=r'$-2x_1 + x_2 < 4$', upper=upper)
  44.  
  45. plt.grid()
  46. plt.legend()
  47. plt.xlim(-3, scale)
  48. plt.ylim(-3, scale)
  49. plt.show()
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. plt.figure(figsize=(10, 5))
  60. plt.title(r'$L = 3x_1 + 0x_2 + 4\rightarrow max$')
  61.  
  62. plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
  63. plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
  64.  
  65. print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
  66. def f1(x1):
  67.     return 4 + 2*x1
  68.  
  69. x2 = f1(x1)
  70. print(f'(-2*{round(random_x1_f1, 2)}) + ({round(random_x2_f1, 2)}) < 4')
  71. print(f'{round(-2*random_x1_f1 + random_x2_f1, 2)} < 4')
  72. print(f'{-2*random_x1_f1 + random_x2_f1 < 4}\n')
  73. if -2*random_x1_f1 + random_x2_f1 < 4:
  74.     upper = 0
  75. else:
  76.     if random_x2_f1 > f1(random_x1_f1):
  77.         upper = 0
  78.     else:
  79.         upper = 1
  80. plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
  81. plot(x1, x2, np.array([0, 2]), np.array([4, 8]), colors[0], label=r'$-2x_1 + x_2 < 4$', upper=upper)
  82.  
  83.  
  84. random_x1_f2 = random.uniform(-2, scale)
  85. random_x2_f2 = random.uniform(-2, scale)
  86. print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
  87. def f2(x1):
  88.     return (-1 + x1) / 2
  89. x2 = f2(x1)
  90. print(f'(-{round(random_x1_f2, 2)}) + (2*{round(random_x2_f2, 2)}) > -1')
  91. print(f'{round(-random_x1_f2 + 2*random_x2_f2, 2)} > -1')
  92. print(f'{-random_x1_f2 + 2*random_x2_f2 > -1}\n')
  93. if -random_x1_f2 + 2*random_x2_f2 > -1:
  94.     upper = 1
  95. else:
  96.     if random_x2_f2 < f2(random_x1_f2):
  97.         upper = 1
  98.     else:
  99.         upper = 0
  100. plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
  101. plot(x1, x2, np.array([1, 3]), np.array([0, 1]), colors[1], label=r'$-x_1 + 2x_2 > -1$', upper=upper)
  102.  
  103. plt.grid()
  104. plt.legend()
  105. plt.xlim(-3, scale)
  106. plt.ylim(-3, scale)
  107. plt.show()
  108.  
  109.  
  110.  
  111.  
  112.  
  113. plt.figure(figsize=(10, 5))
  114. plt.title(r'$L = 3x_1 + 0x_2 + 4\rightarrow max$')
  115. plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
  116. plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
  117.  
  118.  
  119. print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
  120. def f1(x1):
  121.     return 4 + 2*x1
  122.  
  123. x2 = f1(x1)
  124. print(f'(-2*{round(random_x1_f1, 2)}) + ({round(random_x2_f1, 2)}) < 4')
  125. print(f'{round(-2*random_x1_f1 + random_x2_f1, 2)} < 4')
  126. print(f'{-2*random_x1_f1 + random_x2_f1 < 4}\n')
  127. if -2*random_x1_f1 + random_x2_f1 < 4:
  128.     upper = 0
  129. else:
  130.     if random_x2_f1 > f1(random_x1_f1):
  131.         upper = 0
  132.     else:
  133.         upper = 1
  134. plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
  135. plot(x1, x2, np.array([0, 2]), np.array([4, 8]), colors[0], label=r'$-2x_1 + x_2 < 4$', upper=upper)
  136.  
  137.  
  138. print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
  139. def f2(x1):
  140.     return (-1 + x1) / 2
  141. x2 = f2(x1)
  142. print(f'(-{round(random_x1_f2, 2)}) + (2*{round(random_x2_f2, 2)}) > -1')
  143. print(f'{round(-random_x1_f2 + 2*random_x2_f2, 2)} > -1')
  144. print(f'{-random_x1_f2 + 2*random_x2_f2 > -1}\n')
  145. if -random_x1_f2 + 2*random_x2_f2 > -1:
  146.     upper = 1
  147. else:
  148.     if random_x2_f2 < f2(random_x1_f2):
  149.         upper = 1
  150.     else:
  151.         upper = 0
  152. plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
  153. plot(x1, x2, np.array([1, 3]), np.array([0, 1]), colors[1], label=r'$-x_1 + 2x_2 > -1$', upper=upper)
  154.  
  155.  
  156.  
  157.  
  158. x2 = np.copy(x1)
  159. plt.axvline(x=-4/3, lw=2, color='black')
  160. plt.scatter(np.array([-4/3, -4/3]), np.array([4, 0]), c='black')
  161.  
  162. plt.annotate('Вектор нормалі', xy=(3, 0), xycoords='data', xytext=(-3.3, 0.1), textcoords='data', arrowprops=dict(facecolor='black'))
  163.  
  164. plt.scatter(100, 1000, c='red', edgecolors='black', s=50, label=r'$L_{max} \rightarrow \infty$')
  165. plt.grid()
  166. plt.legend()
  167. plt.xlim(-3, scale)
  168. plt.ylim(-3, scale)
  169. plt.show()
  170.  
  171.  
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement