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 = 10
- x1 = np.linspace(-100, 100, 1000)
- plt.figure(figsize=(10, 5))
- plt.title(r'$L = 3x_1 + 0x_2 + 4\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 4 + 2*x1
- x2 = f1(x1)
- print(f'(-2*{round(random_x1_f1, 2)}) + ({round(random_x2_f1, 2)}) < 4')
- print(f'{round(-2*random_x1_f1 + random_x2_f1, 2)} < 4')
- print(f'{-2*random_x1_f1 + random_x2_f1 < 4}\n')
- if -2*random_x1_f1 + random_x2_f1 < 4:
- upper = 0
- else:
- if random_x2_f1 > f1(random_x1_f1):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
- plot(x1, x2, np.array([0, 2]), np.array([4, 8]), colors[0], label=r'$-2x_1 + x_2 < 4$', 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 + 0x_2 + 4\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 4 + 2*x1
- x2 = f1(x1)
- print(f'(-2*{round(random_x1_f1, 2)}) + ({round(random_x2_f1, 2)}) < 4')
- print(f'{round(-2*random_x1_f1 + random_x2_f1, 2)} < 4')
- print(f'{-2*random_x1_f1 + random_x2_f1 < 4}\n')
- if -2*random_x1_f1 + random_x2_f1 < 4:
- upper = 0
- else:
- if random_x2_f1 > f1(random_x1_f1):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
- plot(x1, x2, np.array([0, 2]), np.array([4, 8]), colors[0], label=r'$-2x_1 + x_2 < 4$', 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 (-1 + x1) / 2
- x2 = f2(x1)
- print(f'(-{round(random_x1_f2, 2)}) + (2*{round(random_x2_f2, 2)}) > -1')
- print(f'{round(-random_x1_f2 + 2*random_x2_f2, 2)} > -1')
- print(f'{-random_x1_f2 + 2*random_x2_f2 > -1}\n')
- if -random_x1_f2 + 2*random_x2_f2 > -1:
- 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([1, 3]), np.array([0, 1]), colors[1], label=r'$-x_1 + 2x_2 > -1$', 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 + 0x_2 + 4\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 4 + 2*x1
- x2 = f1(x1)
- print(f'(-2*{round(random_x1_f1, 2)}) + ({round(random_x2_f1, 2)}) < 4')
- print(f'{round(-2*random_x1_f1 + random_x2_f1, 2)} < 4')
- print(f'{-2*random_x1_f1 + random_x2_f1 < 4}\n')
- if -2*random_x1_f1 + random_x2_f1 < 4:
- upper = 0
- else:
- if random_x2_f1 > f1(random_x1_f1):
- upper = 0
- else:
- upper = 1
- plt.scatter(random_x1_f1, random_x2_f1, c=colors[0])
- plot(x1, x2, np.array([0, 2]), np.array([4, 8]), colors[0], label=r'$-2x_1 + x_2 < 4$', upper=upper)
- print(f"Пробна точка: ({round(random_x1_f2, 2)}, {round(random_x2_f2, 2)})")
- def f2(x1):
- return (-1 + x1) / 2
- x2 = f2(x1)
- print(f'(-{round(random_x1_f2, 2)}) + (2*{round(random_x2_f2, 2)}) > -1')
- print(f'{round(-random_x1_f2 + 2*random_x2_f2, 2)} > -1')
- print(f'{-random_x1_f2 + 2*random_x2_f2 > -1}\n')
- if -random_x1_f2 + 2*random_x2_f2 > -1:
- 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([1, 3]), np.array([0, 1]), colors[1], label=r'$-x_1 + 2x_2 > -1$', upper=upper)
- x2 = np.copy(x1)
- plt.axvline(x=-4/3, lw=2, color='black')
- plt.scatter(np.array([-4/3, -4/3]), np.array([4, 0]), c='black')
- plt.annotate('Вектор нормалі', xy=(3, 0), xycoords='data', xytext=(-3.3, 0.1), textcoords='data', arrowprops=dict(facecolor='black'))
- plt.scatter(100, 1000, c='red', edgecolors='black', s=50, label=r'$L_{max} \rightarrow \infty$')
- plt.grid()
- plt.legend()
- plt.xlim(-3, scale)
- plt.ylim(-3, scale)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement