Skip to content

Latest commit

 

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

inequalityplot

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.

Requirements

  • MATLAB R2022b or later
  • Symbolic Math Toolbox

API

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 <= 5

Name-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.Boundary

Basic Example

syms x y

figure
inequalityplot(y <= 2*x + 1,[-3 3],[-4 7])
axis equal
grid on

This plots the half-plane satisfying:

y <= 2*x + 1

Systems of Inequalities

Several 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 on

Linear Programming Feasible Regions

The 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 off

The feasible set is the intersection:

x >= 0
y >= 0
2*x + y <= 8
x + 2*y <= 8

For a bounded two-variable linear program, the resulting feasible region is a polygon.

Nonlinear Inequalities

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 on

This shades the part of the disk:

x^2 + y^2 <= 9

that also satisfies:

y >= x
y >= -x

Another 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 on

Strict and Non-Strict Inequalities

Non-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 on

API Change

Earlier 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.

Author

Ildeberto de los Santos Ruiz, idelossantos@ittg.edu.mx

View inequalityplot on File Exchange

About

Plot inequalities in 2-D space

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages