Basics: The sdpvar and set object


The most important command in YALMIP is sdpvar. This command is used to the define decision variables. To define a matrix (or scalar) P with height n and width m, we write

P = sdpvar(n,m)

A square matrix is symmetric by default!. To obtain a fully parameterized square matrix, a third argument is needed.

P = sdpvar(3,3,'full')

The third argument can be used to obtain a number of pre-defined types of variables, such as Toeplitz, Hankel, symmetric and skew-symmetric matrices. See the help text on sdpvar for details.  Alternatively, the associated standard commands can be applied to a suitable vector

x = sdpvar(n,1);
D = diag(x) ;    % Diagonal matrix
H = hankel(x);   % Hankel matrix
T = toeplitz(x); % Hankel matrix

Scalars can be defined in three different ways.

x = sdpvar(1,1); y = sdpvar(1,1);
x = sdpvar(1);   y = sdpvar(1);
sdpvar x y

The sdpvar objects are manipulated in MATLAB as any other variable and (almost) all standard functions are overloaded. Hence, the following commands are valid

P = sdpvar(3,3) + diag(sdpvar(3,1));
X = [P P;P eye(length(P))] + 2*trace(P);
Y = X + sum(sum(P*rand(length(P)))) + P(end,end)+hankel(X(:,1));

To define constraints, the command set is used (with set meaning set as in convex set, non-convex set, set of integers etc, not as in set/get). The meaning of a constraint is context-dependent. If left-hand side and right-hand side are Hermitian, the constraint is interpreted in terms of positive definiteness, otherwise element-wise. Hence, declaring a symmetric matrix and a positive definiteness constraint is done with

n = 3;
P = sdpvar(n,n);
F = set(P>0);

while a symmetric matrix with positive elements is defined with, e.g.,

P = sdpvar(n,n);
F = set(P(:)>0);

According to the rules above, a non-square matrix with positive elements can be defined using the > operator immediately

P = sdpvar(n,2*n);
F = set(P>0);

A list of several constraints is defined by just adding set objects.

P = sdpvar(n,n);
F = set(P>0) + set(P(1,1)>2);

Of course, the involved expressions can be arbitrary sdpvar objects, and equality constraints (==) can be defined, as well as constraints using <.

F = set(P>0) + set(P(1,1)<2) + set(sum(sum(P))==10);

In fact, non-strict operators =< and >= may also be used (by default, there is no difference, but by using the option shift in sdpsetttings, it is possible to aim for strict feasibility). Note though that no solver can distinguish between strict and non-strict constraints. In fact, most solvers will not even respect a non-strict constraint but often return slightly infeasible solutions.

F = set(P>=0) + set(P(1,1)<=2) + set(sum(sum(P))==10);

Finally, a convenient way to define several constraint is to use double-sided constraints.

F = set(0 < P(1,1) < 2);