Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot inequalities

Tags:

matlab

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!

like image 778
user1173405 Avatar asked Jul 05 '12 13:07

user1173405


People also ask

How do you graph an inequality?

To graph an inequality, treat the <, ≤, >, or ≥ sign as an = sign, and graph the equation. If the inequality is < or >, graph the equation as a dotted line. If the inequality is ≤ or ≥, graph the equation as a solid line.


2 Answers

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

like image 135
Sam Roberts Avatar answered Oct 09 '22 20:10

Sam Roberts


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.

like image 37
Eitan T Avatar answered Oct 09 '22 22:10

Eitan T