I am going to assume this image is the result you want, i.e. g
is always a line with negative slope and h
is always a line with positive slope:
Here's the function used to generate one of these many plots. I recommend you read the rationale below before looking at how the code works.
function main(ax, g, h)
% ax - handle to an Axes object, i.e. ax = gca;
% g - symbolic function of line with negative slope, i.e. syms g(x); g(x) = 3 - x;
% h - symbolic function of line with positive slope, i.e. syms h(x); h(x) = 1 + x;
X = linspace(-5, 5, 101);
Y = linspace(-5, 5, 101)';
G = double(g(X))-Y; % compute g - y everywhere
H = double(h(X))-Y; % compute h - y everywhere
gi = finverse(g);
hi = finverse(h);
% solve x-intercepts
gx = solve(g);
hx = solve(h);
% solve y-intercepts
gy = solve(gi);
hy = solve(hi);
if sign(gx) == sign(hx)% triangles up/down stacked
% LR: indicates whether we want left/right of Y-axis
% gc: comparison operator (>= or <=) of `g-Y (gc) 0`
% gY: comparison operator (>= or <=) of `Y (gY) 0` for triangle formed by g
% hc: comparison operator (>= or <=) of `h-Y (hc) 0`
% hY: comparison operator (>= or <=) of `Y (hY) 0` for triangle formed by h
if gx >= 0 % triangles in Q1/Q4
LR = X >= 0;
elseif gx < 0 % triangles in Q2/Q3
LR = X <= 0;
end
if gy >= 0 % look below g
gc = @(x) ge(x, 0);
gY = @(x) ge(x, 0);
else % look above g
gc = @(x) le(x, 0);
gY = @(x) le(x, 0);
end
if hy >= 0 % look below h
hc = @(x) ge(x, 0);
hY = @(x) ge(x, 0);
else % look above h
hc = @(x) le(x, 0);
hY = @(x) le(x, 0);
end
gt = gc(G) & gY(Y) & LR;
ht = hc(H) & hY(Y) & LR;
elseif sign(gy) == sign(hy) % triangles left/right side by side
% UD: indicates whether we want up/down of X-axis
% gc: comparison operator (>= or <=) of `g-Y (gc) 0`
% gX: comparison operator (>= or <=) of `X (gX) 0` for triangle formed by g
% hc: comparison operator (>= or <=) of `h-Y (hc) 0`
% hX: comparison operator (>= or <=) of `X (hX) 0` for triangle formed by h
if gy >= 0 % triangles in Q1/Q2
UD = Y >= 0;
elseif gx < 0 % triangles in Q3/Q4
UD = Y <= 0;
end
if gx >= 0 % look left/below of g
gc = @(x) ge(x, 0);
gX = @(x) ge(x, 0);
else % look right/above g
gc = @(x) le(x, 0);
gX = @(x) le(x, 0);
end
if hx >= 0 % look left/above of g
hc = @(x) le(x, 0);
hX = @(x) ge(x, 0);
else % look right/below h
hc = @(x) ge(x, 0);
hX = @(x) le(x, 0);
end
gt = gc(G) & gX(X) & UD;
ht = hc(H) & hX(X) & UD;
else % only 1 triangle
ghx = solve(g == h);
ghy = solve(gi == hi);
if ghy >= 0 % Q1/2
if ghx >= 0 % Q1
gt = G >= 0 & X >= 0 & Y >= 0;
ht = gt;
else % Q2
ht = H >= 0 & X <= 0 & Y >= 0;
gt = ht;
end
else % Q3/4
if ghx >= 0 % Q4
ht = H <= 0 & X >= 0 & Y <= 0;
gt = ht;
else % Q3
gt = G <= 0 & X <= 0 & Y <= 0;
ht = gt;
end
end
end
Z = 0*(X+Y);
Z(gt | ht) = 1;
contourf(ax, X, Y, Z, [1,1]);
plot(ax, [-5, 5], [0, 0], 'k-')
plot(ax, [0,0], [-5, 5], 'k-');
plot(ax, [-5, 5], double(g([-5, 5])), 'b-');
plot(ax, [-5, 5], double(h([-5, 5])), 'r-');
end
The function can be called as:
syms x g(x) h(x)
g(x) = 3 - x;
h(x) = 1 + x;
ax = gca;
hold(ax, 'on')
set(ax, 'XLim', [-5, 5], 'YLim', [-5, 5]);
main(ax, g, h)
The main idea of the method is as follows:
- Generate a 2D grid of zeros over the 2D plane.
- If a cell is within the specified area, then make it 1.
- Then plot a contour of this grid.
The down side of this method is that the polygons are jagged/imperfect and not smooth. This is because this is a numerical solution to the problem as opposed to an analytical solution.
The part that required the most thinking was how to determine the area to plot. In general, the plotted area is either 1 right-angle triangle (if one of g
and h
pass the origin), or 2 right-angle triangles (all other cases except when both g
and h
pass through origin).
In the case of 1 right-angle triangle, just find the intersection of g
and h
, determine its quadrant, and determine whether the hypotenuse of the right-angle triangle is part of g
or h
.
In the case of 2 right-angle triangles, first determine whether they are aligned side by side (if y-intercepts are both positive/negative), or one on top of the other (if x-intercepts are both positive/negative). In each case, one triangle's hypotenuse is part of g
, and the other's hypotenuse is part of h
. We consider the case where the triangles are aligned side by side, since other case is analogous:
- If the y-intercept is positive, then the X-axis is the lower bound of each triangle, i.e. we want the area that satisfies
Y >= 0
.
If the x-intercept of g
is positive, then we want the area below g
, as far left as the Y-axis, i.e. we want the points (x,y) satisfying y <= g
under the constraint x >= 0
, which is equivalent to g-Y >= 0
and X >= 0
.
(If the x-intercept of g
is negative, then we want the area above g
, as far right as the Y-axis. This translates to g - Y <= 0
and X <= 0
.)
The triangle formed by g
is thus constrained by the conditions Y >= 0
, g-Y >= 0
, and X >= 0
.
- Repeat step 2 to get the constraints for the triangle formed by
h
.
- The final desired area is the union of the areas formed by the two triangles.