|
| 1 | + |
| 2 | + |
| 3 | +## Como desenhar o gráfico manualmente |
| 4 | + |
| 5 | +1. **Eixos**: Desenhe os eixos $x_1$ (horizontal) e $x_2$ (vertical), ambos a partir de zero (pois $x_1, x_2 \geq 0$). |
| 6 | +2. **Desenhe as retas das restrições (igualdades):** |
| 7 | + |
| 8 | +- $x_1 + 3x_2 = 7$ |
| 9 | +Pontos: |
| 10 | + - $x_1=0 \Rightarrow x_2 = \frac{7}{3} \approx 2,33$ |
| 11 | + - $x_2=0 \Rightarrow x_1=7$ |
| 12 | +- $2x_1 + 2x_2 = 8$ |
| 13 | +Pontos: |
| 14 | + - $x_1=0 \Rightarrow x_2=4$ |
| 15 | + - $x_2=0 \Rightarrow x_1=4$ |
| 16 | +- $x_1 + x_2 = 3$ |
| 17 | +Pontos: |
| 18 | + - $x_1=0 \Rightarrow x_2=3$ |
| 19 | + - $x_2=0 \Rightarrow x_1=3$ |
| 20 | +- $x_2 = 2$ (reta horizontal) |
| 21 | + |
| 22 | +3. **Sombreie a região que satisfaz as desigualdades (área abaixo ou à esquerda das retas, e acima dos eixos).** |
| 23 | +4. **Marque os vértices da região viável e identifique o ponto ótimo (3,0).** |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## Código Python para gerar o gráfico |
| 28 | + |
| 29 | +```python |
| 30 | +import numpy as np |
| 31 | +import matplotlib.pyplot as plt |
| 32 | + |
| 33 | +# Definir os valores de x1 para plotar as retas |
| 34 | +x1 = np.linspace(0, 8, 400) |
| 35 | + |
| 36 | +# Restrições |
| 37 | +# 1) x1 + 3x2 <= 7 => x2 <= (7 - x1)/3 |
| 38 | +x2_1 = (7 - x1) / 3 |
| 39 | + |
| 40 | +# 2) 2x1 + 2x2 <= 8 => x2 <= (8 - 2x1)/2 = 4 - x1 |
| 41 | +x2_2 = 4 - x1 |
| 42 | + |
| 43 | +# 3) x1 + x2 <= 3 => x2 <= 3 - x1 |
| 44 | +x2_3 = 3 - x1 |
| 45 | + |
| 46 | +# 4) x2 <= 2 |
| 47 | +x2_4 = 2 * np.ones_like(x1) |
| 48 | + |
| 49 | +# Limites para x2 (não negativas) |
| 50 | +x2_min = np.zeros_like(x1) |
| 51 | + |
| 52 | +# Plotar as restrições |
| 53 | +plt.figure(figsize=(8,8)) |
| 54 | +plt.plot(x1, x2_1, label=r'$x_1 + 3x_2 \leq 7$') |
| 55 | +plt.plot(x1, x2_2, label=r'$2x_1 + 2x_2 \leq 8$') |
| 56 | +plt.plot(x1, x2_3, label=r'$x_1 + x_2 \leq 3$') |
| 57 | +plt.plot(x1, x2_4, label=r'$x_2 \leq 2$') |
| 58 | + |
| 59 | +# Preencher a região viável |
| 60 | +# Criar uma grade de pontos |
| 61 | +X1, X2 = np.meshgrid(np.linspace(0,8,400), np.linspace(0,5,400)) |
| 62 | + |
| 63 | +# Condições das restrições |
| 64 | +cond1 = X1 + 3*X2 <= 7 + 1e-5 |
| 65 | +cond2 = 2*X1 + 2*X2 <= 8 + 1e-5 |
| 66 | +cond3 = X1 + X2 <= 3 + 1e-5 |
| 67 | +cond4 = X2 <= 2 + 1e-5 |
| 68 | +cond5 = X1 >= 0 |
| 69 | +cond6 = X2 >= 0 |
| 70 | + |
| 71 | +# Região viável |
| 72 | +region = cond1 & cond2 & cond3 & cond4 & cond5 & cond6 |
| 73 | + |
| 74 | +plt.imshow(region.astype(int), extent=(0,8,0,5), origin='lower', cmap='Greys', alpha=0.3) |
| 75 | + |
| 76 | +# Pontos vértices (calculados anteriormente) |
| 77 | +vertices = np.array([ |
| 78 | + [0,0], |
| 79 | + [0,2], |
| 80 | + [1,2], |
| 81 | + [2,0], |
| 82 | + [3,0] |
| 83 | +]) |
| 84 | + |
| 85 | +plt.scatter(vertices[:,0], vertices[:,1], color='red') |
| 86 | +for i, (x, y) in enumerate(vertices): |
| 87 | + plt.text(x + 0.1, y, f'({x},{y})', fontsize=9) |
| 88 | + |
| 89 | +# Ponto ótimo |
| 90 | +plt.scatter(3,0, color='green', s=100, label='Ótimo (3,0)') |
| 91 | + |
| 92 | +plt.xlim(0,8) |
| 93 | +plt.ylim(0,5) |
| 94 | +plt.xlabel(r'$x_1$') |
| 95 | +plt.ylabel(r'$x_2$') |
| 96 | +plt.title('Região Viável e Restrições do Problema') |
| 97 | +plt.legend() |
| 98 | +plt.grid(True) |
| 99 | +plt.show() |
| 100 | +``` |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +### Como usar o código |
| 105 | + |
| 106 | +- Copie e cole o código em um ambiente Python com as bibliotecas **numpy** e **matplotlib** instaladas (ex: Jupyter Notebook, Google Colab, Anaconda). |
| 107 | +- Execute para visualizar o gráfico com as restrições, região viável sombreada, vértices e ponto ótimo destacado. |
| 108 | + |
0 commit comments