Plot symbolic inequalities and systems of inequalities in the Cartesian plane.
inequalityplot shades the region that satisfies one symbolic inequality or
the intersection of several symbolic inequalities. It also plots the boundary
of each inequality using fimplicit.
- MATLAB R2022b or later
- Symbolic Math Toolbox
inequalityplot(ineq)
inequalityplot(ineq,xLimits,yLimits)
h = inequalityplot(___)
h = inequalityplot(___,Name=Value)where ineq is a symbolic inequality, symbolic array, or cell array of
symbolic inequalities involving exactly two symbolic variables.
xLimits and yLimits are two-element vectors:
[minimum maximum]The default plotting window is:
-5 <= x <= 5
-5 <= y <= 5Name-value arguments:
| Name | Default | Description |
|---|---|---|
Resolution |
500 |
Number of grid points in each direction. |
FaceAlpha |
0.25 |
Transparency of the shaded feasible region. |
FaceColor |
[0 0.4470 0.7410] |
RGB color of the shaded feasible region. |
The function returns a structure h with handles to the shaded region and the
boundary curves:
h.Region
h.Boundarysyms x y
figure
inequalityplot(y <= 2*x + 1,[-3 3],[-4 7])
axis equal
grid onThis plots the half-plane satisfying:
y <= 2*x + 1Several inequalities can be passed together. The shaded region is their logical intersection.
syms x y
ineq = [
y >= x
y <= -x + 4
];
figure
inequalityplot(ineq,[-2 6],[-2 6])
axis equal
grid onThe function is useful for visualizing two-variable linear programming constraints.
syms x y
constraints = [
x >= 0
y >= 0
2*x + y <= 8
x + 2*y <= 8
];
figure
inequalityplot(constraints,[0 6],[0 6], ...
FaceAlpha=0.35, ...
FaceColor=[0.2 0.6 0.3])
axis equal
grid on
xlabel("x")
ylabel("y")
title("Linear Programming Feasible Region")You can also overlay objective-function level curves:
hold on
fimplicit(3*x + 2*y == 6,[0 6 0 6],"--")
fimplicit(3*x + 2*y == 10,[0 6 0 6],"--")
fimplicit(3*x + 2*y == 14,[0 6 0 6],"--")
hold offThe feasible set is the intersection:
x >= 0
y >= 0
2*x + y <= 8
x + 2*y <= 8For a bounded two-variable linear program, the resulting feasible region is a polygon.
The API is not restricted to straight lines or half-planes.
syms x y
constraints = [
x^2 + y^2 <= 9
y >= x
y >= -x
];
figure
inequalityplot(constraints,[-4 4],[-4 4], ...
Resolution=800, ...
FaceAlpha=0.3, ...
FaceColor=[0.85 0.33 0.10])
axis equal
grid onThis shades the part of the disk:
x^2 + y^2 <= 9that also satisfies:
y >= x
y >= -xAnother example combines a disk with a parabola:
syms x y
constraints = [
x^2 + y^2 <= 16
y >= x^2 - 3
];
figure
inequalityplot(constraints,[-5 5],[-5 5])
axis equal
grid onNon-strict inequalities such as <= and >= are drawn with solid boundary
curves. Strict inequalities such as < and > are drawn with dashed boundary
curves.
syms x y
figure
inequalityplot([x > 0, y < 3, y >= x],[-1 5],[-1 5])
axis equal
grid onEarlier versions of inequalityplot accepted function handles such as:
ellipse = @(x,y) x.^2/4 + y.^2 <= 1;
inequalityplot(ellipse,[-3 3],[-3 3])The current version uses symbolic inequalities instead:
syms x y
inequalityplot(x^2/4 + y^2 <= 1,[-3 3],[-3 3])For multiple inequalities, the previous API required combining logical conditions manually inside one function handle. The new API accepts a list of symbolic inequalities:
syms x y
constraints = [
x > 0
x < 1
y > 0
y < x
];
inequalityplot(constraints,[-1 2],[-1 2])This makes the plotted system closer to its mathematical notation and allows the function to extract and draw the boundary of each inequality automatically.
Ildeberto de los Santos Ruiz, idelossantos@ittg.edu.mx