Advertisement
mirosh111000

Графічний метод

Mar 7th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.11 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.  
  16. print(" / x1 + 3*x2 ≥ 3\n/ x1 + x2 ≥ 5\n\ x1 ≤ 4\n \ -2*x1 + x2 ≥ 2\nx1, x2 ≥ 0\n")
  17.  
  18. x1 = np.linspace(-100, 100, 1000)
  19.  
  20. plt.figure(figsize=(10, 5))
  21. plt.title(r'$L = x_1 - 3x_2 \rightarrow max(min)$')
  22.  
  23. plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
  24. plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
  25.  
  26. random_x1_f1 = random.uniform(-2, 10)
  27. random_x2_f1 = random.uniform(-2, 10)
  28. print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
  29. def f1(x1):
  30. return (3 - x1) / 3
  31.  
  32. x2 = f1(x1)
  33. print(f'({round(random_x1_f1, 2)}) + (3*{round(random_x2_f1, 2)}) > 3')
  34. print(f'{round(random_x1_f1 + 3*random_x2_f1, 2)} > 3')
  35. print(f'{random_x1_f1 + 3*random_x2_f1 > 3}\n')
  36. if random_x1_f1 + 3*random_x2_f1 > 3:
  37. upper = 1
  38. else:
  39. if random_x2_f1 < f1(random_x1_f1):
  40. upper = 1
  41. else:
  42. upper = 0
  43. plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
  44. plot(x1, x2, np.array([0, 3]), np.array([1, 0]), colors[0], label=r'$x_1 + 3x_2 > 3$', upper=upper)
  45.  
  46. plt.grid()
  47. plt.legend()
  48. plt.xlim(-3, 10)
  49. plt.ylim(-3, 10)
  50. plt.show()
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. plt.figure(figsize=(10, 5))
  61. plt.title(r'$L = x_1 - 3x_2 \rightarrow max(min)$')
  62.  
  63. plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
  64. plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
  65.  
  66. print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
  67. def f1(x1):
  68. return (3 - x1) / 3
  69.  
  70. x2 = f1(x1)
  71. print(f'({round(random_x1_f1, 2)}) + (3*{round(random_x2_f1, 2)}) > 3')
  72. print(f'{round(random_x1_f1 + 3*random_x2_f1, 2)} > 3')
  73. print(f'{random_x1_f1 + 3*random_x2_f1 > 3}\n')
  74. if random_x1_f1 + 3*random_x2_f1 > 3:
  75. upper = 1
  76. else:
  77. if random_x2_f1 < f1(random_x1_f1):
  78. upper = 1
  79. else:
  80. upper = 0
  81. plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
  82. plot(x1, x2, np.array([0, 3]), np.array([1, 0]), colors[0], label=r'$x_1 + 3x_2 > 3$', upper=upper)
  83.  
  84. random_x1_f2 = random.uniform(-2, 10)
  85. random_x2_f2 = random.uniform(-2, 10)
  86. print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
  87. def f2(x1):
  88. return 5 - x1
  89. x2 = f2(x1)
  90. print(f'({round(random_x1_f2, 2)}) + ({round(random_x2_f2, 2)}) > 5')
  91. print(f'{round(random_x1_f2 + random_x2_f2, 2)} > 5')
  92. print(f'{random_x1_f2 + random_x2_f2 > 5}\n')
  93. if random_x1_f2 + random_x2_f2 > 5:
  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([0, 5]), np.array([5, 0]), colors[1], label=r'$x_1 + x_2 > 5$', upper=upper)
  102.  
  103. plt.grid()
  104. plt.legend()
  105. plt.xlim(-3, 10)
  106. plt.ylim(-3, 10)
  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 = x_1 - 3x_2 \rightarrow max(min)$')
  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 (3 - x1) / 3
  129.  
  130. x2 = f1(x1)
  131. print(f'({round(random_x1_f1, 2)}) + (3*{round(random_x2_f1, 2)}) > 3')
  132. print(f'{round(random_x1_f1 + 3*random_x2_f1, 2)} > 3')
  133. print(f'{random_x1_f1 + 3*random_x2_f1 > 3}\n')
  134. if random_x1_f1 + 3*random_x2_f1 > 3:
  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, 3]), np.array([1, 0]), colors[0], label=r'$x_1 + 3x_2 > 3$', upper=upper)
  143.  
  144.  
  145. print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
  146. def f2(x1):
  147. return 5 - x1
  148. x2 = f2(x1)
  149. print(f'({round(random_x1_f2, 2)}) + ({round(random_x2_f2, 2)}) > 5')
  150. print(f'{round(random_x1_f2 + random_x2_f2, 2)} > 5')
  151. print(f'{random_x1_f2 + random_x2_f2 > 5}\n')
  152. if random_x1_f2 + random_x2_f2 > 5:
  153. upper = 1
  154. else:
  155. if random_x2_f2 < f2(random_x1_f2):
  156. upper = 1
  157. else:
  158. upper = 0
  159. plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
  160. plot(x1, x2, np.array([0, 5]), np.array([5, 0]), colors[1], label=r'$x_1 + x_2 > 5$', upper=upper)
  161.  
  162. random_x1_f3 = random.uniform(-2, 10)
  163. random_x2_f3 = random.uniform(-2, 10)
  164. print(f"Пробна точка: ({round(random_x1_f3, 2)}, {round(random_x2_f3, 2)})")
  165. print(f'{round(random_x1_f3, 2)} < 4')
  166. print(f'{random_x1_f3 < 4}\n')
  167.  
  168. x2 = np.copy(x1)
  169. plt.axvline(x=4, color=colors[2])
  170. plt.scatter(np.array([4, 4, random_x1_f3]), np.array([4, 0, random_x1_f3]), c=colors[2])
  171. plt.fill_betweenx([min(x2), max(x2)], min(x1), 4, color=colors[2], alpha=0.15, label=r'$x_1 < 4$')
  172.  
  173.  
  174. plt.grid()
  175. plt.legend()
  176. plt.xlim(-3, 10)
  177. plt.ylim(-3, 10)
  178. plt.show()
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187. plt.figure(figsize=(10, 5))
  188. plt.title(r'$L = x_1 - 3x_2 \rightarrow max(min)$')
  189.  
  190. plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
  191. plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
  192.  
  193.  
  194. print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
  195. def f1(x1):
  196. return (3 - x1) / 3
  197.  
  198. x2 = f1(x1)
  199. print(f'({round(random_x1_f1, 2)}) + (3*{round(random_x2_f1, 2)}) > 3')
  200. print(f'{round(random_x1_f1 + 3*random_x2_f1, 2)} > 3')
  201. print(f'{random_x1_f1 + 3*random_x2_f1 > 3}\n')
  202. if random_x1_f1 + 3*random_x2_f1 > 3:
  203. upper = 1
  204. else:
  205. if random_x2_f1 < f1(random_x1_f1):
  206. upper = 1
  207. else:
  208. upper = 0
  209. plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
  210. plot(x1, x2, np.array([0, 3]), np.array([1, 0]), colors[0], label=r'$x_1 + 3x_2 > 3$', upper=upper)
  211.  
  212.  
  213. print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
  214. def f2(x1):
  215. return 5 - x1
  216. x2 = f2(x1)
  217. print(f'({round(random_x1_f2, 2)}) + ({round(random_x2_f2, 2)}) > 5')
  218. print(f'{round(random_x1_f2 + random_x2_f2, 2)} > 5')
  219. print(f'{random_x1_f2 + random_x2_f2 > 5}\n')
  220. if random_x1_f2 + random_x2_f2 > 5:
  221. upper = 1
  222. else:
  223. if random_x2_f2 < f2(random_x1_f2):
  224. upper = 1
  225. else:
  226. upper = 0
  227. plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
  228. plot(x1, x2, np.array([0, 5]), np.array([5, 0]), colors[1], label=r'$x_1 + x_2 > 5$', upper=upper)
  229.  
  230. print(f"Пробна точка: ({round(random_x1_f3, 2)}, {round(random_x2_f3, 2)})")
  231. print(f'{round(random_x1_f3, 2)} < 4')
  232. print(f'{random_x1_f3 < 4}\n')
  233.  
  234. x2 = np.copy(x1)
  235. plt.axvline(x=4, color=colors[2])
  236. plt.scatter(np.array([4, 4, random_x1_f3]), np.array([4, 0, random_x1_f3]), c=colors[2])
  237. plt.fill_betweenx([min(x2), max(x2)], min(x1), 4, color=colors[2], alpha=0.15, label=r'$x_1 < 4$')
  238.  
  239.  
  240.  
  241. random_x1_f4 = random.uniform(-2, 10)
  242. random_x2_f4 = random.uniform(-2, 10)
  243. print(f"Пробна точка: ({round(random_x1_f4, 2)}, {round(random_x2_f4, 2)})")
  244. def f4(x1):
  245. return 2 + 2*x1
  246. x2 = f4(x1)
  247. print(f'(-2*{round(random_x1_f4, 2)}) + ({round(random_x2_f4, 2)}) > 2')
  248. print(f'{round(-2*random_x1_f4 + random_x2_f4, 2)} > 2')
  249. print(f'{-2*random_x1_f4 + random_x2_f4 > 2}\n')
  250. if -2*random_x1_f4 + random_x2_f4 > 2:
  251. upper = 1
  252. else:
  253. if random_x2_f4 < f4(random_x1_f4):
  254. upper = 1
  255. else:
  256. upper = 0
  257. plt.scatter(random_x1_f4, random_x2_f4, c=colors[5])
  258. plot(x1, x2, np.array([0, -1]), np.array([2, 0]), colors[5], label=r'$-2x_1 + x_2 > 2$', upper=upper)
  259.  
  260. plt.grid()
  261. plt.legend()
  262. plt.xlim(-3, 10)
  263. plt.ylim(-3, 10)
  264. plt.show()
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273. plt.figure(figsize=(10, 5))
  274. plt.title(r'$L = x_1 - 3x_2 \rightarrow max(min)$')
  275. plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
  276. plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
  277.  
  278.  
  279. print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
  280. def f1(x1):
  281. return (3 - x1) / 3
  282.  
  283. x2 = f1(x1)
  284. print(f'({round(random_x1_f1, 2)}) + (3*{round(random_x2_f1, 2)}) > 3')
  285. print(f'{round(random_x1_f1 + 3*random_x2_f1, 2)} > 3')
  286. print(f'{random_x1_f1 + 3*random_x2_f1 > 3}\n')
  287. if random_x1_f1 + 3*random_x2_f1 > 3:
  288. upper = 1
  289. else:
  290. if random_x2_f1 < f1(random_x1_f1):
  291. upper = 1
  292. else:
  293. upper = 0
  294. plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
  295. plot(x1, x2, np.array([0, 3]), np.array([1, 0]), colors[0], label=r'$x_1 + 3x_2 > 3$', upper=upper)
  296.  
  297.  
  298. print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
  299. def f2(x1):
  300. return 5 - x1
  301. x2 = f2(x1)
  302. print(f'({round(random_x1_f2, 2)}) + ({round(random_x2_f2, 2)}) > 5')
  303. print(f'{round(random_x1_f2 + random_x2_f2, 2)} > 5')
  304. print(f'{random_x1_f2 + random_x2_f2 > 5}\n')
  305. if random_x1_f2 + random_x2_f2 > 5:
  306. upper = 1
  307. else:
  308. if random_x2_f2 < f2(random_x1_f2):
  309. upper = 1
  310. else:
  311. upper = 0
  312. plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
  313. plot(x1, x2, np.array([0, 5]), np.array([5, 0]), colors[1], label=r'$x_1 + x_2 > 5$', upper=upper)
  314.  
  315.  
  316.  
  317. print(f"Пробна точка: ({round(random_x1_f3, 2)}, {round(random_x2_f3, 2)})")
  318. print(f'{round(random_x1_f3, 2)} < 4')
  319. print(f'{random_x1_f3 < 4}\n')
  320.  
  321. x2 = np.copy(x1)
  322. plt.axvline(x=4, color=colors[2])
  323. plt.scatter(np.array([4, 4, random_x1_f3]), np.array([4, 0, random_x1_f3]), c=colors[2])
  324. plt.fill_betweenx([min(x2), max(x2)], min(x1), 4, color=colors[2], alpha=0.15, label=r'$x_1 < 4$')
  325.  
  326.  
  327.  
  328. print(f"Пробна точка: ({round(random_x1_f4, 2)}, {round(random_x2_f4, 2)})")
  329. def f4(x1):
  330. return 2 + 2*x1
  331. x2 = f4(x1)
  332. print(f'(-2*{round(random_x1_f4, 2)}) + ({round(random_x2_f4, 2)}) > 2')
  333. print(f'{round(-2*random_x1_f4 + random_x2_f4, 2)} > 2')
  334. print(f'{-2*random_x1_f4 + random_x2_f4 > 2}\n')
  335. if -2*random_x1_f4 + random_x2_f4 > 2:
  336. upper = 1
  337. else:
  338. if random_x2_f4 < f4(random_x1_f4):
  339. upper = 1
  340. else:
  341. upper = 0
  342. plt.scatter(random_x1_f4, random_x2_f4, c=colors[5])
  343. plot(x1, x2, np.array([0, -1]), np.array([2, 0]), colors[5], label=r'$-2x_1 + x_2 > 2$', upper=upper)
  344.  
  345.  
  346. def f(x1):
  347. return x1 / 3
  348.  
  349. x2 = f(x1)
  350. plt.plot(x1, x2, c='black', lw=2)
  351. plt.scatter(np.array([0, 6]), np.array([0, 2]), c='black')
  352. plt.annotate('Вектор нормалі', xy=(1, -3), xycoords='data', xytext=(-1.2, 0.4), textcoords='data', arrowprops=dict(facecolor='black'))
  353. plt.text(1, 4, f'max', fontsize=10, ha='right')
  354. plt.scatter(1, 4, c='white', s=1, label=r'$L_{min} \rightarrow - \infty$')
  355. plt.scatter(1, 4, c='red', edgecolors='black', s=50, label=r'$max(1, 4); L(max)=-11$')
  356. plt.grid()
  357. plt.legend()
  358. plt.xlim(-3, 10)
  359. plt.ylim(-5, 10)
  360. plt.show()
  361.  
  362.  
  363.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement