2

I am using the following code to plot the intersection points on a graph, then visually inspecting the intersection points to go back to code and shade the feasibility region.

Is there a better way to find the feasible region than simply plotting the lines and reading the intersection points off the graph?

# Filling a polygon locating the corner points
# Then let Matplotlib fill within these points
x= [0.0, 0.0, 6.67,5.0]
y= [0.0, 4.0, .67, 0.0]
fill(x,y)
show()

Full code example:

x= arange(-3,10.1,0.1)
y= arange(-3,10.1,0.1)
y1= 0.4*x-2.0
y2= 4.0-0.5*x

xlim(-3,10)
ylim(-3,10)
hlines(0,-3,10,color='k')
vlines(0,-3,10,color='k')
grid(True)

xlabel('x-axis')
ylabel('y-axis')
title ('Shaded Area Shows the Feasible Region')

plot(x,y1,color='b')
plot(x,y2,color='r')
legend(['2x-5y=10','x+2y=8'])

x= [0.0, 0.0, 6.67,5.0]
y= [0.0, 4.0, .67, 0.0]
fill(x,y)
show()
ali_m
  • 71,714
  • 23
  • 223
  • 298
Boerseun
  • 21
  • 3

1 Answers1

1

If you just want to draw the feasibility region you can do:

x= arange(-3,10.1,0.1)
y= arange(-3,10.1,0.1)
y1= 0.4*x-2.0
y2= 4.0-0.5*x

xlim(-3,10)
ylim(-3,10)
hlines(0,-3,10,color='k')
vlines(0,-3,10,color='k')
grid(True)

xlabel('x-axis')
ylabel('y-axis')
title ('Shaded Area Shows the Feasible Region')

plot(x,y1,color='b')
plot(x,y2,color='r')
legend(['2x-5y=10','x+2y=8'])

bottom = np.maximum(y1, 0)
fill_between(x, bottom, y2, where=(x>0) & (y2>y1))

enter image description here

elyase
  • 39,479
  • 12
  • 112
  • 119
  • Thank you Alyase, but I need to know the plot points. I found some indications that perhaps something like explained here [link](http://stackoverflow.com/questions/13662525/how-to-get-pixel-coordinates-for-matplotlib-generated-scatterplot) but I don't know how to make it work with my code. – Boerseun Feb 05 '15 at 00:15
  • The intersection point is easy, just calculate `y1-y2` and look where it crosses 0. – elyase Feb 05 '15 at 03:24
  • Could you please explain this snippet in a little bit more detail: `bottom = np.maximum(y1, 0)`? – bmc Jul 14 '17 at 12:36
  • @bmc, that returns an array with the positive values from `y1`, and zeros where `y1` is negative. – elyase Jul 18 '17 at 00:05