0

I'm working with optimization and I'd like to get a plot of the intersection of two constraints. This is one constraint and this is the other. What I need is the something like that.

Here is a piece of the code that generates both constraints:

# First constraint    
        x1_r1c, x2_r1c = np.meshgrid(x1_c, x2)
        y_r1c = x2_r1c - x1_r1c**2
        fig4, ax4 = plt.subplots()
        cont_r1 = ax4.contourf(x1_r1c, x2_r1c, y_r1c, np.arange(0,100,2), colors='blue', extend='above')

# Second constraint

        x1_r2c, x2_r2c = np.meshgrid(x1_c, x2)
        y_r2c = x2_r2c + x1_r2c - 6
        fig6, ax6 = plt.subplots()
        cont_r2 = ax6.contourf(x1_r2c, x2_r2c, y_r2c, np.arange(-8, 0, 0.1), colors='blue', extend='below') 

I've tried to use numpy.intersect1d but I had some problems with size. I've also tried to subtract y_r1c and y_r2c but it didn't work at all. My major doubt is in find out if I need to create a brand new variable based on y_r1c and y_r2c to get the intersection of the constraints.

Thank you for the help.

cieslak
  • 1
  • 3
  • i'd suggest creating a boolean variable by `and`ing your constraints. Then plot that boolean. – Dave Kielpinski Mar 11 '20 at 18:27
  • Sorry, but I haven't understood well. Could you explain a little bit more? – cieslak Mar 11 '20 at 18:30
  • sorry - what i suggest is that you should convert each constraint to a boolean function, returning 1 if the constraint is satisfied and 0 if it isn't. Then you can get the intersection of constraints by `and`ing those two functions to get a new boolean function. Finally, plot the intersection of constraints directly - its values are 1 and 0. – Dave Kielpinski Mar 11 '20 at 18:39
  • Hmm, so I create a boolean matrix (by using, as example, `numpy.array(y_r1c, dtype=bool)`, for each constraint? Then perform the intersection between them? – cieslak Mar 11 '20 at 18:50
  • it's even easier than you think. for numpy arrays `x` and `y`, `z = (x == y)` returns a boolean array by elementwise comparison. so each element of `z` is 1 or 0 depending on whether `x` at that element equals `y` at that element. if `y` is a constant, the elementwise comparison is done using the constant at each element. likewise you can use other comparison operators in place of `==`. – Dave Kielpinski Mar 11 '20 at 20:08

0 Answers0