Advertisement
mirosh111000

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

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