Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- import random
- def plot(x1, x2, point_x1, point_x2, color='blue', label=r'$x_1 + 3x_2 ≥ 3$', upper=1):
- plt.plot(x1, x2, c=color)
- plt.scatter(point_x1, point_x2, marker='o', c=color)
- if upper == 1:
- plt.fill_between(x1, x2, max(x2), color=color, alpha=0.15, label=label)
- else:
- plt.fill_between(x1, x2, min(x2), color=color, alpha=0.15, label=label)
- colors = ['blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'black', 'white', 'orange', 'purple', 'pink', 'brown', 'gray']
- print(" / x1 + 3*x2 ≥ 3\n/ x1 + x2 ≥ 5\n\ x1 ≤ 4\n \ -2*x1 + x2 ≥ 2\nx1, x2 ≥ 0\n")
- x1 = np.linspace(-100, 100, 1000)
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = x_1 - 3x_2 \rightarrow max(min)$')
- plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
- plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
- random_x1_f1 = random.uniform(-2, 10)
- random_x2_f1 = random.uniform(-2, 10)
- print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
- def f1(x1):
- return (3 - x1) / 3
- x2 = f1(x1)
- print(f'({round(random_x1_f1, 2)}) + (3*{round(random_x2_f1, 2)}) > 3')
- print(f'{round(random_x1_f1 + 3*random_x2_f1, 2)} > 3')
- print(f'{random_x1_f1 + 3*random_x2_f1 > 3}\n')
- if random_x1_f1 + 3*random_x2_f1 > 3:
- upper = 1
- else:
- if random_x2_f1 < f1(random_x1_f1):
- upper = 1
- else:
- upper = 0
- plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
- plot(x1, x2, np.array([0, 3]), np.array([1, 0]), colors[0], label=r'$x_1 + 3x_2 > 3$', upper=upper)
- plt.grid()
- plt.legend()
- plt.xlim(-3, 10)
- plt.ylim(-3, 10)
- plt.show()
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = x_1 - 3x_2 \rightarrow max(min)$')
- plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
- plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
- print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
- def f1(x1):
- return (3 - x1) / 3
- x2 = f1(x1)
- print(f'({round(random_x1_f1, 2)}) + (3*{round(random_x2_f1, 2)}) > 3')
- print(f'{round(random_x1_f1 + 3*random_x2_f1, 2)} > 3')
- print(f'{random_x1_f1 + 3*random_x2_f1 > 3}\n')
- if random_x1_f1 + 3*random_x2_f1 > 3:
- upper = 1
- else:
- if random_x2_f1 < f1(random_x1_f1):
- upper = 1
- else:
- upper = 0
- plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
- plot(x1, x2, np.array([0, 3]), np.array([1, 0]), colors[0], label=r'$x_1 + 3x_2 > 3$', upper=upper)
- random_x1_f2 = random.uniform(-2, 10)
- random_x2_f2 = random.uniform(-2, 10)
- print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
- def f2(x1):
- return 5 - x1
- x2 = f2(x1)
- print(f'({round(random_x1_f2, 2)}) + ({round(random_x2_f2, 2)}) > 5')
- print(f'{round(random_x1_f2 + random_x2_f2, 2)} > 5')
- print(f'{random_x1_f2 + random_x2_f2 > 5}\n')
- if random_x1_f2 + random_x2_f2 > 5:
- upper = 1
- else:
- if random_x2_f2 < f2(random_x1_f2):
- upper = 1
- else:
- upper = 0
- plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
- plot(x1, x2, np.array([0, 5]), np.array([5, 0]), colors[1], label=r'$x_1 + x_2 > 5$', upper=upper)
- plt.grid()
- plt.legend()
- plt.xlim(-3, 10)
- plt.ylim(-3, 10)
- plt.show()
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = x_1 - 3x_2 \rightarrow max(min)$')
- plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
- plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
- print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
- def f1(x1):
- return (3 - x1) / 3
- x2 = f1(x1)
- print(f'({round(random_x1_f1, 2)}) + (3*{round(random_x2_f1, 2)}) > 3')
- print(f'{round(random_x1_f1 + 3*random_x2_f1, 2)} > 3')
- print(f'{random_x1_f1 + 3*random_x2_f1 > 3}\n')
- if random_x1_f1 + 3*random_x2_f1 > 3:
- upper = 1
- else:
- if random_x2_f1 < f1(random_x1_f1):
- upper = 1
- else:
- upper = 0
- plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
- plot(x1, x2, np.array([0, 3]), np.array([1, 0]), colors[0], label=r'$x_1 + 3x_2 > 3$', upper=upper)
- print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
- def f2(x1):
- return 5 - x1
- x2 = f2(x1)
- print(f'({round(random_x1_f2, 2)}) + ({round(random_x2_f2, 2)}) > 5')
- print(f'{round(random_x1_f2 + random_x2_f2, 2)} > 5')
- print(f'{random_x1_f2 + random_x2_f2 > 5}\n')
- if random_x1_f2 + random_x2_f2 > 5:
- upper = 1
- else:
- if random_x2_f2 < f2(random_x1_f2):
- upper = 1
- else:
- upper = 0
- plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
- plot(x1, x2, np.array([0, 5]), np.array([5, 0]), colors[1], label=r'$x_1 + x_2 > 5$', upper=upper)
- random_x1_f3 = random.uniform(-2, 10)
- random_x2_f3 = random.uniform(-2, 10)
- print(f"Пробна точка: ({round(random_x1_f3, 2)}, {round(random_x2_f3, 2)})")
- print(f'{round(random_x1_f3, 2)} < 4')
- print(f'{random_x1_f3 < 4}\n')
- x2 = np.copy(x1)
- plt.axvline(x=4, color=colors[2])
- plt.scatter(np.array([4, 4, random_x1_f3]), np.array([4, 0, random_x1_f3]), c=colors[2])
- plt.fill_betweenx([min(x2), max(x2)], min(x1), 4, color=colors[2], alpha=0.15, label=r'$x_1 < 4$')
- plt.grid()
- plt.legend()
- plt.xlim(-3, 10)
- plt.ylim(-3, 10)
- plt.show()
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = x_1 - 3x_2 \rightarrow max(min)$')
- plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
- plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
- print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
- def f1(x1):
- return (3 - x1) / 3
- x2 = f1(x1)
- print(f'({round(random_x1_f1, 2)}) + (3*{round(random_x2_f1, 2)}) > 3')
- print(f'{round(random_x1_f1 + 3*random_x2_f1, 2)} > 3')
- print(f'{random_x1_f1 + 3*random_x2_f1 > 3}\n')
- if random_x1_f1 + 3*random_x2_f1 > 3:
- upper = 1
- else:
- if random_x2_f1 < f1(random_x1_f1):
- upper = 1
- else:
- upper = 0
- plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
- plot(x1, x2, np.array([0, 3]), np.array([1, 0]), colors[0], label=r'$x_1 + 3x_2 > 3$', upper=upper)
- print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
- def f2(x1):
- return 5 - x1
- x2 = f2(x1)
- print(f'({round(random_x1_f2, 2)}) + ({round(random_x2_f2, 2)}) > 5')
- print(f'{round(random_x1_f2 + random_x2_f2, 2)} > 5')
- print(f'{random_x1_f2 + random_x2_f2 > 5}\n')
- if random_x1_f2 + random_x2_f2 > 5:
- upper = 1
- else:
- if random_x2_f2 < f2(random_x1_f2):
- upper = 1
- else:
- upper = 0
- plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
- plot(x1, x2, np.array([0, 5]), np.array([5, 0]), colors[1], label=r'$x_1 + x_2 > 5$', upper=upper)
- print(f"Пробна точка: ({round(random_x1_f3, 2)}, {round(random_x2_f3, 2)})")
- print(f'{round(random_x1_f3, 2)} < 4')
- print(f'{random_x1_f3 < 4}\n')
- x2 = np.copy(x1)
- plt.axvline(x=4, color=colors[2])
- plt.scatter(np.array([4, 4, random_x1_f3]), np.array([4, 0, random_x1_f3]), c=colors[2])
- plt.fill_betweenx([min(x2), max(x2)], min(x1), 4, color=colors[2], alpha=0.15, label=r'$x_1 < 4$')
- random_x1_f4 = random.uniform(-2, 10)
- random_x2_f4 = random.uniform(-2, 10)
- print(f"Пробна точка: ({round(random_x1_f4, 2)}, {round(random_x2_f4, 2)})")
- def f4(x1):
- return 2 + 2*x1
- x2 = f4(x1)
- print(f'(-2*{round(random_x1_f4, 2)}) + ({round(random_x2_f4, 2)}) > 2')
- print(f'{round(-2*random_x1_f4 + random_x2_f4, 2)} > 2')
- print(f'{-2*random_x1_f4 + random_x2_f4 > 2}\n')
- if -2*random_x1_f4 + random_x2_f4 > 2:
- upper = 1
- else:
- if random_x2_f4 < f4(random_x1_f4):
- upper = 1
- else:
- upper = 0
- plt.scatter(random_x1_f4, random_x2_f4, c=colors[5])
- plot(x1, x2, np.array([0, -1]), np.array([2, 0]), colors[5], label=r'$-2x_1 + x_2 > 2$', upper=upper)
- plt.grid()
- plt.legend()
- plt.xlim(-3, 10)
- plt.ylim(-3, 10)
- plt.show()
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = x_1 - 3x_2 \rightarrow max(min)$')
- plt.plot(np.linspace(-100, 100, 1000), np.array([0 for i in range(1000)]), c='black')
- plt.plot(np.array([0 for i in range(1000)]), np.linspace(-100, 100, 1000), c='black')
- print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
- def f1(x1):
- return (3 - x1) / 3
- x2 = f1(x1)
- print(f'({round(random_x1_f1, 2)}) + (3*{round(random_x2_f1, 2)}) > 3')
- print(f'{round(random_x1_f1 + 3*random_x2_f1, 2)} > 3')
- print(f'{random_x1_f1 + 3*random_x2_f1 > 3}\n')
- if random_x1_f1 + 3*random_x2_f1 > 3:
- upper = 1
- else:
- if random_x2_f1 < f1(random_x1_f1):
- upper = 1
- else:
- upper = 0
- plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
- plot(x1, x2, np.array([0, 3]), np.array([1, 0]), colors[0], label=r'$x_1 + 3x_2 > 3$', upper=upper)
- print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
- def f2(x1):
- return 5 - x1
- x2 = f2(x1)
- print(f'({round(random_x1_f2, 2)}) + ({round(random_x2_f2, 2)}) > 5')
- print(f'{round(random_x1_f2 + random_x2_f2, 2)} > 5')
- print(f'{random_x1_f2 + random_x2_f2 > 5}\n')
- if random_x1_f2 + random_x2_f2 > 5:
- upper = 1
- else:
- if random_x2_f2 < f2(random_x1_f2):
- upper = 1
- else:
- upper = 0
- plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
- plot(x1, x2, np.array([0, 5]), np.array([5, 0]), colors[1], label=r'$x_1 + x_2 > 5$', upper=upper)
- print(f"Пробна точка: ({round(random_x1_f3, 2)}, {round(random_x2_f3, 2)})")
- print(f'{round(random_x1_f3, 2)} < 4')
- print(f'{random_x1_f3 < 4}\n')
- x2 = np.copy(x1)
- plt.axvline(x=4, color=colors[2])
- plt.scatter(np.array([4, 4, random_x1_f3]), np.array([4, 0, random_x1_f3]), c=colors[2])
- plt.fill_betweenx([min(x2), max(x2)], min(x1), 4, color=colors[2], alpha=0.15, label=r'$x_1 < 4$')
- print(f"Пробна точка: ({round(random_x1_f4, 2)}, {round(random_x2_f4, 2)})")
- def f4(x1):
- return 2 + 2*x1
- x2 = f4(x1)
- print(f'(-2*{round(random_x1_f4, 2)}) + ({round(random_x2_f4, 2)}) > 2')
- print(f'{round(-2*random_x1_f4 + random_x2_f4, 2)} > 2')
- print(f'{-2*random_x1_f4 + random_x2_f4 > 2}\n')
- if -2*random_x1_f4 + random_x2_f4 > 2:
- upper = 1
- else:
- if random_x2_f4 < f4(random_x1_f4):
- upper = 1
- else:
- upper = 0
- plt.scatter(random_x1_f4, random_x2_f4, c=colors[5])
- plot(x1, x2, np.array([0, -1]), np.array([2, 0]), colors[5], label=r'$-2x_1 + x_2 > 2$', upper=upper)
- def f(x1):
- return x1 / 3
- x2 = f(x1)
- plt.plot(x1, x2, c='black', lw=2)
- plt.scatter(np.array([0, 6]), np.array([0, 2]), c='black')
- plt.annotate('Вектор нормалі', xy=(1, -3), xycoords='data', xytext=(-1.2, 0.4), textcoords='data', arrowprops=dict(facecolor='black'))
- plt.text(1, 4, f'max', fontsize=10, ha='right')
- plt.scatter(1, 4, c='white', s=1, label=r'$L_{min} \rightarrow - \infty$')
- plt.scatter(1, 4, c='red', edgecolors='black', s=50, label=r'$max(1, 4); L(max)=-11$')
- plt.grid()
- plt.legend()
- plt.xlim(-3, 10)
- plt.ylim(-5, 10)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement