如何绘制不平等

use*_*405 12 matlab

我想绘制以下不等式: y < p 2(1 - p 1)和x < p 1(1 - (y /(1 - p 1))).

鉴于第一个是满意的,我想绘制两个都满足的区域.
p 1p 2可以在[0,1]内变化.

我将不胜感激任何帮助!

Sam*_*rts 17

试试这个:红色区域是两个不等式都满足的地方.

[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')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 您可能想要解释`colors = ...`行背后的"魔力". (4认同)
  • 对颜色的解释是每个不等式实际上是0和1的二进制矩阵,表示它满足的位置和不满足的位置.`colors`是它们的总和,等于2满足两个不等式,1其中只有一个是,而0则没有.`scatter`为每个值指定不同的颜色,`2`得到红色(在默认的colormap中红色指定为最大值). (2认同)

Eit*_*n T 8

另一种解决方案(类似于Sam Robert's)将使用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'))})
Run Code Online (Sandbox Code Playgroud)

这就是结果:

结果

红色区域(如图例中所述)表示满足两个不等式的位置.

请注意,第二次和第三次contourf调用仅用于说明,以显示仅满足其中一个不等式的位置.