Advertisement
mirosh111000

2.23

Mar 18th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.32 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 = 16
  16.  
  17. x1 = np.linspace(-100, 100, 1000)
  18.  
  19. plt.figure(figsize=(10, 5))
  20. plt.title(r'$L = 3x_1 + 3x_2 \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 2 - x1
  30.  
  31. x2 = f1(x1)
  32. print(f'({round(random_x1_f1, 2)}) + ({round(random_x2_f1, 2)}) > 2')
  33. print(f'{round(random_x1_f1 + random_x2_f1, 2)} > 2')
  34. print(f'{random_x1_f1 + random_x2_f1 > 2}\n')
  35. if random_x1_f1 + random_x2_f1 > 2:
  36.     upper = 1
  37. else:
  38.     if random_x2_f1 < f1(random_x1_f1):
  39.         upper = 1
  40.     else:
  41.         upper = 0
  42. plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
  43. plot(x1, x2, np.array([0, 2]), np.array([2, 0]), colors[0], label=r'$x_1 + x_2 > 2$', 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 + 3x_2 \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 2 - x1
  68.  
  69. x2 = f1(x1)
  70. print(f'({round(random_x1_f1, 2)}) + ({round(random_x2_f1, 2)}) > 2')
  71. print(f'{round(random_x1_f1 + random_x2_f1, 2)} > 2')
  72. print(f'{random_x1_f1 + random_x2_f1 > 2}\n')
  73. if random_x1_f1 + random_x2_f1 > 2:
  74.     upper = 1
  75. else:
  76.     if random_x2_f1 < f1(random_x1_f1):
  77.         upper = 1
  78.     else:
  79.         upper = 0
  80. plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
  81. plot(x1, x2, np.array([0, 2]), np.array([2, 0]), colors[0], label=r'$x_1 + x_2 > 2$', 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 (14 - x1) / 7
  89. x2 = f2(x1)
  90. print(f'({round(random_x1_f2, 2)}) + (7*{round(random_x2_f2, 2)}) < 14')
  91. print(f'{round(random_x1_f2 + 7*random_x2_f2, 2)} < 14')
  92. print(f'{random_x1_f2 + 7*random_x2_f2 < 14}\n')
  93. if random_x1_f2 + 7*random_x2_f2 < 14:
  94.     upper = 0
  95. else:
  96.     if random_x2_f2 > f2(random_x1_f2):
  97.         upper = 0
  98.     else:
  99.         upper = 1
  100. plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
  101. plot(x1, x2, np.array([0, 14]), np.array([2, 0]), colors[1], label=r'$x_1 + 7x_2 < 14$', 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.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. plt.figure(figsize=(10, 5))
  120. plt.title(r'$L = 3x_1 + 3x_2 \rightarrow max$')
  121.  
  122. plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
  123. plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
  124.  
  125.  
  126. print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
  127. def f1(x1):
  128.     return 2 - x1
  129.  
  130. x2 = f1(x1)
  131. print(f'({round(random_x1_f1, 2)}) + ({round(random_x2_f1, 2)}) > 2')
  132. print(f'{round(random_x1_f1 + random_x2_f1, 2)} > 2')
  133. print(f'{random_x1_f1 + random_x2_f1 > 2}\n')
  134. if random_x1_f1 + random_x2_f1 > 2:
  135.     upper = 1
  136. else:
  137.     if random_x2_f1 < f1(random_x1_f1):
  138.         upper = 1
  139.     else:
  140.         upper = 0
  141. plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
  142. plot(x1, x2, np.array([0, 2]), np.array([2, 0]), colors[0], label=r'$x_1 + x_2 > 2$', upper=upper)
  143.  
  144.  
  145. print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
  146. def f2(x1):
  147.     return (14 - x1) / 7
  148. x2 = f2(x1)
  149. print(f'({round(random_x1_f2, 2)}) + (7*{round(random_x2_f2, 2)}) < 14')
  150. print(f'{round(random_x1_f2 + 7*random_x2_f2, 2)} < 14')
  151. print(f'{random_x1_f2 + 7*random_x2_f2 < 14}\n')
  152. if random_x1_f2 + 7*random_x2_f2 < 14:
  153.     upper = 0
  154. else:
  155.     if random_x2_f2 > f2(random_x1_f2):
  156.         upper = 0
  157.     else:
  158.         upper = 1
  159. plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
  160. plot(x1, x2, np.array([0, 14]), np.array([2, 0]), colors[1], label=r'$x_1 + 7x_2 < 14$', upper=upper)
  161.  
  162. random_x1_f3 = random.uniform(-2, scale)
  163. random_x2_f3 = random.uniform(-2, scale)
  164. print(f"Пробна точка: ({round(random_x1_f3, 2)}, {round(random_x2_f3, 2)})")
  165. def f3(x1):
  166.     return (x1 - 2) / 5
  167. x2 = f3(x1)
  168. print(f'({round(random_x1_f3, 2)}) - (5*{round(random_x2_f3, 2)}) < 2')
  169. print(f'{round(random_x1_f3 - 5*random_x2_f3, 2)} < 2')
  170. print(f'{random_x1_f3 - 5*random_x2_f3 < 2}\n')
  171. if random_x1_f3 - 5*random_x2_f3 < 2:
  172.     upper = 1
  173. else:
  174.     if random_x2_f3 > f2(random_x1_f3):
  175.         upper = 1
  176.     else:
  177.         upper = 0
  178. plt.scatter(random_x1_f3, random_x2_f3, c=colors[2])
  179. plot(x1, x2, np.array([2, 7]), np.array([0, 1]), colors[2], label=r'$x_1 - 5x_2 < 2$', upper=upper)
  180.  
  181.  
  182. plt.grid()
  183. plt.legend()
  184. plt.xlim(-3, scale)
  185. plt.ylim(-3, scale)
  186. plt.show()
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195. plt.figure(figsize=(10, 5))
  196. plt.title(r'$L = 3x_1 + 3x_2 \rightarrow max$')
  197. plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
  198. plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
  199.  
  200.  
  201. print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
  202. def f1(x1):
  203.     return 2 - x1
  204.  
  205. x2 = f1(x1)
  206. print(f'({round(random_x1_f1, 2)}) + ({round(random_x2_f1, 2)}) > 2')
  207. print(f'{round(random_x1_f1 + random_x2_f1, 2)} > 2')
  208. print(f'{random_x1_f1 + random_x2_f1 > 2}\n')
  209. if random_x1_f1 + random_x2_f1 > 2:
  210.     upper = 1
  211. else:
  212.     if random_x2_f1 < f1(random_x1_f1):
  213.         upper = 1
  214.     else:
  215.         upper = 0
  216. plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
  217. plot(x1, x2, np.array([0, 2]), np.array([2, 0]), colors[0], label=r'$x_1 + x_2 > 2$', upper=upper)
  218.  
  219.  
  220. print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
  221. def f2(x1):
  222.     return (14 - x1) / 7
  223. x2 = f2(x1)
  224. print(f'({round(random_x1_f2, 2)}) + (7*{round(random_x2_f2, 2)}) < 14')
  225. print(f'{round(random_x1_f2 + 7*random_x2_f2, 2)} < 14')
  226. print(f'{random_x1_f2 + 7*random_x2_f2 < 14}\n')
  227. if random_x1_f2 + 7*random_x2_f2 < 14:
  228.     upper = 0
  229. else:
  230.     if random_x2_f2 > f2(random_x1_f2):
  231.         upper = 0
  232.     else:
  233.         upper = 1
  234. plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
  235. plot(x1, x2, np.array([0, 14]), np.array([2, 0]), colors[1], label=r'$x_1 + 7x_2 < 14$', upper=upper)
  236.  
  237. print(f"Пробна точка: ({round(random_x1_f3, 2)}, {round(random_x2_f3, 2)})")
  238. def f3(x1):
  239.     return (x1 - 2) / 5
  240. x2 = f3(x1)
  241. print(f'({round(random_x1_f3, 2)}) - (5*{round(random_x2_f3, 2)}) < 2')
  242. print(f'{round(random_x1_f3 - 5*random_x2_f3, 2)} < 2')
  243. print(f'{random_x1_f3 - 5*random_x2_f3 < 2}\n')
  244. if random_x1_f3 - 5*random_x2_f3 < 2:
  245.     upper = 1
  246. else:
  247.     if random_x2_f3 > f2(random_x1_f3):
  248.         upper = 1
  249.     else:
  250.         upper = 0
  251. plt.scatter(random_x1_f3, random_x2_f3, c=colors[2])
  252. plot(x1, x2, np.array([2, 7]), np.array([0, 1]), colors[2], label=r'$x_1 - 5x_2 < 2$', upper=upper)
  253.  
  254. def f(x1):
  255.     return -3*x1 / 3
  256.  
  257. x2 = f(x1)
  258. plt.plot(x1, x2, c='black', lw=2)
  259. plt.scatter(np.array([0, 1]), np.array([0, -1]), c='black')
  260. plt.annotate('Вектор нормалі', xy=(3, 3), xycoords='data', xytext=(-1.1, -2.4), textcoords='data', arrowprops=dict(facecolor='black'))
  261. plt.text(7, 1, f'C - max', fontsize=10, ha='right')
  262. plt.text(0, 2, f'A', fontsize=10, ha='right')
  263. plt.text(2, 0, f'B', fontsize=10, ha='right')
  264. plt.plot(np.array([0, 2]), np.array([2, 0]), c='white', label=r'$min(AB); L(min)=6$')
  265. plt.scatter(7, 1, c='red', edgecolors='black', s=50, label=r'$max(7, 1); L(max)=24$')
  266. plt.grid()
  267. plt.legend()
  268. plt.xlim(-3, scale)
  269. plt.ylim(-3, scale)
  270. plt.show()
  271.  
  272.  
  273.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement