0

I am having difficulties setting the maximum and minimum values to shade the feasible region. Please take a look at my code and let me know where I went wrong, thank you.

import numpy as np
import matplotlib.pyplot as plt

'''
Let x = Square Tables and y = Round Tables

Minimize: z = 40x + 60y
Subject to: x + y >= 5 or y1 = 5 - x BLUE
        x + y <= 10 or y2 = 10 - x ORANGE
        x >= y + 3 or y3 = x - 3 GREEN
'''

x = np.linspace(0, 20, 100)
y1 = 5 - x
y2 = 10 - x
y3 = x - 3

plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.xlim((0, 12))
plt.ylim((0, 12))
plt.xlabel(r'$Square Tables$')
plt.ylabel(r'$Round Tables$')

y4 = np.minimum(y1, y2)
y5 = np.maximum(y1, y3)
plt.fill_between(x, y4, y5, color='grey', alpha=0.5)
Gru26
  • 19
  • 4

1 Answers1

2

You can do this:

x = np.linspace(0, 20, 100)
y1 = 5 - x
y2 = 10 - x
y3 = x - 3

plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.xlim((0, 12))
plt.ylim((0, 12))
plt.xlabel(r'$Square Tables$')
plt.ylabel(r'$Round Tables$')
y5 = np.minimum(y2, y3)
plt.fill_between(x, y1, y5,where=y5 >= y1, color='grey', alpha=0.5)

enter image description here

Stelios
  • 5,271
  • 1
  • 18
  • 32
  • Thank you so much! – Gru26 Feb 11 '18 at 17:53
  • Is there a way to determine the actual minimum values using Python and that plot? I can do it by hand, just interested to see what code I would use to minimize my cost... – Gru26 Feb 11 '18 at 18:11
  • @Gru26 You need to use an optimization routine to solve optimization problems. There are many python packages for this purpose. `scipy.optimize` is a good place to start. – Stelios Feb 11 '18 at 19:08