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']
- scale = 16
- x1 = np.linspace(-100, 100, 1000)
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = 3x_1 + 3x_2 \rightarrow max$')
- 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, scale)
- random_x2_f1 = random.uniform(-2, scale)
- print(f"Пробна точка: ({round(random_x1_f1, 2)}, {round(random_x2_f1, 2)})")
- def f1(x1):
- return 2 - x1
- x2 = f1(x1)
- print(f'({round(random_x1_f1, 2)}) + ({round(random_x2_f1, 2)}) > 2')
- print(f'{round(random_x1_f1 + random_x2_f1, 2)} > 2')
- print(f'{random_x1_f1 + random_x2_f1 > 2}\n')
- if random_x1_f1 + random_x2_f1 > 2:
- 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, 2]), np.array([2, 0]), colors[0], label=r'$x_1 + x_2 > 2$', upper=upper)
- plt.grid()
- plt.legend()
- plt.xlim(-3, scale)
- plt.ylim(-3, scale)
- plt.show()
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = 3x_1 + 3x_2 \rightarrow max$')
- 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 2 - x1
- x2 = f1(x1)
- print(f'({round(random_x1_f1, 2)}) + ({round(random_x2_f1, 2)}) > 2')
- print(f'{round(random_x1_f1 + random_x2_f1, 2)} > 2')
- print(f'{random_x1_f1 + random_x2_f1 > 2}\n')
- if random_x1_f1 + random_x2_f1 > 2:
- 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, 2]), np.array([2, 0]), colors[0], label=r'$x_1 + x_2 > 2$', upper=upper)
- random_x1_f2 = random.uniform(-2, scale)
- random_x2_f2 = random.uniform(-2, scale)
- print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
- def f2(x1):
- return (14 - x1) / 7
- x2 = f2(x1)
- print(f'({round(random_x1_f2, 2)}) + (7*{round(random_x2_f2, 2)}) < 14')
- print(f'{round(random_x1_f2 + 7*random_x2_f2, 2)} < 14')
- print(f'{random_x1_f2 + 7*random_x2_f2 < 14}\n')
- if random_x1_f2 + 7*random_x2_f2 < 14:
- upper = 0
- else:
- if random_x2_f2 > f2(random_x1_f2):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
- plot(x1, x2, np.array([0, 14]), np.array([2, 0]), colors[1], label=r'$x_1 + 7x_2 < 14$', upper=upper)
- plt.grid()
- plt.legend()
- plt.xlim(-3, scale)
- plt.ylim(-3, scale)
- plt.show()
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = 3x_1 + 3x_2 \rightarrow max$')
- 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 2 - x1
- x2 = f1(x1)
- print(f'({round(random_x1_f1, 2)}) + ({round(random_x2_f1, 2)}) > 2')
- print(f'{round(random_x1_f1 + random_x2_f1, 2)} > 2')
- print(f'{random_x1_f1 + random_x2_f1 > 2}\n')
- if random_x1_f1 + random_x2_f1 > 2:
- 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, 2]), np.array([2, 0]), colors[0], label=r'$x_1 + x_2 > 2$', upper=upper)
- print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
- def f2(x1):
- return (14 - x1) / 7
- x2 = f2(x1)
- print(f'({round(random_x1_f2, 2)}) + (7*{round(random_x2_f2, 2)}) < 14')
- print(f'{round(random_x1_f2 + 7*random_x2_f2, 2)} < 14')
- print(f'{random_x1_f2 + 7*random_x2_f2 < 14}\n')
- if random_x1_f2 + 7*random_x2_f2 < 14:
- upper = 0
- else:
- if random_x2_f2 > f2(random_x1_f2):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
- plot(x1, x2, np.array([0, 14]), np.array([2, 0]), colors[1], label=r'$x_1 + 7x_2 < 14$', upper=upper)
- random_x1_f3 = random.uniform(-2, scale)
- random_x2_f3 = random.uniform(-2, scale)
- print(f"Пробна точка: ({round(random_x1_f3, 2)}, {round(random_x2_f3, 2)})")
- def f3(x1):
- return (x1 - 2) / 5
- x2 = f3(x1)
- print(f'({round(random_x1_f3, 2)}) - (5*{round(random_x2_f3, 2)}) < 2')
- print(f'{round(random_x1_f3 - 5*random_x2_f3, 2)} < 2')
- print(f'{random_x1_f3 - 5*random_x2_f3 < 2}\n')
- if random_x1_f3 - 5*random_x2_f3 < 2:
- upper = 1
- else:
- if random_x2_f3 > f2(random_x1_f3):
- upper = 1
- else:
- upper = 0
- plt.scatter(random_x1_f3, random_x2_f3, c=colors[2])
- plot(x1, x2, np.array([2, 7]), np.array([0, 1]), colors[2], label=r'$x_1 - 5x_2 < 2$', upper=upper)
- plt.grid()
- plt.legend()
- plt.xlim(-3, scale)
- plt.ylim(-3, scale)
- plt.show()
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = 3x_1 + 3x_2 \rightarrow max$')
- 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 2 - x1
- x2 = f1(x1)
- print(f'({round(random_x1_f1, 2)}) + ({round(random_x2_f1, 2)}) > 2')
- print(f'{round(random_x1_f1 + random_x2_f1, 2)} > 2')
- print(f'{random_x1_f1 + random_x2_f1 > 2}\n')
- if random_x1_f1 + random_x2_f1 > 2:
- 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, 2]), np.array([2, 0]), colors[0], label=r'$x_1 + x_2 > 2$', upper=upper)
- print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
- def f2(x1):
- return (14 - x1) / 7
- x2 = f2(x1)
- print(f'({round(random_x1_f2, 2)}) + (7*{round(random_x2_f2, 2)}) < 14')
- print(f'{round(random_x1_f2 + 7*random_x2_f2, 2)} < 14')
- print(f'{random_x1_f2 + 7*random_x2_f2 < 14}\n')
- if random_x1_f2 + 7*random_x2_f2 < 14:
- upper = 0
- else:
- if random_x2_f2 > f2(random_x1_f2):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f2, random_x2_f2, c=colors[1])
- plot(x1, x2, np.array([0, 14]), np.array([2, 0]), colors[1], label=r'$x_1 + 7x_2 < 14$', upper=upper)
- print(f"Пробна точка: ({round(random_x1_f3, 2)}, {round(random_x2_f3, 2)})")
- def f3(x1):
- return (x1 - 2) / 5
- x2 = f3(x1)
- print(f'({round(random_x1_f3, 2)}) - (5*{round(random_x2_f3, 2)}) < 2')
- print(f'{round(random_x1_f3 - 5*random_x2_f3, 2)} < 2')
- print(f'{random_x1_f3 - 5*random_x2_f3 < 2}\n')
- if random_x1_f3 - 5*random_x2_f3 < 2:
- upper = 1
- else:
- if random_x2_f3 > f2(random_x1_f3):
- upper = 1
- else:
- upper = 0
- plt.scatter(random_x1_f3, random_x2_f3, c=colors[2])
- plot(x1, x2, np.array([2, 7]), np.array([0, 1]), colors[2], label=r'$x_1 - 5x_2 < 2$', upper=upper)
- def f(x1):
- return -3*x1 / 3
- x2 = f(x1)
- plt.plot(x1, x2, c='black', lw=2)
- plt.scatter(np.array([0, 1]), np.array([0, -1]), c='black')
- plt.annotate('Вектор нормалі', xy=(3, 3), xycoords='data', xytext=(-1.1, -2.4), textcoords='data', arrowprops=dict(facecolor='black'))
- plt.text(7, 1, f'C - max', fontsize=10, ha='right')
- plt.text(0, 2, f'A', fontsize=10, ha='right')
- plt.text(2, 0, f'B', fontsize=10, ha='right')
- plt.plot(np.array([0, 2]), np.array([2, 0]), c='white', label=r'$min(AB); L(min)=6$')
- plt.scatter(7, 1, c='red', edgecolors='black', s=50, label=r'$max(7, 1); L(max)=24$')
- plt.grid()
- plt.legend()
- plt.xlim(-3, scale)
- plt.ylim(-3, scale)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement