CVX Usrguide
CVX Usrguide
CVX Usrguide
or
1
norms can be reformulated as LPs, and solved
using a linear programming solver such as linprog in the Matlab Optimization Tool-
box (see, e.g., [BV04, 6.1]). However, because these norms are part of cvxs base
library of functions, cvx can handle these problems directly.
For example, to nd the value of x that minimizes the Chebyshev norm|Axb|
,
we can employ the linprog command from the Matlab Optimization Toolbox:
97 f = [ zeros(n,1); 1 ];
98 Ane = [ +A, -ones(m,1) ; ...
99 -A, -ones(m,1) ];
100 bne = [ +b; -b ];
101 xt = linprog(f,Ane,bne);
102 x_cheb = xt(1:n,:);
With cvx, the same problem is specied as follows:
11
108 cvx_begin
109 variable x(n);
110 minimize( norm(A*x-b,Inf) );
111 cvx_end
The code based on linprog, and the cvx specication above will both solve the
Chebyshev norm minimization problem, i.e., each will produce an x that minimizes
|Axb|
.
Similarly, to minimize the
1
norm | |
1
, we can use linprog as follows:
139 f = [ zeros(n,1); ones(m,1); ones(m,1) ];
140 Aeq = [ A, eye(m), +eye(m) ];
141 lb = [ -Inf(n,1); zeros(m,1); zeros(m,1) ];
142 xzz = linprog(f,[],[],Aeq,b,lb,[]);
143 x_l1 = xzz(1:n,:);
The cvx version is, not surprisingly,
149 cvx_begin
150 variable x(n);
151 minimize( norm(A*x-b,1) );
152 cvx_end
cvx automatically transforms both of these problems into LPs, not unlike those gen-
erated manually for linprog.
The advantage that automatic transformation provides is magnied if we consider
functions (and their resulting transformations) that are less well-known than the
and
1
norms. For example, consider the norm
|Ax b|
lgst,k
= [Ax b[
[1]
+ +[Ax b[
[k]
,
where [Ax b[
[i]
denotes the ith largest element of the absolute values of the entries
of Ax b. This is indeed a norm, albeit a fairly esoteric one. (When k = 1, it
reduces to the
m
i=1
((Ax b)
i
),
with variable x R
n
, where is the Huber penalty function
(z) =
_
[z[
2
[z[ 1
2[z[ 1 [z[ 1.
The Huber penalty function is convex, and has been provided in the cvx function
library. So solving the Huber penalty minimization problem in cvx is simple:
204 cvx_begin
205 variable x(n);
206 minimize( sum(huber(A*x-b)) );
207 cvx_end
cvx automatically transforms this problem into an SOCP, which the core solver then
solves. (The cvx user, however, does not need to know how the transformation is
carried out.)
2.4 Other constraints
We hope that, by now, it is not surprising that adding the simple bounds l _ x _ u
to the problems in 2.3 above is as simple as inserting the lines
x >= l;
x <= u;
before the cvx_end statement in each cvx specication. In fact, cvx supports more
complex constraints as well. For example, let us dene new matrices C and d in
Matlab as follows,
227 p = 4;
228 C = randn(p,n);
229 d = randn(p,1);
13
Now let us add an equality constraint and a nonlinear inequality constraint to the
original least-squares problem:
232 cvx_begin
233 variable x(n);
234 minimize( norm(A*x-b) );
235 subject to
236 C*x == d;
237 norm(x,Inf) <= 1;
238 cvx_end
Both of the added constraints conform to the DCP rules, and so are accepted by cvx.
After the cvx_end command, cvx converts this problem to an SOCP, and solves it.
Expressions using comparison operators (==, >=, etc.) behave quite dierently
when they involve cvx optimization variables, or expressions constructed from cvx
optimization variables, than when they involve simple numeric values. For example,
because x is a declared variable, the expression C*x==d in line 236 above causes a
constraint to be included in the cvx specication, and returns no value at all. On the
other hand, outside of a cvx specication, if x has an appropriate numeric value
for example immediately after the cvx_end commandthat same expression would
return a vector of 1s and 0s, corresponding to the truth or falsity of each equality.
2
Likewise, within a cvx specication, the statement norm(x,Inf)<=1 adds a nonlinear
constraint to the specication; outside of it, it returns a 1 or a 0 depending on the
numeric value of x (specically, whether its
b
)
Figure 1: An example trade-o curve from the quickstart demo, lines 268-286.
norm(A*x-b)+gamma(k)*norm(x,1)
on line 277 is recognized as convex by cvx, as long as gamma(k) is positive or zero. If
gamma(k) were negative, then this expression becomes the sum of a convex term and
a concave term, which causes cvx to generate the following error:
??? Error using ==> cvx.plus
Disciplined convex programming error:
Addition of convex and concave terms is forbidden.
16
3 The basics
3.1 Data types for variables
As mentioned above, all variables must be declared using the variable command
(or the variables command; see below) before they can be used in constraints or an
objective function.
Variables can be real or complex; and scalar, vector, matrix, or n-dimensional
arrays. In addition, matrices can have structure as well, such as symmetry or band-
edness. The structure of a variable is given by supplying a list of descriptive keywords
after the name and size of the variable. For example, the code segment
variable w(50) complex;
variable X(20,10);
variable Y(50,50) symmetric;
variable Z(100,100) hermitian toeplitz;
(inside a cvx specication) declares that w is a complex 50-element vector variable, X
is a real 2010 matrix variable, Y is a real 5050 symmetric matrix variable, and Z is
a complex 100100 Hermitian Toeplitz matrix variable. The structure keywords can
be applied to n-dimensional arrays as well: each 2-dimensional slice of the array is
given the stated structure. The currently supported structure keywords are:
banded(lb,ub) complex diagonal hankel hermitian lower_bidiagonal
lower_hessenberg lower_triangular scaled_identity skew_symmetric
symmetric toeplitz tridiagonal upper_bidiagonal upper_hankel
upper_hessenberg upper_triangular
With a couple of exceptions, the structure keywords are self-explanatory:
banded(lb,ub): the matrix is banded with a lower bandwidth lb and an upper
bandwidth ub. If both lb and ub are zero, then a diagonal matrix results. ub
can be omitted, in which case it is set equal to lb. For example, banded(1,1)
(or banded(1)) is a tridiagonal matrix.
scaled_identity: the matrix is a (variable) multiple of the identity matrix.
This is the same as declaring it to be diagonal and Toeplitz.
upper_hankel: The matrix is Hankel (i.e., constant along antidiagonals), and
zero below the central antidiagonal, i.e., for i + j > n + 1.
When multiple keywords are supplied, the resulting matrix structure is determined
by intersection; if the keywords conict, then an error will result.
A variable statement can be used to declare only a single variable, which can
be a bit inconvenient if you have a lot of variables to declare. For this reason, the
variables statement is provided which allows you to declare multiple variables; i.e.,
variables x1 x2 x3 y1(10) y2(10,10,10);
The one limitation of the variables command is that it cannot declare complex or
structured arrays (e.g., symmetric, etc.). These must be declared one at a time, using
the singular variable command.
17
3.2 Objective functions
Declaring an objective function requires the use of the minimize or maximize func-
tion, as appropriate. The objective function in a call to minimize must be convex;
the objective function in a call to maximize must be concave. At most one objective
function may be declared in a given cvx specication, and the objective function must
have a scalar value. (For the only exception to this rule, see the section on dening
new functions in 5).
If no objective function is specied, the problem is interpreted as a feasibility
problem, which is the same as performing a minimization with the objective function
set to zero. In this case, cvx_optval is either 0, if a feasible point is found, or +Inf,
if the constraints are not feasible.
3.3 Constraints
The following constraint types are supported in cvx:
Equality == constraints, where both the left- and right-hand sides are ane
functions of the optimization variables.
Less-than <=, < inequality constraints, where the left-hand expression is convex,
and the right-hand expression is concave.
Greater-than >=, > constraints, where the left-hand expression is concave, and
the right-hand expression is convex.
In cvx, the strict inequalities < and > are accepted, but interpreted as the associated
nonstrict inequalities, <= and >=, respectively. We encourage you to use the nonstrict
forms <= and >=, since they are mathematically correct. (Future versions of cvx
might assign a slightly dierent meaning to strict inequalities.)
These equality and inequality operators work for arrays. When both sides of
the constraint are arrays of the same size, the constraint is imposed elementwise. For
example, if a and b are mn matrices, then a<=b is interpreted by cvx as mn (scalar)
inequalities, i.e., each entry of a must be less than or equal to the corresponding entry
of b. cvx also handles cases where one side is a scalar and the other is an array. This
is interpreted as a constraint for each element of the array, with the (same) scalar
appearing on the other side. As an example, if a is an m n matrix, then a>=0 is
interpreted as mn inequalities: each element of the matrix must be nonnegative.
Note also the important distinction between =, which is an assignment, and ==,
which imposes an equality constraint (inside a cvx specication); for more on this
distinction, see 8.4. Also note that the non-equality operator ~= may not be used in
a constraint; in any case, such constraints are rarely convex. Inequalities cannot be
used if either side is complex.
cvx also supports a set membership constraint; see 3.5.
18
3.4 Functions
The base cvx function library includes a variety of convex, concave, and ane func-
tions which accept cvx variables or expressions as arguments. Many are common
Matlab functions such as sum, trace, diag, sqrt, max, and min, re-implemented as
needed to support cvx; others are new functions not found in Matlab. A complete
list of the functions in the base library can be found in B. Its also possible to add
your own new functions; see 5.
An example of a function in the base library is quad_over_lin, which represents
the quadratic-over-linear function, dened as f(x, y) = x
T
x/y, with domain R
n
R
++
, i.e., x is an arbitrary vector in R
n
, and y is a positive scalar. (The function also
accepts complex x, but well consider real x to keep things simple.) The quadratic-
over-linear function is convex in x and y, and so can be used as an objective, in an
appropriate constraint, or in a more complicated expression. We can, for example,
minimize the quadratic-over-linear function of (Ax b, c
T
x + d) using
minimize( quad_over_lin( A*x-b, c*x+d ) );
inside a cvx specication, assuming x is a vector optimization variable, A is a matrix,
b and c are vectors, and d is a scalar. cvx recognizes this objective expression as a
convex function, since it is the composition of a convex function (the quadratic-over-
linear function) with an ane function.
You can also use the function quad_over_lin outside a cvx specication. In
this case, it just computes its (numerical) value, given (numerical) arguments. Its
not quite the same as the expression ((A*x-b)*(A*x-b))/(c*x+d), however. This
expression makes sense, and returns a real number, when c
T
x + d is negative; but
quad_over_lin(A*x-b,c*x+d) returns +Inf if c
T
x + d ,> 0.
3.5 Sets
cvx supports the denition and use of convex sets. The base library includes the cone
of positive semidenite nn matrices, the second-order or Lorentz cone, and various
norm balls. A complete list of sets supplied in the base library is given in B.
Unfortunately, the Matlab language does not have a set membership operator,
such as x in S, to denote x S. So in cvx, we use a slightly dierent syntax to
require that an expression is in a set. To represent a set we use a function that
returns an unnamed variable that is required to be in the set. Consider, for example,
S
n
+
, the cone of symmetric positive semidenite n n matrices. In cvx, we represent
this by the function semidefinite(n), which returns an unnamed new variable, that
is constrained to be positive semidenite. To require that the matrix expression X
be symmetric positive semidenite, we use the syntax X == semidefinite(n). The
literal meaning of this is that X is constrained to be equal to some unnamed variable,
which is required to be an n n symmetric positive semidenite matrix. This is, of
course, equivalent to saying that X must be symmetric positive semidenite.
As an example, consider the constraint that a (matrix) variable X is a correlation
matrix, i.e., it is symmetric, has unit diagonal elements, and is positive semidenite.
In cvx we can declare such a variable and impose such constraints using
19
variable X(n,n) symmetric;
X == semidefinite(n);
diag(X) == ones(n,1);
The second line here imposes the constraint that X be positive semidenite. (You can
read == here as is, so the second line can be read as X is positive semidenite.)
The lefthand side of the third line is a vector containing the diagonal elements of X,
whose elements we require to be equal to one. Incidentally, cvx allows us to simplify
the third line to
diag(X) == 1;
because cvx follows the Matlab convention of handling array/scalar comparisons by
comparing each element of the array independently with the scalar.
Sets can be combined in ane expressions, and we can constrain an ane expres-
sion to be in a convex set. For example, we can impose constraints of the form
A*X*A-X == B*semidefinite(n)*B;
where X is an n n symmetric variable matrix, and A and B are n n constant
matrices. This constraint requires that AXA
T
X = BY B
T
, for some Y S
n
+
.
cvx also supports sets whose elements are ordered lists of quantities. As an ex-
ample, consider the second-order or Lorentz cone,
Q
m
= (x, y) R
m
R [ |x|
2
y = epi | |
2
, (2)
where epi denotes the epigraph of a function. An element of Q
m
is an ordered list,
with two elements: the rst is an m-vector, and the second is a scalar. We can use this
cone to express the simple least-squares problem from 2.1 (in a fairly complicated
way) as follows:
minimize y
subject to (Ax b, y) Q
m
.
(3)
cvx uses Matlabs cell array facility to mimic this notation:
cvx_begin
variables x(n) y;
minimize( y );
subject to
{ A*x-b, y } == lorentz(m);
cvx_end
The function call lorentz(m) returns an unnamed variable (i.e., a pair consisting of
a vector and a scalar variable), constrained to lie in the Lorentz cone of length m. So
the constraint in this specication requires that the pair { A*x-b, y } lies in the
appropriately-sized Lorentz cone.
20
3.6 Dual variables
When a disciplined convex program is solved, the associated dual problem is also
solved. (In this context, the original problem is called the primal problem.) The
optimal dual variables, each of which is associated with a constraint in the original
problem, give valuable information about the original problem, such as the sensitiv-
ities with respect to perturbing the constraints [BV04, Ch.5]. To get access to the
optimal dual variables in cvx, you simply declare them, and associate them with the
constraints. Consider, for example, the LP
minimize c
T
x
subject to Ax _ b,
with variable x R
n
, and m inequality constraints. The dual of this problem is
maximize b
T
y
subject to c + A
T
y = 0
y _ 0,
where the dual variable y is associated with the inequality constraint Ax _ b in the
original LP. To represent the primal problem and this dual variable in cvx, we use
the following syntax:
n = size(A,2);
cvx_begin
variable x(n);
dual variable y;
minimize( c * x );
subject to
y : A * x <= b;
cvx_end
The line
dual variable y
tells cvx that y will represent the dual variable, and the line
y : A * x <= b;
associates it with the inequality constraint. Notice how the colon : operator is being
used in a dierent manner than in standard Matlab, where it is used to construct
numeric sequences like 1:10. This new behavior is in eect only when a dual variable
is present, so there should be no confusion or conict. No dimensions are given for
y; they are automatically determined from the constraint with which it is associated.
For example, if m = 20, typing y at the Matlab command prompt immediately before
cvx_end yields
y =
cvx dual variable (20x1 vector)
21
It is not necessary to place the dual variable on the left side of the constraint; for
example, the line above can also be written in this way:
A * x <= b : y;
In addition, dual variables for inequality constraints will always be nonnegative, which
means that the sense of the inequality can be reversed without changing the dual
variables value; i.e.,
b >= A * x : y;
yields an identical result. For equality constraints, on the other hand, swapping the
left- and right- hand sides of an equality constraint will negate the optimal value of
the dual variable.
After the cvx_end statement is processed, and assuming the optimization was
successful, cvx assigns numerical values to x and ythe optimal primal and dual
variable values, respectively. Optimal primal and dual variables for this LP must
satisfy the complementary slackness conditions
y
i
(b Ax)
i
= 0, i = 1, . . . , m. (4)
You can check this in Matlab with the line
y .* (b-A*x)
which prints out the products of the entries of y and b-A*x, which should be nearly
zero. This line must be executed after the cvx_end command (which assigns nu-
merical values to x and y); it will generate an error if it is executed inside the cvx
specication, where y and b-A*x are still just abstract expressions.
If the optimization is not successful, because either the problem is infeasible or
unbounded, then x and y will have dierent values. In the unbounded case, x will
contain an unbounded direction; i.e., a point x satisfying
c
T
x = 1, Ax _ 0, (5)
and y will be lled with NaN values, reecting the fact that the dual problem is
infeasible. In the infeasible case, x is lled with NaN values, while y contains an
unbounded dual direction; i.e., a point y satisfying
b
T
y = 1, A
T
y = 0, y _ 0 (6)
Of course, the precise interpretation of primal and dual points and/or directions
depends on the structure of the problem. See references such as [BV04] for more on
the interpretation of dual information.
cvx also supports the declaration of indexed dual variables. These prove useful
when the number of constraints in a model (and, therefore, the number of dual
variables) depends upon the parameters themselves. For more information on indexed
dual variables, see 8.5.
22
3.7 Expression holders
Sometimes it is useful to store a cvx expression into a Matlab variable for future use.
For instance, consider the following cvx script:
variables x y
z = 2 * x - y;
square( z ) <= 3;
quad_over_lin( x, z ) <= 1;
The construction z = 2 * x - y is not an equality constraint; it is an assignment.
It is storing an intermediate calculation 2 * x - y, which is an ane expression,
which is then used later in two dierent constraints. We call z an expression holder
to dierentiate it from a formally declared cvx variable. For more on the critical
dierences between assignment and equality, see Section 8.4.
Often it will be useful to accumulate an array of expressions into a single Matlab
variable. Unfortunately, a somewhat technical detail of the Matlab object model can
cause problems in such cases. Consider this construction:
variable u(9);
x(1) = 1;
for k = 1 : 9,
x(k+1) = sqrt( x(k) + u(k) );
end
This seems reasonable enough: x should be a vector whose rst value is 1, and whose
subsequent values are concave cvx expressions. But if you try this in a cvx model,
Matlab will give you a rather cryptic error:
??? The following error occurred converting from cvx to double:
Error using ==> double
Conversion to double from cvx is not possible.
The reason this occurs is that the Matlab variable x is initialized as a numeric array
when the assignment x(1)=1 is made; and Matlab will not permit cvx objects to be
subsequently inserted into numeric arrays.
The solution is to explicitly declare x to be an expression holder before assigning
values to it. We have provided keywords expression and expressions for just this
purpose, for declaring a single or multiple expression holders for future assignment.
Once an expression holder has been declared, you may freely insert both numeric
and cvx expressions into it. For example, the previous example can be corrected as
follows:
variable u(9);
expression x(10);
x(1) = 1;
for k = 1 : 9,
x(k+1) = sqrt( x(k) + u(k) );
end
23
cvx will accept this construction without error. You can then use the concave ex-
pressions x(1), . . . , x(10) in any appropriate ways; for example, you could maximize
x(10).
The dierences between a variable object and an expression object are quite
signicant. A variable object holds an optimization variable, and cannot be over-
written or assigned in the cvx specication. (After solving the problem, however, cvx
will overwrite optimization variables with optimal values.) An expression object, on
the other hand, is initialized to zero, and should be thought of as a temporary place
to store cvx expressions; it can be assigned to, freely re-assigned, and overwritten in
a cvx specication.
Of course, as our rst example shows, it is not always necessary to declare an
expression holder before it is created or used. But doing so provides an extra measure
of clarity to models, so we strongly recommend it.
24
4 The DCP ruleset
cvx enforces the conventions dictated by the disciplined convex programming ruleset,
or DCP ruleset for short. cvx will issue an error message whenever it encounters
a violation of any of the rules, so it is important to understand them before begin-
ning to build models. The rules are drawn from basic principles of convex analysis,
and are easy to learn, once youve had an exposure to convex analysis and convex
optimization.
The DCP ruleset is a set of sucient, but not necessary, conditions for convexity.
So it is possible to construct expressions that violate the ruleset but are in fact
convex. As an example consider the entropy function,
n
i=1
x
i
log x
i
, dened for
x > 0, which is concave. If it is expressed as
- sum( x .* log( x ) )
cvx will reject it, because its concavity does not follow from any of the composition
rules. (Specically, it violates the no-product rule described in 4.4.) Problems
involving entropy, however, can be solved, by explicitly using the entropy function,
sum(entr( x ))
which is in the base cvx library, and thus recognized as concave by cvx. If a convex
(or concave) function is not recognized as convex or concave by cvx, it can be added
as a new atom; see 5.
As another example consider the function
x
2
+ 1 = |[x 1]|
2
, which is convex. If
it is written as
norm([x 1])
(assuming x is a scalar variable or ane expression) it will be recognized by cvx
as a convex expression, and therefore can be used in (appropriate) constraints and
objectives. But if it is written as
sqrt(x^2+1)
cvx will reject it, since convexity of this function does not follow from the cvx ruleset.
4.1 A taxonomy of curvature
In disciplined convex programming, a scalar expression is classied by its curvature.
There are four categories of curvature: constant, ane, convex, and concave. For a
function f : R
n
R dened on all R
n
, the categories have the following meanings:
constant: f(x + (1 )y) = f(x) x, y R
n
, R
ane: f(x + (1 )y) = f(x) + (1 )f(y) x, y R
n
, R
convex: f(x + (1 )y) f(x) + (1 )f(y) x, y R
n
, [0, 1]
concave: f(x + (1 )y) f(x) + (1 )f(y) x, y R
n
, [0, 1]
25
Of course, there is signicant overlap in these categories. For example, constant
expressions are also ane, and (real) ane expressions are both convex and concave.
Convex and concave expressions are real by denition. Complex constant and
ane expressions can be constructed, but their usage is more limited; for example,
they cannot appear as the left- or right-hand side of an inequality constraint.
4.2 Top-level rules
cvx supports three dierent types of disciplined convex programs:
A minimization problem, consisting of a convex objective function and zero or
more constraints.
A maximization problem, consisting of a concave objective function and zero or
more constraints.
A feasibility problem, consisting of one or more constraints.
4.3 Constraints
Three types of constraints may be specied in disciplined convex programs:
An equality constraint, constructed using ==, where both sides are ane.
A less-than inequality constraint, using either <= or <, where the left side is
convex and the right side is concave.
A greater-than inequality constraint, using either >= or >, where the left side is
concave and the right side is convex.
Non-equality constraints, constructed using ~=, are never allowed. (Such constraints
are not convex.)
One or both sides of an equality constraint may be complex; inequality constraints,
on the other hand, must be real. A complex equality constraint is equivalent to two
real equality constraints, one for the real part and one for the imaginary part. An
equality constraint with a real side and a complex side has the eect of constraining
the imaginary part of the complex side to be zero.
As discussed in 3.5 above, cvx enforces set membership constraints (e.g., x S)
using equality constraints. The rule that both sides of an equality constraint must
be ane applies to set membership constraints as well. In fact, the returned value of
set atoms like semidefinite() and lorentz() is ane, so it is sucient to simply
verify the remaining portion of the set membership constraint. For composite values
like { x, y }, each element must be ane.
In this version, strict inequalities <, > are interpreted identically to nonstrict
inequalities >=, <=. Eventually cvx will ag strict inequalities so that they can be
veried after the optimization is carried out.
26
4.4 Expression rules
So far, the rules as stated are not particularly restrictive, in that all convex programs
(disciplined or otherwise) typically adhere to them. What distinguishes disciplined
convex programming from more general convex programming are the rules governing
the construction of the expressions used in objective functions and constraints.
Disciplined convex programming determines the curvature of scalar expressions
by recursively applying the following rules. While this list may seem long, it is for
the most part an enumeration of basic rules of convex analysis for combining convex,
concave, and ane forms: sums, multiplication by scalars, and so forth.
A valid constant expression is
any well-formed Matlab expression that evaluates to a nite value.
A valid ane expression is
a valid constant expression;
a declared variable;
a valid call to a function in the atom library with an ane result;
the sum or dierence of ane expressions;
the product of an ane expression and a constant.
A valid convex expression is
a valid constant or ane expression;
a valid call to a function in the atom library with a convex result;
an ane scalar raised to a constant power p 1, p ,= 3, 5, 7, 9, ...;
a convex scalar quadratic form (4.8);
the sum of two or more convex expressions;
the dierence between a convex expression and a concave expression;
the product of a convex expression and a nonnegative constant;
the product of a concave expression and a nonpositive constant;
the negation of a concave expression.
A valid concave expression is
a valid constant or ane expression;
a valid call to a function in the atom library with a concave result;
a concave scalar raised to a power p (0, 1);
a concave scalar quadratic form (4.8);
the sum of two or more concave expressions;
27
the dierence between a concave expression and a convex expression;
the product of a concave expression and a nonnegative constant;
the product of a convex expression and a nonpositive constant;
the negation of a convex expression.
If an expression cannot be categorized by this ruleset, it is rejected by cvx. For matrix
and array expressions, these rules are applied on an elementwise basis. We note that
the set of rules listed above is redundant; there are much smaller, equivalent sets of
rules.
Of particular note is that these expression rules generally forbid products between
nonconstant expressions, with the exception of scalar quadratic forms (see 4.8 below).
For example, the expression x*sqrt(x) happens to be a convex function of x, but
its convexity cannot be veried using the cvx ruleset, and so is rejected. (It can be
expressed as x^(3/2) or pow_p(x,3/2), however.) We call this the no-product rule,
and paying close attention to it will go a long way to insuring that the expressions
you construct are valid.
4.5 Functions
In cvx, functions are categorized in two attributes: curvature (constant, ane, con-
vex, or concave) and monotonicity (nondecreasing, nonincreasing, or nonmonotonic).
Curvature determines the conditions under which they can appear in expressions ac-
cording to the expression rules given in 4.4 above. Monotonicity determines how
they can be used in function compositions, as we shall see in 4.6 below.
For functions with only one argument, the categorization is straightforward. Some
examples are given in the table below.
Function Meaning Curvature Monotonicity
sum( x )
i
x
i
ane nondecreasing
abs( x ) [x[ convex nonmonotonic
sqrt( x )
x concave nondecreasing
Following standard practice in convex analysis, convex functions are interpreted
as + when the argument is outside the domain of the function, and concave func-
tions are interpreted as when the argument is outside its domain. In other
words, convex and concave functions in cvx are interpreted as their extended-valued
extensions.
This has the eect of automatically constraining the argument of a function to be
in the functions domain. For example, if we form sqrt(x+1) in a cvx specication,
where x is a variable, then x will automatically be constrained to be larger than or
equal to 1. There is no need to add a separate constraint, x>=-1, to enforce this.
Monotonicity of a function is determined in the extended sense, i.e., including the
values of the argument outside its domain. For example, sqrt(x) is determined to be
nondecreasing since its value is constant () for negative values of its argument;
then jumps up to 0 for argument zero, and increases for positive values of its argument.
28
cvx does not consider a function to be convex or concave if it is so only over
a portion of its domain, even if the argument is constrained to lie in one of these
portions. As an example, consider the function 1/x. This function is convex for x > 0,
and concave for x < 0. But you can never write 1/x in cvx (unless x is constant),
even if you have imposed a constraint such as x>=1, which restricts x to lie in the
convex portion of function 1/x. You can use the cvx function inv_pos(x), dened
as 1/x for x > 0 and otherwise, for the convex portion of 1/x; cvx recognizes this
function as convex and nonincreasing. In cvx, you can express the concave portion
of 1/x, where x is negative, using -inv_pos(-x), which will be correctly recognized
as concave and nonincreasing.
For functions with multiple arguments, curvature is always considered jointly, but
monotonicity can be considered on an argument-by-argument basis. For example,
quad_over_lin( x, y )
_
[x[
2
/y y > 0
+ y 0
convex, nonincreasing in y
is jointly convex in both arguments, but it is monotonic only in its second argument.
In addition, some functions are convex, concave, or ane only for a subset of its
arguments. For example, the function
norm( x, p ) |x|
p
(1 p) convex in x, nonmonotonic
is convex only in its rst argument. Whenever this function is used in a cvx speci-
cation, then, the remaining arguments must be constant, or cvx will issue an error
message. Such arguments correspond to a functions parameters in mathematical
terminology; e.g.,
f
p
(x) : R
n
R, f
p
(x) |x|
p
So it seems tting that we should refer to such arguments as parameters in this
context as well. Henceforth, whenever we speak of a cvx function as being convex,
concave, or ane, we will assume that its parameters are known and have been given
appropriate, constant values.
4.6 Compositions
A basic rule of convex analysis is that convexity is closed under composition with an
ane mapping. This is part of the DCP ruleset as well:
A convex, concave, or ane function may accept an ane expression (of compat-
ible size) as an argument. The result is convex, concave, or ane, respectively.
For example, consider the function square( x ), which is provided in the cvx atom
library. This function squares its argument; i.e., it computes x.*x. (For array ar-
guments, it squares each element independently.) It is in the cvx atom library, and
known to be convex, provided its argument is real. So if x is a real variable of
dimension n, a is a constant n-vector, and b is a constant, the expression
square( a * x + b )
29
is accepted by cvx, which knows that it is convex.
The ane composition rule above is a special case of a more sophisticated compo-
sition rule, which we describe now. We consider a function, of known curvature and
monotonicity, that accepts multiple arguments. For convex functions, the rules are:
If the function is nondecreasing in an argument, that argument must be convex.
If the function is nonincreasing in an argument, that argument must be concave.
If the function is neither nondecreasing or nonincreasing in an argument, that
argument must be ane.
If each argument of the function satises these rules, then the expression is accepted
by cvx, and is classied as convex. Recall that a constant or ane expression is
both convex and concave, so any argument can be ane, including as a special case,
constant.
The corresponding rules for a concave function are as follows:
If the function is nondecreasing in an argument, that argument must be concave.
If the function is nonincreasing in an argument, that argument must be convex.
If the function is neither nondecreasing or nonincreasing in an argument, that
argument must be ane.
In this case, the expression is accepted by cvx, and classied as concave.
For more background on these composition rules, see [BV04, 3.2.4]. In fact, with
the exception of scalar quadratic expressions, the entire DCP ruleset can be thought
of as special cases of these six rules.
Let us examine some examples. The maximum function is convex and nonde-
creasing in every argument, so it can accept any convex expressions as arguments.
For example, if x is a vector variable, then
max( abs( x ) )
obeys the rst of the six composition rules and is therefore accepted by cvx, and
classied as convex.
As another example, consider the sum function, which is both convex and concave
(since it is ane), and nondecreasing in each argument. Therefore the expressions
sum( square( x ) )
sum( sqrt( x ) )
are recognized as valid in cvx, and classied as convex and concave, respectively. The
rst one follows from the rst rule for convex functions; and the second one follows
from the rst rule for concave functions.
Most people who know basic convex analysis like to think of these examples in
terms of the more specic rules: a maximum of convex functions is convex, and a sum
of convex (concave) functions is convex (concave). But these rules are just special
30
cases of the general composition rules above. Some other well known basic rules that
follow from the general composition rules are: a nonnegative multiple of a convex
(concave) function is convex (concave); a nonpositive multiple of a convex (concave)
function is concave (convex).
Now we consider a more complex example in depth. Suppose x is a vector vari-
able, and A, b, and f are constants with appropriate dimensions. cvx recognizes the
expression
sqrt(f*x) + min(4,1.3-norm(A*x-b))
as concave. Consider the term sqrt(f*x). cvx recognizes that sqrt is concave and
f*x is ane, so it concludes that sqrt(f*x) is concave. Now consider the second
term min(4,1.3-norm(A*x-b)). cvx recognizes that min is concave and nondecreas-
ing, so it can accept concave arguments. cvx recognizes that 1.3-norm(A*x-b) is
concave, since it is the dierence of a constant and a convex function. So cvx con-
cludes that the second term is also concave. The whole expression is then recognized
as concave, since it is the sum of two concave functions.
The composition rules are sucient but not necessary for the classication to be
correct, so some expressions which are in fact convex or concave will fail to satisfy
them, and so will be rejected by cvx. For example, if x is a vector variable, the
expression
sqrt( sum( square( x ) ) )
is rejected by cvx, because there is no rule governing the composition of a concave
nondecreasing function with a convex function. Of course, the workaround is simple
in this case: use norm( x ) instead, since norm is in the atom library and known by
cvx to be convex.
4.7 Monotonicity in nonlinear compositions
Monotonicity is a critical aspect of the rules for nonlinear compositions. This has
some consequences that are not so obvious, as we shall demonstrate here by example.
Consider the expression
square( square( x ) + 1 )
where x is a scalar variable. This expression is in fact convex, since (x
2
+ 1)
2
=
x
4
+ 2x
2
+ 1 is convex. But cvx will reject the expression, because the outer square
cannot accept a convex argument. Indeed, the square of a convex function is not, in
general, convex: for example, (x
2
1)
2
= x
4
2x
2
+ 1 is not convex.
There are several ways to modify the expression above to comply with the ruleset.
One way is to write it as x^4 + 2*x^2 + 1, which cvx recognizes as convex, since
cvx allows positive even integer powers using the ^ operator. (Note that the same
technique, applied to the function (x
2
1)
2
, will fail, since its second term is concave.)
Another approach is to use the alternate outer function square_pos, included in
the cvx library, which represents the function (x
+
)
2
, where x
+
= max0, x. Obvi-
ously, square and square_pos coincide when their arguments are nonnegative. But
31
square_pos is nondecreasing, so it can accept a convex argument. Thus, the expres-
sion
square_pos( square( x ) + 1 )
is mathematically equivalent to the rejected version above (since the argument to the
outer function is always positive), but it satises the DCP ruleset and is therefore
accepted by cvx.
This is the reason several functions in the cvx atom library come in two forms:
the natural form, and one that is modied in such a way that it is monotonic, and
can therefore be used in compositions. Other such monotonic extensions include
sum_square_pos and quad_pos_over_lin. If you are implementing a new function
yourself, you might wish to consider if a monotonic extension of that function would
also be useful.
4.8 Scalar quadratic forms
In its original form described in [Gra04, GBY06], the DCP ruleset forbids even the
use of simple quadratic expressions such as x * x (assuming x is a scalar variable).
For practical reasons, we have chosen to make an exception to the ruleset to allow for
the recognition of certain specic quadratic forms that map directly to certain convex
quadratic functions (or their concave negatives) in the cvx atom library:
conj( x ) .* x is replaced with square( x )
y * y is replaced with sum_square( y )
(A*x-b)*Q*(Ax-b) is replaced with quad_form( A * x - b, Q )
cvx detects the quadratic expressions such as those on the left above, and determines
whether or not they are convex or concave; and if so, translates them to an equivalent
function call, such as those on the right above.
cvx examines each single product of ane expressions, and each single squaring
of an ane expression, checking for convexity; it will not check, for example, sums
of products of ane expressions. For example, given scalar variables x and y, the
expression
x ^ 2 + 2 * x * y + y ^2
will cause an error in cvx, because the second of the three terms 2 * x * y, is neither
convex nor concave. But the equivalent expressions
( x + y ) ^ 2
( x + y ) * ( x + y )
will be accepted. cvx actually completes the square when it comes across a scalar
quadratic form, so the form need not be symmetric. For example, if z is a vector
variable, a, b are constants, and Q is positive denite, then
( z + a ) * Q * ( z + b )
32
will be recognized as convex. Once a quadratic form has been veried by cvx, it
can be freely used in any way that a normal convex or concave expression can be, as
described in 4.4.
Quadratic forms should actually be used less frequently in disciplined convex pro-
gramming than in a more traditional mathematical programming framework, where
a quadratic form is often a smooth substitute for a nonsmooth form that one truly
wishes to use. In cvx, such substitutions are rarely necessary, because of its support
for nonsmooth functions. For example, the constraint
sum( ( A * x - b ) .^ 2 ) <= 1
is equivalently represented using the Euclidean norm:
norm( A * x - b ) <= 1
With modern solvers, the second form can be represented using a second-order cone
constraintso the second form may actually be more ecient. So we encourage you to
re-evaluate the use of quadratic forms in your models, in light of the new capabilities
aorded by disciplined convex programming.
33
5 Adding new functions to the cvx atom library
cvx allows new convex and concave functions to be dened and added to the atom
library, in two ways, described in this section. The rst method is simple, and can
(and should) be used by many users of cvx, since it requires only a knowledge of the
basic DCP ruleset. The second method is very powerful, but a bit complicated, and
should be considered an advanced technique, to be attempted only by those who are
truly comfortable with convex analysis, disciplined convex programming, and cvx in
its current state.
Please do let us know if you have implemented a convex or concave function that
you think would be useful to other users; we will be happy to incorporate it in a
future release.
5.1 New functions via the DCP ruleset
The simplest way to construct a new function that works within cvx is to construct it
using expressions that fully conform to the DCP ruleset. To illustrate this, consider
the convex deadzone function, dened as
f(x) = max[x[ 1, 0 =
_
_
_
0 [x[ 1
x 1 x > 1
1 x x < 1
To implement this function in cvx, simply create a le deadzone.m containing
function y = deadzone( x )
y = max( abs( x ) - 1, 0 )
This function works just as you expect it would outside of cvxi.e., when its argu-
ment is numerical. But thanks to Matlabs operator overloading capability, it will
also work within cvx if called with an ane argument. cvx will properly conclude
that the function is convex, because all of the operations carried out conform to the
rules of DCP: abs is recognized as a convex function; we can subtract a constant from
it, and we can take the maximum of the result and 0, which yields a convex function.
So we are free to use deadzone anywhere in a cvx specication that we might use
abs, for example, because cvx knows that it is a convex function.
Let us emphasize that when dening a function this way, the expressions you
use must conform to the DCP ruleset, just as they would if they had been inserted
directly into a cvx model. For example, if we replace max with min above; e.g.,
function y = deadzone_bad( x )
y = min( abs( x ) - 1, 0 )
then the modied function fails to meet the DCP ruleset. The function will work
outside of a cvx specication, happily computing the value min[x[ 1, 0 for a
numerical argument x. But inside a cvx specication, invoked with a nonconstant
argument, it will not work, because it doesnt follow the DCP composition rules.
34
5.2 New functions via partially specied problems
A more advanced method for dening new functions in cvx relies on the following
basic result of convex analysis. Suppose that S R
n
R
m
is a convex set and
g : (R
n
R
m
) (R +) is a convex function. Then
f : R
n
(R +), f(x) inf g(x, y) [ y, (x, y) S (7)
is also a convex function. (This rule is sometimes called the partial minimization
rule.) We can think of the convex function f as the optimal value of a family of
convex optimization problems, indexed or parametrized by x,
minimize g(x, y)
subject to (x, y) S
with optimization variable y.
One special case should be very familar: if m = 1 and g(x, y) y, then
f(x) inf y [ y, (x, y) S
gives the classic epigraph representation of f:
epi f = S + (0 R
+
) ,
where 0 R
n
.
In cvx you can dene a convex function in this very manner, that is, as the optimal
value of a parameterized family of disciplined convex programs. We call the under-
lying convex program in such cases an incomplete specicationso named because
the parameters (that is, the function inputs) are unknown when the specication is
constructed. The concept of incomplete specications can at rst seem a bit compli-
cated, but it is very powerful mechanism that allows cvx to support a wide variety
of functions.
Let us look at an example to see how this works. Consider the unit-halfwidth
Huber penalty function h(x):
h : R R, h(x)
_
x
2
[x[ 1
2[x[ 1 [x[ 1.
(8)
We can express the Huber function in terms of the following family of convex QPs,
parameterized by x:
minimize 2v + w
2
subject to [x[ v + w
w 1,
(9)
with scalar variables v and w. The optimal value of this simple QP is equal to the
Huber penalty function of x. We note that the objective and constraint functions in
this QP are (jointly) convex in v, w and x.
We can implement the Huber penalty function in cvx as follows:
35
function cvx_optval = huber( x )
cvx_begin
variables w v;
minimize( w^2 + 2 * v );
subject to
abs( x ) <= w + v;
w <= 1;
cvx_end
If huber is called with a numeric value of x, then upon reaching the cvx_end state-
ment, cvx will nd a complete specication, and solve the problem to compute the
result. cvx places the optimal objective function value into the variable cvx_optval,
and function returns that value as its output. Of course, its very inecient to com-
pute the Huber function of a numeric value x by solving a QP. But it does give the
correct value (up to the core solver accuracy).
What is most important, however, is that if huber is used within a cvx specica-
tion, with an ane cvx expression for its argument, then cvx will do the right thing.
In particular, cvx will recognize the Huber function, called with ane argument, as
a valid convex expression. In this case, the function huber will contain a special
Matlab object that represents the function call in constraints and objectives. Thus
the function huber can be used anywhere a traditional convex function can be used,
in constraints or objective functions, in accordance with the DCP ruleset.
There is a corresponding development for concave functions as well. Given a
convex set S as above, and a concave function g : (R
n
R
m
) (R ), the
function
f : R (R ), f(x) sup g(x, y) [ y, (x, y) S (10)
is concave. If g(x, y) y, then
f(x) sup y [ y, (x, y) S (11)
gives the hypograph representation of f:
hypo f = S R
n
+
.
In cvx, a concave incomplete specication is simply one that uses a maximize ob-
jective instead of a minimize objective; and if properly constructed, it can be used
anywhere a traditional concave function can be used within a cvx specication.
For an example of a concave incomplete specication, consider the function
f : R
nn
R, f(X) =
min
(X + X
T
) (12)
Its hypograph can be represented using a single linear matrix inequality:
hypo f = (X, t) [ f(X) t =
_
(X, t) [ X + X
T
tI _ 0
_
(13)
So we can implement this function in cvx as follows:
36
function cvx_optval = lambda_min_symm( X )
n = size( X, 1 );
cvx_begin
variable y;
maximize( y );
subject to
X + X - y * eye( n ) == semidefinite( n );
cvx_end
If a numeric value of X is supplied, this function will return min(eig(X+X)) (to within
numerical tolerances). However, this function can also be used in cvx constraints and
objectives, just like any other concave function in the atom library.
There are two practical issues that arise when dening functions using incomplete
specications, both of which we will illustrate using our huber example above. First of
all, as written the function works only with scalar values. To apply it (elementwise) to
a vector requires that we iterate through the elements in a for loopa very inecient
enterprise, particularly in cvx. A far better approach is to extend the huber function
to handle vector inputs. This is, in fact, rather simple to do: we simply create a
multiobjective version of the problem:
function cvx_optval = huber( x )
sx = size( x );
cvx_begin
variables w( sx ) v( sx );
minimize( w .^ 2 + 2 * v );
subject to
abs( x ) <= w + v;
w <= 1;
cvx_end
This version of huber will in eect create sx instances of the problem in parallel;
and when used in a cvx specication, will be handled correctly.
The second issue is that if the input to huber is numeric, then direct computation
is a far more ecient way to compute the result than solving a QP. (What is more,
the multiobjective version cannot be used with numeric inputs.) One solution is to
place both versions in one le, with an appropriate test to select the proper version
to use:
function cvx_optval = huber( x )
if isnumeric( x ),
xa = abs( x );
flag = xa < 1;
cvx_optval = flag .* xa.^2 + (~flag) * (2*xa-1);
else,
sx = size( x );
cvx_begin
37
variables w( sx ) v( sx );
minimize( w .^ 2 + 2 * v );
subject to
abs( x ) <= w + v;
w <= 1;
cvx_end
end
Alternatively, you can create two separate versions of the function, one for numeric
input and one for cvx expressions, and place the numeric version in a subdirectory
called @double. Matlab will call the @double version only when its arguments are
numeric, and it will call your cvx version in other cases. This is the approach taken
for the version of huber found in the cvx atom library.
One good way to learn more about using incomplete specications is to examine
some of the examples already in the cvx atom library. Good choices include huber,
inv_pos, lambda_min, lambda_max, matrix_frac, quad_over_lin, sum_largest,
and others. Some are a bit dicult to read because of diagnostic or error-checking
code, but these are relatively simple.
38
6 Semidenite programming using cvx
Those who are familiar with semidenite programming (SDP) know that the con-
straints that utilize the set semidefinite(n) in 3.5 above are, in practice, typi-
cally expressed using linear matrix inequality (LMI) notation. For example, given
X = X
T
R
nn
, the constraint X _ 0 denotes that X S
n
+
; that is, that X is
positive semidenite.
cvx provides a special SDP mode which allows this LMI convention to be employed
inside cvx models using Matlabs standard inequality operators >=, <=, etc.. In order
to use it, one must simply begin a model with the statement cvx_begin sdp or
cvx_begin SDP instead of simply cvx_begin. When SDP mode is engaged, cvx
interprets certain inequality constraints in a dierent manner. To be specic:
Equality constraints are interpreted the same (i.e., elementwise).
Inequality constraints involving vectors and scalars are interpreted the same;
i.e., elementwise.
Inequality constraints involving non-square matrices are disallowed; attempt-
ing to use them causes an error. If you wish to do true elementwise com-
parison of matrices X and Y, use a vectorization operation X(:) <= Y(:) or
vec( X ) <= vec( Y ). (vec is a function provided by cvx that is equivalent
to the colon operation.)
Inequality constraints involving real, square matrices are interpreted as follows:
X >= Y and X > Y become X - Y == semidefinite(n)
X <= Y and X < Y become Y - X == semidefinite(n)
If either side is complex, then the inequalities are interpreted as follows:
X >= Y and X > Y become X - Y == hermitian_semidefinite(n)
X <= Y and X < Y become Y - X == hermitian_semidefinite(n)
In the above, n=max(size(X,1),size(Y,1)).
There is one additional restriction: both X and Y must be the same size, or one
must be the scalar zero. For example, if X and Y are matrices of size n,
X >= 1 or 1 >= Y illegal
X >= ones(n,n) or ones(n,n) >= Y legal
X >= 0 or 0 >= Y legal
In eect, cvx enforces a stricter interpretation of the inequality operators for
LMI constraints.
39
Note that LMI constraints enforce symmetry (real or Hermitian, as appropriate)
on their inputs. Unlike SDPSOL [WB00], cvx does not extract the symmetric
part for you: you must take care to insure symmetry yourself. Since cvx sup-
ports the declaration of symmetric matrices, this is reasonably straightforward.
If cvx cannot determine that an LMI is symmetric, a warning will be issued.
A dual variable, if supplied, will be applied to the converted equality constraint.
It will be given a positive semidenite value if an optimal point is found.
So, for example, the cvx model found in the le examples/closest_toeplitz_sdp.m,
cvx_begin
variable Z(n,n) hermitian toeplitz
dual variable Q
minimize( norm( Z - P, fro ) )
Z == hermitian_semidefinite( n ) : Q;
cvx_end
can also be written as follows:
cvx_begin sdp
variable Z(n,n) hermitian toeplitz
dual variable Q
minimize( norm( Z - P, fro ) )
Z >= 0 : Q;
cvx_end
Many other examples in the cvx example library utilize semidenite constraints; and
all of them use SDP mode. To nd them, simply search for the text cvx_begin sdp
in the examples/ subdirectory tree using your favorite le search tool. One of these
examples is reproduced in 8.5.
Since semidenite programming is popular, some may wonder why SDP mode is
not the default behavior. The reason for this is that we place a strong emphasis
on maintaining consistency between Matlabs native behavior and that of cvx; and
the use of the >=, <=, >, < operators to create LMIs represents a deviation from
that ideal. For example, the expression Z >= 0 in the example above constrains
the variable Z to be positive semidenite. But after the model has been solved and
Z has been replaced with a numeric value, the expression Z >= 0 will test for the
elementwise nonnegativity of Z. To verify that the numeric value of Z is, in fact,
positive semidenite, you must perform a test like min(eig(Z)) >= 0.
40
7 Geometric programming using cvx
Geometric programs (GPs) are special mathematical programs that can be converted
to convex form using a change of variables. The convex form of GPs can be expressed
as DCPs, but cvx also provides a special mode that allows a GP to be specied in
its native form. cvx will automatically perform the necessary conversion, compute
a numerical solution, and translate the results back to the original problem. For a
tutorial on geometric programming, we refer the reader to [BKVH05].
To utilize GP mode, you must begin your cvx specication with the command
cvx_begin gp or cvx_begin GP instead of simply cvx_begin. For example, the
following code, found in the example library at gp/max_volume_box.m, determines
the maximum volume box subject to various area and ratio constraints:
cvx_begin gp
variables w h d
maximize( w * h * d )
subject to
2*(h*w+h*d) <= Awall;
w*d <= Afloor;
h/w >= alpha;
h/w <= beta;
d/w >= gamma;
d/w <= delta;
cvx_end
As the example illustrates, cvx supports the construction of monomials and posyn-
omials using addition, multiplication, division (when appropriate), and powers. In
addition, cvx supports the construction of generalized geometric programs (GGPs),
by permitting the use of generalized posynomials wherever posynomials are permitted
in standard GP [BKVH05].
The solvers used in this version of cvx do not support geometric programming
natively. Instead, they are solved using the successive approximation technique de-
scribed in Appendix D.1. This means that solving GPs can be slow, but for small
and medium sized problems, the method works well.
In the remainder of this section, we will describe specic rules that apply when
constructing models in GP mode.
7.1 Top-level rules
cvx supports three types of geometric programs:
A minimization problem, consisting of a generalized posynomial objective and
zero or more constraints.
A maximization problem, consisting of a monomial objective and zero or more
constraints.
41
A feasibility problem, consisting of one or more constraints.
The asymmetry between minimizations and maximizationsspecically, that only
monomial objectives are allowed in the latteris an unavoidable artifact of the ge-
ometry of GPs and GGPs.
7.2 Constraints
Three types of constraints may be specied in geometric programs:
An equality constraint, consrtucted using ==, where both sides are monomials.
A less-than inequality constraint <=, < where the left side is a generalized posyn-
omial and the right side is a monomial.
A greater-than inequality constraint >=, > where the left side is a monomial and
the right side is a generalized posynomial.
As with DCPs, non-equality constraints are not permitted.
7.3 Expressions
The basic building blocks of generalized geometric programming are monomials,
posynomials, and generalized posynomials. A valid monomial is
a declared variable;
the product of two or more monomials;
the ratio of two monomials;
a monomial raised to a real power; or
a call to one of the following functions with monomial arguments: prod, cumprod,
geo_mean, sqrt.
A valid posynomial expression is
a valid monomial;
the sum of two or more posynomials;
the product of two or more posynomials;
the ratio of a posynomial and a monomial;
a posynomial raised to a positive integral power; or
a call to one of the following functions with posynomial arguments: sum, cumsum,
mean, prod, cumprod.
42
A valid generalized posynomial expression is
a valid posynomial;
the sum of two or more generalized posynomials;
the product of two or more generalized posynomials;
the ratio of a generalized posynomial and a monomial;
a generalized posynomial raised to a positive real power; or
a call to one of the following functions with arguments that are generalized
posynomials: sum, cumsum, mean, prod, cumprod, geo_mean, sqrt, norm,
sum_largest, norm_largest.
It is entirely possible to create and manipulate arrays of monomials, posynomials,
and/or generalized posynomials in cvx, in which case these rules extend in an ob-
vious manner. For example, the product of two monomial matrices produces either
a posynomial matrix or a monomial matrix, depending upon the structure of said
matrices.
43
8 Advanced topics
In this section we describe a number of the more advanced capabilities of cvx. We
recommend that you skip this section at rst, until you are comfortable with the basic
capabilities described above.
8.1 Solver selection
cvx currently supports two solvers: SeDuMi and SDPT3 (the default). To select
SeDuMi as your default solver, simply type
12 cvx_solver sedumi
at the command line, outside of a cvx model. To revert to SDPT3, type
13 cvx_solver sdpt3
To see which solver is currently selected, simply type
14 cvx_solver
We have found that SDPT3 is much more reliable for problems that use second-order
cones, which include problems involving absolute values, quadratics, power functions,
and norms. SDPT3 is currently in very active development; so if you encounter a
problem that SDPT3 cannot solve but SeDuMi can, please send us a bug report and
we will forward the results to the authors of SDPT3.
8.2 Controlling solver precision
Numerical methods for convex optimization are not exact; they compute their results
to within a predened numerical precision or tolerance. The precision chosen by de-
fault in cvx, which in turn is inherited from the defaults chosen by its solver SeDuMi,
should be entirely acceptable for most applications. Nevertheless, you may wish to
tighten or relax that precision in some applications.
There are several ways to call the cvx_precision command. If you call it with no
arguments, it simply returns a two-element vector of the current precision settings.
The rst element in that vector is the standard precision; the precision the solver
must obtain to return Solved, Unbounded, or Infeasible. The second element in the
vector is the reduced precision, the precision that the solver must achieve in order
to return Solved/Inaccurate, Unbounded/Inaccurate, Infeasible/Inaccurate.
Calling cvx_precision with an argument allows you to actually change the pre-
cision level. One way is to supply a string as an argument, either in command mode
or function mode, chosen from one of ve values:
cvx_precision low: standard = reduced =
1/4
1.2 10
4
.
cvx_precision medium: standard =
3/8
1.3 10
6
, reduced =
1/4
.
44
cvx_precision default: standard =
1/2
1.5 10
8
, reduced =
1/4
.
cvx_precision high: standard =
3/4
1.1 10
11
, reduced =
3/8
.
cvx_precision best: standard = 0, reduced =
1/2
(see below).
In function mode, these calls look like cvx_precision(low), etc. The best preci-
sion setting is special: it instructs the solver to continue until it is completely unable
to make progress. Then, as long as it reaches at least the reduced precision of
1/2
, it may claim a successful solution; otherwise, it returns a cvx_status value of
Failed. An Inaccurate status value is not possible in best mode (nor, for that
matter, in low mode, which sets the standard and reduced precisions to be identical).
The cvx_precision command can also be called with either a scalar or a length-
two vector. If you pass it a scalar, it will assume that as the standard precision, and
it will compute a default reduced precision value for you. Roughly speaking, that
reduced precision will be the square root of the standard precision, with some bounds
imposed to make sure that it stays reasonable. If you supply a vector of values, then
the smallest value will be chosen as the standard precision, and the larger value as
the reduced precision.
The cvx_precision command can be used either within a cvx model or outside
of it; and its behavior diers in each case. If you call it from within a model, e.g.,
cvx_begin
cvx_precision high
...
cvx_end
then the setting you choose will apply only until cvx_end is reached. If you call it
outside a model, e.g.,
cvx_precision high
cvx_begin
...
cvx_end
then the setting you choose will apply globally; that is, to any subsequent models
that are created and solved. The local approach should be preferred in an application
where multiple models are constructed and solved at dierent levels of precision.
If you call cvx_precision in function mode, either with a string or a numeric
value, it will return as its output the previous precision vectorthe same result you
would obtain if you called it with no arguments. This may seem confusing at rst,
but this is done so that you can save the previous value in a variable, and restore it
at the end of your calcuations; e.g.,
cvxp = cvx_precision( high );
cvx_begin
...
cvx_end
cvx_precision( cvxp );
45
This is considered good coding etiquette in a larger application where multiple cvx
models at multiple precision levels may be employed. Of course, a simpler but equally
courteous approach is to call cvx_precision within the cvx model, as described
above, so that its eect lasts only for that model.
8.3 Miscellaneous cvx commands
cvx_problem: typing this within a cvx specication provides a summary of the
current problem. Note that this will contain a lot of information that you may
not recognizecvx performs its conversion to canonical form as the problem is
entered, generating extra temporary variables and constraints in the process.
cvx_clear: typing this resets the cvx system, clearing any and all problems
from memory, but without erasing any of your numeric data. This is useful if
you make a mistake and need to start over. But note that in current versions
of cvx, you can simply start another model with cvx_begin, and the previous
model will be erased (with a warning).
cvx_quiet: typing cvx_quiet(true) suppresses screen output from the solver.
Typing cvx_quiet(false) restores the screen output. In each case, it returns a
logical value representing the previous state of the quiet ag, so you can restore
that state later if you wishwhich is good practice if you wish to share your
code with others.
cvx_pause: typing cvx_pause(true) causes cvx to pause and wait for keyboard
input before and after the solver is called. Useful primarily for demo purposes.
Typing cvx_pause(false) resets the behavior. In each case, it returns a logical
value representing the previous state of the pause ag, so you can restore that
state later if you wish.
cvx_where: returns the directory where the cvx distribution has been installed
assuming that the Matlab path has been set to include that distribution. Useful
if you want to nd certain helpful subdirectories, such as doc, examples, etc.
cvx_version: returns a description of the cvx version number, build number,
and hardware platform you are currently using. If you encounter a bug in cvx,
please run this function and copy its output into your email message along with
a description of your problem.
8.4 Assignments versus equality constraints
Anyone who has used the C or Matlab languages for a suciently long time un-
derstands the dierences between assignments, which employ a single equals sign =
operator, and an equality, which employs the double equal == operator. In cvx, this
distinction is particularly important; confusing the two operators can have serious
consequences. Even when the distinction is well understood, there are important
caveats to using assignments in cvx that we address here as well.
46
The consequences of inadvertently using assignments within a cvx specication
can cause subtle but critical problems. For example, let A, C R
nn
and b R be
given, and consider the simple SDP
minimize Tr(CX)
subject to Tr(AX) = b
X _ 0
(14)
Suppose that we tried to express this problem in cvx as follows:
1 n = 5;
2 A = randn(n,n); C = randn(n,n); b = randn;
3 cvx_begin
4 variable X(n,n) symmetric;
5 minimize( trace( C * X ) );
6 subject to
7 trace( A * X ) == b;
8 X = semidefinite(n);
9 cvx_end
At rst glance, line 8 may look like it constrains X to be positive semidenite; but
it is an assignment, not an equality constraint. So X is actually overwritten with an
anonymous, positive semidenite variable, and the original X is not constrained all!
Fortunately, this particular error is easily caught and prevented by cvx. When
cvx_end is reached, cvx examines each declared variable to verify that it still points
to the variable object it was originally assigned upon declaration. If it does not, it
will issue an error like this:
??? Error using ==> cvx_end
The following cvx variable(s) have been overwritten:
X
This is often an indication that an equality constraint was
written with one equals = instead of two ==. The model
must be rewritten before cvx can proceed.
We hope that this check will prevent at least some typographical errors from having
frustrating consequences in your models.
Of course, this single check does not prevent you from using all assignments
inside your cvx specications, only those that overwrite formally declared variables.
As discussed in 3.7, other kinds of assignments are permitted, and may be genuinely
useful. But in our view they should be used sparingly. For instance, consider the rst
example from 3.7:
variables x y
z = 2 * x - y;
square( z ) <= 3;
quad_over_lin( x, z ) <= 1;
47
The following alternative formulation, which declares z as a formal variable, is nu-
merically equivalent:
variables x y z
z == 2 * x - y;
square( z ) <= 3;
quad_over_lin( x, z ) <= 1;
We recommend taking this approach whenever possible. Declaring intermediate cal-
culations as variables provides an extra measure of clarity in your models, and it
exposes more of the models structure to the solver, possibly improving performance.
8.5 Indexed dual variables
In some models, the number of constraints depends on the model parametersnot
just their sizes. It is straightforward to build such models in cvx using, say, a Matlab
for loop. In order to assign each of these constraints a separate dual variable, we
must nd a way to adjust the number of dual variables as well. For this reason,
cvx supports indexed dual variables. In reality, they are simply standard Matlab cell
arrays whose entries are cvx dual variable objects.
Let us illustrate by example how to declare and use indexed dual variables. Con-
sider the following semidenite program:
minimize
n
i=1
(n i)X
ii
subject to
n
i=1
X
i,i+k
= b
k
, k = 1, 2, . . . , n
X _ 0
(15)
([Stu99]). This problem minimizes a weighted sum of the main diagonal of a positive
semidenite matrix, while holding the sums along each diagonal constant. The pa-
rameters of the problem are the elements of the vector b R
n
, and the optimization
variable is a symmetric matrix X R
nn
. The cvx version of this model is
cvx_begin
variable X( n, n ) symmetric
minimize( ( n - 1 : -1 : 0 ) * diag( X ) );
for k = 0 : n-1,
sum( diag( X, k ) ) == b( k+1 );
end
X == semidefinite(n);
cvx_end
If we wish to obtain dual information for the n simple equality constraints, we need
a way to assign each constraint in the for loop a separate dual variable. This is
accomplished as follows:
cvx_begin
variable X( n, n ) symmetric
48
dual variables y{n}
minimize( ( n - 1 : -1 : 0 ) * diag( X ) );
for k = 0 : n-1,
sum( diag( X, k ) ) == b( k+1 ) : y{k+1};
end
X == semidefinite(n);
cvx_end
The statement
dual variables y{n}
allocates a cell array of n dual variables, and stores the result in the Matlab vari-
able Z. The equality constraint in the for loop has been augmented with a reference
to y{k+1}, so that each constraint is assigned a separate dual variable. When the
cvx_end command is issued, cvx will compute the optimal values of these dual vari-
ables, and deposit them into an n-element cell array y.
This example admittedly is a bit simplistic. With a bit of careful arrangement, it
is possible to rewrite this model so that the n equality constraints can be combined
into a single vector constraint, which in turn would require only a single vector dual
variable.
3
For a more complex example that is not amenable to such a simplication,
see the le
examples/cvxbook/Ch07_statistical_estim/cheb.m
in the cvx distribution. In that problem, each constraint in the for loop is a linear
matrix inequality, not a scalar linear equation; so the indexed dual variables are
symmetric matrices, not scalars.
3
Indeed, a future version of cvx will support the use of the Matlab function spdiags, which will
reduce the entire for loop to the single constraint spdiags(X,0:n-1)==b.
49
A Installation and compatability
cvx requires Matlab 6.1 or later.
4
Primary cvx development is performed using an
Intel-based Mac running Matlab 7.4, with additional development performed on the
following platforms and Matlab versions:
Windows (32-bit): Matlab 6.1, 7.4
Linux (32-bit): Matlab 6.1, 7.4
Linux (64-bit): Matlab 7.1, 7.4
For purposes of testing and support, we have access to most other versions of Mat-
lab for Windows, Linux, and Mac (both Intel and PowerPC). The one signicant
exception is version 7.0.0, so we cannot provide support for it.
Version 7.0.0 has exhibited numerical problems for at least one user; the problems
were eliminated by upgrading. We have also identied another issue using cvx with
Matlab 7.0.1 and 7.0.4; workarounds are provided in A.4 below.
Precompiled MEX les are included for Windows (32-bit), Linux (32-bit), and
Intel Mac OS X. For other platforms, the MEX les will be compiled during the
setup process; see A.2. For platforms other than those listed above (e.g., Solaris,
HPUX), cvx is quite likely to work, but we are unable to provide support.
A.1 Basic instructions
1. Retrieve the latest version of cvx from http://www.stanford.edu/
~
boyd/cvx.
You can download the package as either a .zip le or a .tar.gz le.
2. If you have been running a previous version of cvx, remove it or move it out
of the waysay, by renaming it cvx_oldbefore proceeding. DO NOT allow
the new version of cvx to be unpacked on top of the old.
3. Unpack the le anywhere you like; a directory called cvx will be created. There
is one important exception: do not place cvx in Matlabs own toolbox directory.
(If your previous installation of cvx was placed there, youll need to move it
out of there.)
4. Start Matlab.
5. Change the current directory to the location of cvx. For example, if you un-
packed cvx.zip on your Windows machine into the directory C:\Matlab\personal,
then you would type
cd C:\Matlab\personal\cvx
4
Previous versions of this manual stated that cvx required Matlab 6.0 or later; we have since
discovered that version 6.0 is not sucient.
50
at the Matlab command prompt. Alternatively, if you unpacked cvx.tar.gz
into the directory ~/matlab on a Linux machine, then you would type
cd ~/matlab/cvx
at the Matlab command prompt.
6. Type the command
cvx_setup
at the Matlab prompt. This does two things: it sets the Matlab search path
so it can nd all of the cvx program les, and it runs a simple test problem to
verify the installation. On some platforms, it compiles MEX les as well (see
A.2 below). If all goes well, the command will output the line
No errors! cvx has been successfully installed.
(among others). If this message is not displayed, or any warnings or errors are
generated, then there is a problem with the cvx installation. Try installing the
package again; and if that fails, send us a bug report and tell us about it.
7. If you plan to use cvx regularly, you will need to save the current Matlab path
for subsequent Matlab sessions. Follow the instructions provided by cvx_setup
to accomplish that.
A.2 MEX le compilation
Precompiled MEX les are provided only for a handful of platforms; for others, they
must be compiled by the user. The cvx_setup script will automatically detect the
absence of compiled MEX les and compile them for SeDuMi, SDPT3, and cvx.
However, cvx_setup will succeed only if the MEX system is congured properly on
your machine. If you have compiled MEX les before, this is likely the case; otherwise,
run the command
mex -setup
at the Matlab prompt and answer the questions that follow. If necessary, consult
documentation for Matlab for details on this process.
A.3 About SeDuMi and SDPT3
The cvx distribution includes copies of the solvers SeDuMi and SDPT3 in the direc-
tories cvx/sedumi and cvx/sdpt3, respectively. We strongly recommend that you
use our versions of these solvers, and remove any other versions that you have in your
Matlab path. The version of SeDuMi provided with cvx incorporates some bug xes
and a slight modication that is not currently found in versions available from the
authors. This is not the case with SDPT3; however, the author of SDPT3 frequently
51
posts bug xes to the software without updating the version number (currently 4.0
beta). Therefore, it is important to be sure that any newer version of SDPT3 that
you might wish to use was downloaded after you obtained cvx.
A.4 A Matlab 7.0 issue
The techniques that cvx use to embed a new modeling language inside Matlab seem
to cause some confusion for Matlab 7.0, specically version 7.0.4 (R14SP2). We are
reasonably condent that the issue is due to a bug in Matlab itself. It does not occur
in versions 6.5.2 and earlier, or 7.1 and later. It may occur in earlier versions of
Matlab 7.0 as well (R14,R14SP1).
The bug is this: in some circumstances, a .m-le containing a cvx model will cause
an error that looks like this:
??? Error: File: thrusters.m Line: 43 Column: 5
"p" was previously used as a variable,
conflicting with its use here as the name of a function.
The le name, line/column numbers, and variable name may dier, but the error
message remains the same. The example that produced this particular error is a
simple inequality constraint
p >= -5;
where p is a cvx variable previously declared with a variable statement. Interest-
ingly, a dierent inequality constraint
u >= 0
in the same model causes no complaint. So we cannot oer you a precise set of
circumstances that cause the error.
Fortunately, the workaround is very simple: simply remove all of the extra space
in the constraint. In this example, changing the constraint to
p>=-5;
eliminates the error. You may still indent the constraint as you wishjust remove
the intermediate spaces.
We have no idea why this workaround works, but it doesreliably.
52
B Operators, functions, and sets
B.1 Basic operators and linear functions
Matlabs standard arithmetic operations for addition +, subtraction -, multiplication
* .*, division / \ ./ ./ .\, and exponentiation ^ .^ have been overloaded to work
in cvx whenever appropriatethat is, whenever their use is consistent with both
standard mathematical and Matlab conventions and the DCP ruleset. For example:
Two cvx expressions can be added together if they are of the same dimension
(or one is scalar) and have the same curvature (i.e., both are convex, concave,
or ane).
A cvx expression can be multiplied or divided by a scalar constant. If the
constant is positive, the curvature is preserved; if it is negative, curvature is
reversed.
An ane column vector cvx expression can be multiplied by a constant matrix
of appropriate dimensions; or it can be left-divided by a non-singular constant
matrix of appropriate dimension.
Numerious other combinations are possible, of course. For example, the use of the
exponentiation operators ^ .^ are somewhat limited; see B.2 below.
Matlabs basic matrix manipulation and arithmetic operations have been extended
to work with cvx expressions as well, including:
Concatenation: [ A, B ; C, D ]
Indexing: x(n+1:end), X([3,4],:), etc.
Indexed assignment, including deletion: y(2:4) = 1, Z(1:4,:) = [], etc.
Transpose and conjugate transpose: Z., y
A number of Matlabs basic functions have been extended to work with cvx expres-
sions as well:
conj conv cumsum diag dot find fliplr flipud flipdim horzcat
hankel ipermute kron permute repmat reshape rot90 sparse sum
trace tril triu toeplitz vertcat
Most should behave identically with cvx expressions as they do with numeric ex-
pressions. Those that perform some sort of summation, such as cumsum, sum, or
multiplication, such as conv, dot or kron, can only be used in accordance with the
disciplined convex programming rules. For example, kron(X,Y) is valid only if either
X or Y is constant; and trace(Z) is valid only if the elements along the diagonal have
the same curvature.
53
B.2 Nonlinear functions
What follows are two lists of nonlinear functions supported by cvx: rst, a list of
standard Matlab functions extended to work with cvx; and second, a list of new
functions created specically for use in cvx.
In some cases, limitations of the underlying solvers place certain restrictions or
caveats on their use:
Functions marked with a dagger () are not supported natively by the solvers
that cvx uses. They are handled using a successive approximation method,
which makes multiple calls to the underlying solver, achieving the same nal
precision. (See D.1 for details.) If you use one of these functions, you will be
warned that successive approximation will be used.
Functions involving powers (e.g., x^p) and p-norms (e.g., norm(x,p)) are marked
with a star (). cvx represents these functions exactly when p is a rational num-
ber. For irrational values of p, a nearby rational is selected using Matlabs rat
function. See D.2 for details.
B.2.1 Built-in functions
abs: absolute value for real and complex arrays. Convex.
exp: exponential. Convex and nondecreasing.
log: logarithm. Concave and nondecreasing.
max: maximum. Convex and nondecreasing.
min: minimum. Concave and nondecreasing.
norm: norms for real and complex vectors and matrices. Convex. The one-
argument version norm(x) computes the 2-norm for vectors and induced 2-norm
(maximum singular value) for matrices. The two-argument version norm(x,p)
is supported as follows:
For vectors, all p 1 are accepted, but see Appendix D.2 for more details
about how cvx handles values other than 1, 2, and Inf.
For matrices, p must be 1, 2, Inf, or Fro.
polyval: polynomial evaluation. polyval(p,x), where p is a vector of length
n, computes
p(1) * x.^(n-1) + p(2) * x.^(n-2) + ... + p(n-1) * x + p(n)
This function can be used in cvx in two ways:
54
If p is a variable and x is a constant, then polyval(x,p) computes a linear
combination of the elements of p. The combination must satisfy the DCP
rules for addition and scaling.
If p is a constant and x is a variable, then polyval(x,p) constructs a
polynomial function of the variable x. The polynomial must be ane,
convex, or concave, and x must be real and ane.
power: x^p and x.^p, where x is a real variable and and p is a real constant.
For x^p, x and p must be scalars. Only those values of p which can reasonably
and unambiguously interpreted as convex or concave are accepted:
p = 0. Constant. x.^p is identically 1.
0 < p < 1. Concave. The argument x must be concave (or ane), and is
implicitly constrained to be nonnegative.
p = 1. Ane. x.^p is then x.
p 2, 4, 6, 8, .... Convex. Argument x must be ane.
p > 1, p , 2, 3, 4, 5, .... Convex. Argument x must be ane, and is
implicitly constrained to be nonnegative.
Negative and odd integral values of p are not permitted, but see the functions
pow_p, pow_pos, and pow_abs in the next section for useful alternatives.
power: p.^x and p^x, where p is a real constant and x is a real variable. For
p^x, p and x must be scalars. Valid values of p include:
p 0, 1. Constant.
0 < p < 1. Convex and nonincreasing; x must be concave.
p > 1. Convex and nondecreasing; x must be convex.
Negative values of p are not permitted.
sqrt: square root. Implicitly constrains its argument to be nonnegative. Con-
cave and nondecreasing.
B.2.2 New nonlinear functions
Even though these functions were developed specically for cvx, they work outside
of a cvx specication as well, when supplied with numeric arguments.
berhu(x,M): The reversed Huber function (hence, Berhu), dened as [x[ for
[x[ M, and ([x[
2
+ M
2
)/2M for [x[ M. Convex. If M is omitted, M = 1
is assumed; but if supplied, it must be a positive constant. Also callable with
three arguments as berhu(x,M,t), which computes t+t*berhu(x/t,M), useful
for concomitant scale estimation (see [Owe06]).
55
det_inv: determinant of inverse of a symmetric (or Hermitian) positive denite
matrix, (det X
1
), which is the same as the sum of the inverses of the eigenval-
ues. When used inside a cvx specication, det_inv constrains the matrix to be
symmetric (if real) or Hermitian (if complex) and positive semidenite. When
used with numerical arguments, det_inv returns -Inf if these constraints are
not met. Concave.
det_rootn: n-th root of the determinant of a semidenite matrix, (det X)
1/n
.
When used inside a cvx specication, det_rootn constrains the matrix to be
symmetric (if real) or Hermitian (if complex) and positive semidenite. When
used with numerical arguments, det_rootn returns -Inf if these constraints
are not met. Concave.
det_root2n: the 2n-th root of the determinant of a semidenite matrix; i.e.,
det_root2n(X)=sqrt(det_rootn(X)). Concave. Maintained solely for back-
compatibility purposes.
entr, the elementwise entropy function: entr(x)=x.*log(x). Concave. Re-
turns -Inf when called with a constant argument that has a negative entry.
geo_mean: the geometric mean of a vector, (
n
k=1
x
k
)
1/n
. When used inside a
cvx specication, geo_mean constrains the elements of the vector to be non-
negative. When used with numerical arguments, geo_mean returns -Inf if any
element is negative. Concave and increasing.
huber(x,M), dened as 2M[x[ M
2
for [x[ M, and [x[
2
for [x[ M. Convex.
If M is omitted, then M = 1 is assumed; but if it supplied, it must be a positive
constant. Also callable as huber(x,M,t), which computes t+t*huber(x/t,M),
useful for concomitant scale estimation (see [Owe06]).
huber_circ(x,M), the circularly symmetric Huber function, dened as |x|
2
for |x|
2
M, and 2M|x|
2
M
2
for |x|
2
M. Same (and implemented) as
huber_pos(norm(x),M). Convex.
huber_pos(x,M). Same as Huber function for nonnegative x; zero for negative
x. Convex and nondecreasing.
inv_pos, inverse of the positive portion, 1/ maxx, 0. Inside cvx specication,
imposes constraint that its argument is positive. Outside cvx specication,
returns + if x 0. Convex and decreasing.
kl_div, elementwise Kullback-Leibler distance, kl_div(x,y)=x.*log(x./y)-x+y,
for x, y nonnegative, with x(i) zero whenever y(i) is zero. Convex. Outside
cvx specication, returns + if arguments arent in the domain.
lambda_max: maximum eigenvalue of a real symmetric or complex Hermitian
matrix. Inside cvx, imposes constraint that its argument is symmetric (if real)
or Hermitian (if complex). Convex.
56
lambda_min: minimum eigenvalue of a real symmetric or complex Hermitian
matrix. Inside cvx, imposes constraint that its argument is symmetric (if real)
or Hermitian (if complex). Concave.
log_det: log of determinant of a positive denite matrix, log det(X). When
used inside a cvx specication, log_det constrains its argument to be symmet-
ric (if real) or Hermitian (if complex) and positive denite. With numerical
argument, log_det returns -Inf if these constraints are not met. Concave.
log_norm_cdf(x): logarithm of cumulative distribution function of standard
normal random variable. Concave and increasing. The current implementation
is a fairly crude SDP-representable approximation, with modest accuracy over
the interval [4, 4]; we intend to replace it with a much better approximation
at some point.
log_sum_exp(x): the logarithm of the sum of the elementwise exponentials
of x. Convex and nondecreasing. This is used internally in expert GP mode,
but can also be used in standard DCPs.
logsumexp_sdp: a polynomial approximation to the log-sum-exp function with
global absolute accuracy. This approximation is used in default GP mode, but
can also be used in standard DCPs.
matrix_frac(x,Y): matrix fractional function, x
T
Y
1
x. In cvx, imposes con-
straint that Y is symmetric (or Hermitian) and positive denite; outside cvx,
returns + unless Y = Y
T
0. Convex.
norm_largest( x, k ), for real and complex vectors, returns the sum of the
largest k magnitudes in the vector x. Convex.
norm_nuc(X), is the sum of the singular values of a real or complex matrix X.
(This is the dual of the usual spectral matrix norm, i.e., the largest singular
value.) Convex.
norms( x, p, dim ) and norms_largest( x, k, dim ). Computes vector
norms along a specied dimension of a matrix or N-d array. Useful for sum-of-
norms and max-of-norms problems. Convex.
poly_env( p, x ). Computes the value of the convex or concave envelope
of the polynomial described by p (in the polyval sense). p must be a real
constant vector whose length n is 0, 1, 2, 3, or some other odd length; and x
must be real and ane. The sign of the rst nonzero element of p determines
whether a convex (positive) or concave (negative) envelope is constructed. For
example, consider the function p(x) (x
2
1)
2
= x
4
2x
2
+1, depicted along
with its convex envelope in Figure 2. The two coincide when [x[ 1, but
deviate when [x[ < 1. Attempting to call polyval([1,0,2,0,1],x) in a cvx
model would yield an error, but a call to poly_env([1,0,2,0,1],x) yields a
57
1.5 1 0.5 0 0.5 1 1.5
0.2
0
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
(x
2
1)
2
envelope
Figure 2: The polynomial function p(x) = x
4
2x
2
+ 1 and its convex envelope.
valid representation of the envelope. For convex or concave polynomials, this
function produces the same result as polyval.
pos: maxx, 0, for real x. Convex and increasing.
pow_abs(x,p): [x[
p
for x R or x C and p 1. Convex. If p is irrational,
a nearby rational value is chosen; see Appendix D.2 for details.
pow_pos(x,p): maxx, 0
p
for x R and p 1. Convex and nondecreasing.
If p is irrational, a nearby rational value is chosen; see Appendix D.2 for details.
pow_p(x,p), for x R and real constant p computes nonnegative convex and
concave branches of the power function:
p 0 : f
p
(x)
_
x
p
x > 0
+ x 0
convex, nonincreasing
0 <p 1 : f
p
(x)
_
x
p
x 0
x < 0
concave, nondecreasing
p 1 : f
p
(x)
_
x
p
x 0
+ x < 0
convex, nonmonotonic
quad_form(x,P), x
T
Px for real x and symmetric P, and x
H
Px for complex x
and Hermitian P. Convex in x for P constant and positive semidenite; concave
in x for P constant and negative semidenite. This function is provided since
cvx will not recognize x*P*x as convex (even when P is positive semidenite).
58
quad_over_lin, x
T
x/y for x R
n
, y > 0; for x C
n
, y > 0: x
x/y. In cvx
specication, adds constraint that y > 0. Outside cvx specication, returns
+ if y 0. Convex, and decreasing in y.
quad_pos_over_lin: sum_square_pos( x ) / y for x R
n
, y > 0. Convex,
increasing in x, and decreasing in y.
rel_entr: Scalar relative entropy: rel_entr(x,y)=x.*log(x/y). Convex.
sigma_max: maximum singular value of real or complex matrix. Same as norm.
Convex.
square: x
2
for x R. Convex.
square_abs: [x[
2
for x R or x C.
square_pos: maxx, 0
2
for x R. Convex and increasing.
sum_largest(x,k) sum of the largest k values, for real vector x. Convex and
increasing.
sum_smallest(x,k), sum of the smallest k values, i.e., -sum_smallest(-x,k).
Concave and decreasing.
sum_square: sum( square( x ) ). Convex.
sum_square_abs: sum( square_abs( x ) ). Convex.
sum_square_pos: sum( square_pos( x ) ); works only for real values. Con-
vex and increasing.
trace_inv(X), trace of the inverse of an SPD matrix X, which is the same as
the sum of the inverses of the eigenvalues. Convex. Outside of cvx, returns
+Inf if argument is not positive denite.
trace_sqrtm(X), trace of the matrix squareroot of a positive semidenite ma-
trix X. which is the same as the sum of the squareroots of the eigenvalues.
Concave. Outside of cvx, returns +Inf if argument is not positive semidenite.
B.3 Sets
cvx currently supports the following sets; in each case, n is a positive integer constant.
nonnegative(n):
R
n
+
x R
n
[ x
i
0, i = 1, 2, . . . , n
simplex(n):
R
n
1+
x R
n
[ x
i
0, i = 1, 2, . . . , n,
i
x
i
= 1
59
lorentz(n):
Q
n
(x, y) R
n
R [ |x|
2
y
rotated_lorentz(n):
Q
n
r
(x, y, z) R
n
RR [ |x|
2
yz, y, z 0
complex_lorentz(n):
Q
n
c
(x, y) C
n
R [ |x|
2
y
rotated_complex_lorentz(n):
Q
n
rc
(x, y, z) C
n
RR [ |x|
2
yz, y, z 0
semidefinite(n):
S
n
+
_
X R
nn
[ X = X
T
, X _ 0
_
hermitian_semidefinite(n):
H
n
+
_
Z C
nn
[ Z = Z
H
, X _ 0
_
nonneg_poly_coeffs(n): The cone of all coecients of nonnegative polyno-
mials of degree n; n must be even:
P
+,n
_
p R
n+1
[
n
i=0
p
i+1
x
ni
0 x R
_
convex_poly_coeffs(n): The cone of all coecients of convex polynomials of
degree n; n must be even:
P
+,n
_
p R
n+1
[
n2
i=0
(n i)(n i 1)p
i+1
x
ni2
0 x R
_
exponential_cone:
E cl
_
(x, y, z) RRR [ y > 0, ye
x/y
z
_
geo_mean_cone(n):
G
n
cl
_
(x, y) R
n
R
n
R
n
[ x 0, (
n
i=1
x
i
)
1/n
y
_
60
C cvx status messages
After a complete cvx specication has been entered and the cvx_end command issued,
the solver is called to generate a numerical result. The solver can produce one of ve
exit conditions, which are indicated by the value of the string variable cvx_status.
The nominal values of cvx_status, and the resulting values of the other variables,
are as follows:
Solved: A complementary (primal and dual) solution has been found. The
primal and dual variables are replaced with their computed values, and the the
optimal value of the problem is placed in cvx_optval (which, by convention, is
0 for feasibility problems).
Unbounded: The problem has been proven to be unbounded below through the
discovery of an unbounded primal direction. This direction is stored in the
primal variables. The value of cvx_optval is set to -Inf for minimizations,
and -Inf for maximizations. Feasibility problems by construction cannot be
unbounded below.
It is important to understand that the unbounded primal direction is very likely
not a feasible point. If a feasible point is required, the problem should be re-
solved as a feasibility problem by omitting the objective.
Infeasible: The problem has been proven to be infeasible through the discov-
ery of an unbounded dual direction. Appropriate components of this direction
are stored in the dual variables. The values of the primal variables are lled with
NaNs. The value of cvx_optval is set to +Inf for minimizations and feasibility
problems, and -Inf for maximizations.
In some cases, SeDuMi is unable to achieve the numerical certainty it requires to make
one of the above determinationsbut is able to draw a weaker conclusion by relaxing
those tolerances somewhat. In such cases, one of the following results is returned:
Inaccurate/Solved: The problem is likely to have a complementary solution.
Inaccurate/Unbounded: The problem is likely to be unbounded.
Inaccurate/Infeasible: The problem is likely to be infeasible.
The values of the primal and dual variables, and of cvx_optval, are updated identi-
cally to the accurate cases. Two nal results are also possible:
Failed: The solver failed to make sucient progress towards a solution. The
values of cvx_optval and primal and dual variables are lled with NaNs. This
result can occur because of numerical problems within SeDuMi, often because
the problem is particularly nasty in some way (e.g., a non-zero duality gap).
61
Overdetermined: The presolver has determined that the problem has more
equality constraints than variables, which means that the coecient matrix
of the equality constraints is singular. In practice, such problems are often,
but not always, infeasible. Unfortunately, solvers typically cannot handle such
problems, so a precise conclusion cannot be reached.
The situations that most commonly produce an Overdetermined result are discussed
in D.3 below.
62
D Advanced solver topics
D.1 The successive approximation method
Prior to version 1.2, the functions requested most often to be added to the cvx
function library were those from the exponential family, including exp, log, and
various entropy functions. Unfortunately, cvx utilizes symmetric primal/dual solvers
that simply cannot support those functions natively; and a variety of practical factors
has delayed the use of other types of solvers with cvx.
For this reason, we have constructed a successive approximation method that
allows symmetric primal/dual solvers to support the exponential family of functions.
The precise nature of the method will be published elsewhere, but we can provide
a highly simplied description here. First, we construct a global approximation for
exp (or log, etc..) which is accurate within a neighborhood of some center point
x
0
. Solving this approximation yields an approximate optimal point x. We shift the
center point x
0
towards x, construct a new approximation, and solve again. This
process is repeated until [ x x
0
[ is small enough to conclude that our approximate
is accurate enough to represent the original model. Again, this is a highly simplied
description of the approach; for instance, we are also monitoring the dual problem as
well to guide our judgements for shifting x
0
and terminating.
D.2 Irrational powers
In order to implement power expressions like x
p
and p-norms |x|
p
for 1 < p < , cvx
uses an SDP-compatible method described in [AG01], and enhanced by the authors
of cvx. This approach is exactas long as the exponent p is rational. To determine
integral values p
n
, p
d
such that p
n
/p
d
= p, cvx uses Matlabs rat function with its
default tolerance of 10
6
. There is currently no way to change this tolerance. See the
documentation for rat for more details.
The complexity of the SDP implementation depends on roughly on the size of the
values p
n
and p
d
. Let us introduce a more precise measure of this complexity. For
p = 2, a constraint x
p
y can be represented with exactly one 2 2 LMI:
x
2
y =
_
y x
x 1
_
_ 0.
For other values of p = p
n
/p
d
, cvx generates a number of 22 LMIs that depends on
both p
n
and p
d
; we denote this number by k(p
n
, p
d
). (A number of internal variables
and equality constraints are also generated, but we ignore them for this analysis.) An
empirical study has shown that for p = p
n
/p
d
> 1, cvx achieves
k(p
n
, p
d
) log
2
p
n
+ (p
n
),
where the (p
n
) term grows very slowly compared to the log
2
term. Indeed, for
p
n
4096, we have veried that (p
n
) is usually 1 or 2, but occasionally 0 or 3.
Similar results are obtained for 0 < p < 1 and p < 0.
63
The cost of this SDP representation is relatively small for nearly all useful values of
p. Nevertheless, cvx issues a warning whenever k(p
n
, p
d
) > 10 to insure that the user is
not surprised by any unexpected slowdown. In the event that this threshold does not
suit you, you may change it using the command cvx_power_warning(thresh), where
thresh is the desired cuto value. Setting the threshold to Inf disables it completely.
As with the command cvx_precision, you can place a call to cvx_power_warning
within a model to change the threshold for a single model; or outside of a model
to make a global change. The command always returns the previous value of the
threshold, so you can save it and restore it upon completion of your model, if you wish.
You can query the current value by calling cvx_power_warning with no arguments.
D.3 Overdetermined problems
This status message Overdetermined commonly occurs when structure in a variable
or set is not properly recognized. For example, consider the problem of nding the
smallest diagonal addition to a matrix W R
nn
to make it positive semidenite:
minimize Trace D
subject to W + D _ 0
D diagonal
(16)
In cvx, this problem might be expressed as follows:
n = size(W,1);
cvx_begin
variable D(n,n) diagonal;
minimize( trace( D ) );
subject to
W + D == semidefinite(n);
cvx_end
If we apply this specication to the matrix W=randn(5,5), a warning is issued,
Warning: Overdetermined equality constraints;
problem is likely infeasible.
and the variable cvx_status is set to Overdetermined.
What has happened here is that the unnamed variable returned by statement
semidefinite(n) is symmetric, but W is xed and unsymmetric. Thus the problem,
as stated, is infeasible. But there are also n
2
equality constraints here, and only
n + n (n + 1)/2 unique degrees of freedomthus the problem is overdetermined.
The following modied version of the specication corrects this problem by extracting
the symmetric part of W:
n = size(W,1);
cvx_begin
variable D(n,n) diagonal;
64
minimize( trace( D ) );
subject to
0.5 * ( W + W ) + D == semidefinite(n);
cvx_end
E Acknowledgements
We wish to thank the following people for their contributions to the development of
cvx: Toh Kim Chuan, Laurent El Ghaoui, Arpita Ghosh, Siddharth Joshi, Johan
Lofberg, Almir Mutapcic, Michael Overton and his students, Art Owen, Rahul Pan-
icker, Imre Polik, Joelle Skaf, Lieven Vandenberghe, Argyris Zymnis. We are also
grateful to the many students in several universities who have (perhaps unwittingly)
served as beta testers by using cvx in their classwork. We thank Igal Sason for catch-
ing many typos in an earlier version of this document, and generally helping us to
improve its clarity.
65
References
[AG01] F. Alizadeh and D. Goldfarb. Second-order cone programming. Techni-
cal Report RRR 51-2001, RUTCOR, Rutgers University, November 2001.
Available at http://rutcor.rutgers.edu/pub/rrr/reports2001/51.ps.
[BKMR98] A. Brooke, D. Kendrick, A. Meeraus, and R. Raman. GAMS: A Users
Guide. The Scientic Press, South San Francisco, 1998. Available at http:
//www.gams.com/docs/gams/GAMSUsersGuide.pdf.
[BKVH05] S. Boyd, S. J. Kim, L. Vandenberghe, and A. Hassibi. A tutorial on
geometric programming. Optimization and Engineering, 2005. Available at
http://www.stanford.edu/
~
boyd/gp_tutorial.html.
[BV04] S. Boyd and L. Vandenberghe. Convex Optimization. Cambridge Univer-
sity Press, 2004. Available at http://www.stanford.edu/
~
boyd/cvxbook.
html.
[Cru02] C. Crusius. A Parser/Solver for Convex Optimization Problems. PhD
thesis, Stanford University, 2002.
[DV05] J. Dahl and L. Vandenberghe. CVXOPT: A Python Package for Convex
Optimization. Available at http://www.ee.ucla.edu/
~
vandenbe/cvxopt,
2005.
[FGK99] R. Fourer, D. Gay, and B. Kernighan. AMPL: A Modeling Language for
Mathematical Programming. Duxbury Press, December 1999.
[GBY06] M. Grant, S. Boyd, and Y. Ye. Disciplined convex programming. In L. Lib-
erti and N. Maculan, editors, Global Optimization: from Theory to Im-
plementation, Nonconvex Optimization and Its Applications, pages 155
210. Springer, New York, 2006. Available at http://www.stanford.edu/
~
boyd/disc_cvx_prog.html.
[Gra04] M. Grant. Disciplined Convex Programming. PhD thesis, Department of
Electrical Engineering, Stanford University, December 2004. See http:
//www.stanford.edu/
~
boyd/disc_cvx_prog.html.
[Lof05] J. Lofberg. YALMIP version 3 (software package). http://control.ee.
ethz.ch/
~
joloef/yalmip.php, September 2005.
[Mat04] The MathWorks, Inc. MATLAB (software package). http://www.
mathworks.com, 2004.
[Mat05] The MathWorks, Inc. MATLAB optimization toolbox (software package).
http://www.mathworks.com/products/optimization/, 2005.
[MOS05] MOSEK ApS. Mosek (software package). http://www.mosek.com, Febru-
ary 2005.
66
[Owe06] A. Owen. A robust hybrid of lasso and ridge regression. Technical report,
Department of Statistics, Stanford University, October 2006. Authors in-
ternal draft.
[Stu99] J. Sturm. Using SeDuMi 1.02, a MATLAB toolbox for optimization over
symmetric cones. Optimization Methods and Software, 11:625653, 1999.
Software available at http://sedumi.mcmaster.ca/.
[TTT06] K. Toh, R. T ut unc u, and M. Todd. SDPT3 4.0 (beta) (software package).
http://www.math.nus.edu.sg/
~
mattohkc/sdpt3.html, July 2006.
[WB00] S.-P. Wu and S. Boyd. SDPSOL: A parser/solver for semidenite programs
with matrix structure. In L. El Ghaoui and S.-I. Niculescu, editors, Recent
Advances in LMI Methods for Control, chapter 4, pages 7991. SIAM, 2000.
Available at http://www.stanford.edu/
~
boyd/sdpsol.html.
67
Index
ane function, 28
AMPL, 4
array variable, 17
assignment, 46
banded matrix variable, 17
bug report, 6
complex variable, 17
composition, 29
concave
function, 28
constraint, 11, 13, 18
bounds, 11, 13, 18
equality, 14, 18, 46
inequality, 14, 18
nonconvex, 14, 16
set, 19
convex
function, 28
cvx, 4
functions, 53
installing, 50
operators, 53
precision, 61
status messages, 61
cvx begin, 8
cvx clear, 46
cvx end, 8
cvx expert, 63
cvx optval, 10, 18
cvx pause, 46
cvx power warning, 63
cvx precision, 44
cvx problem, 46
cvx quiet, 46
cvx solver, 44
cvx status, 10
cvx version, 46
cvx where, 46
CVXOPT, 5
DCP, 4, 5
ruleset, 5, 25
deadzone function, 34
dening new function, 34
diagonal matrix variable, 17
disciplined convex programming, see DCP
dual variable, 21
epigraph, 35
equality constraint, 46
examples, 8
expression, 23
expression holder, 23
expressions, 23
feasibility, 18, 26
feedback, 6
function, 19
cvx libary, 53
ane, 28
composition, 29
concave, 28
convex, 28
deadzone, 34
dening new, 34
epigraph, 35
generalized posynomial, 41
Huber, 35
hypograph, 36
monomial, 41
monotonicity, 28
objective, 9
posynomial, 41
quadratic, 32
GAMS, 4
generalized geometric programming, see
GGP
geometric program, see GP
geometric programming mode, 41
GGP, 41
GP, 4, 41
mode, 4, 41
68
Hermitian matrix variable, 17
Huber function, 35
hypograph, 36
inequality
constraint, 18
matrix, 39, 40
infeasible problem, 22
installing cvx, 50
Lagrange multipler, 21
least squares, 8
linear
matrix inequality, see LMI
program, see LP
linprog, 11
LMI, 39
Lorentz cone, see second-order cone
LP, 4, 5
Matlab, 4
maximize, 10, 18
minimize, 9, 18
mode
GP, 41
SDP, 39
monotonicity, 28
MOSEK, 5
nondecreasing, 28
nonincreasing, 28
norm, 9
norm(,1), 12
norm(,Inf), 11, 14
objective function, 9, 18
nonconvex, 10
operators, 53
output
suppressing, 46
overdetermined problem, 64
platforms, 50
posynomial, 41
powers, 63
precision, 61
changing, 44
problem
dual, 21
feasibility, 18, 26
infeasible, 22
overdetermined, 64
python, 4
QP, 4
quadratic form, 32
quadratic program, see QP
SDP, 4, 5, 19, 39
mode, 4, 39
SDPT3, 5, 44
second-order cone, 20
second-order cone program, see SOCP
SeDuMi, 5, 44
semidefinite, 19
semidenite program, see SDP
semidenite programming mode, 39
set, 19
SOCP, 4, 5, 20
solver
changing, 44
status messages, 61
successive approximation, 63
symmetric matrix variable, 17
Toeplitz matrix variable, 17
trade-o curve, 15
variable
array, 17
banded matrix, 17
complex, 17
declaring, 17
diagonal matrix, 17
Hermitian matrix, 17
structure, 17
symmetric matrix, 17
Toeplitz matrix, 17
variable, 9, 17
variables, 17
version information, 5
YALMIP, 4
69