14

I would like to plot the following inequalities: y < p2(1 - p1) and x < p1(1 - ( y / (1 - p1))).

Given that the first is satisfied, I want to plot the region in which both are satisfied.
p1 and p2 can vary within [0,1].

I would appreciate any help!

Eitan T
  • 32,660
  • 14
  • 72
  • 109
user1173405
  • 175
  • 1
  • 2
  • 6

3 Answers3

18

Try this: The red area is where both inequalities are satisfied.

[X,Y]=meshgrid(0:0.01:1,0:0.01:1); % Make a grid of points between 0 and 1
p1=0.1; p2=0.2; % Choose some parameters
ineq1 = Y<p2*(1-p1);
ineq2 = X<p1*(1-(Y./(1-p1)));
colors = zeros(size(X))+ineq1+ineq2;
scatter(X(:),Y(:),3,colors(:),'filled')

enter image description here

Shai
  • 111,146
  • 38
  • 238
  • 371
Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
  • 4
    You may want to explain the "magic" behind the `colors = ...` line. – sfstewman Jul 05 '12 at 16:30
  • 2
    The explanation for the colors is that each inequation is actually a binary matrix of 0's and 1's indicating where it is satisfied and where it is not. `colors` is their sum, and equals 2 where both inequations are satisfied, 1 where only one of them is, and 0 where none are. `scatter` assigns a different color for each value, and `2` gets the red color (in the default colormap red is assigned to the maximal value). – Eitan T Jul 06 '12 at 00:31
10

An alternative solution (yet similar to Sam Robert's) would be using contourf:

[X, Y] = meshgrid((0:999) / 1000, (0:999) / 1000);
p = rand(2, 1);                            %# In this example p = [0.1, 0.2]
ineq1 = Y < p(2) * (1 - p(1));             %# First inequation
ineq2 = X < p(1) * (1 - (Y / (1 - p(1)))); %# Second inequation
both = ineq1 & ineq2;                      %# Intersection of both inequations

figure, hold on
c = 1:3;                                   %# Contour levels
contourf(c(1) * ineq1, [c(1), c(1)], 'b')  %# Fill area for first inequation
contourf(c(2) * ineq2, [c(2), c(2)], 'g')  %# Fill area for second inequation
contourf(c(3) * both, [c(3), c(3)], 'r')   %# Fill area for both inequations
legend('First', 'Second', 'Both')
set(gca, ...                               %# Fixing axes ticks
    'XTickLabel', {t(get(gca, 'XTick'))}, 'YTickLabel', {t(get(gca, 'YTick'))})

and this is the result:

Result

The red area (as mentioned in the legend) indicates where both inequations are satisfied.

Note that the second and third contourf calls are just for illustration, to show where only one of the inequations is satisfied.

Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • This solution didn't work for anything but your inequation. What is the meaning of meshgrid parameters? How to make it fit my inequations, whatever they are? – Tomáš Zato Mar 15 '15 at 14:22
  • 1
    @TomášZato Perhaps you didn't employ my solution properly. The meshgrid is supposed to create a two-dimensional matrix of X and Y values as an input for the inequalities. – Eitan T Mar 15 '15 at 15:33
  • Yes, I surelly didn't use your solution properly. With [my equations](http://www.wolframalpha.com/input/?i=x%3C%3D30%2B5y%2C+x%3C%3D0), all I could see was blank graph. When I edited the meshgrid to `meshgrid((-100:100) , (-100:100));` I could see nice plot as yours but [with messed up axes markers](http://i.stack.imgur.com/TZGyl.png). – Tomáš Zato Mar 15 '15 at 15:44
  • @TomášZato I think you get an empty graph because the `meshgrid` in my example yields only positive coordinates, whereas your inequalities are satisfied by negative values. I can only speculate that you get "messed up" markers because your inequalities aren't implemented/normalized properly (you didn't share your implementation, so I cannot say where it went wrong). – Eitan T Mar 22 '15 at 13:56
  • What is _t_ ? MATLAB says: Undefined function or variable 't' – David Balažic Mar 17 '16 at 03:03
  • @DavidBalažic You're right, the line that defines `t` is missing. It is used to adjust the tick labels, and it is supposed to be the same vector that built `X` and `Y`. In this example `t = (0:999) / 100`. You can also comment out the last statement if it bothers you. – Eitan T Mar 17 '16 at 10:30
3

I think this method is easy to understand. Make a surface plot and rotate it to top view.

v = -5:0.1:5;
p1 = 0.1;
p2 = 0.2;
[x,y] = meshgrid(v);
ineq1 = y<p2*(1-p1);
ineq2 = x<p1*(1-(y./(1-p1)));
ineq = double(ineq1 & ineq2);    % intersection of the inequalities
surf(x,y,ineq);
view(0,90)      % rotate surface plot to top view
Karan Gill
  • 160
  • 5