Advanced YALMIP programming


In some cases, it might be necessary to do more advanced YALMIP coding. YALMIP comes with a number of commands to facilitate this.

Let us begin by defining a set of variables.

x = sdpvar(2,1);
y = x'*randn(2)*x;

A common task is to find variables used in an expression. To do this, there are two important commands, depends.m and getvariables.m.

depends(x)
 ans =
  1 2

depends(y)
 ans =
  1 2

getvariables(x)
 ans =
  1 2

getvariables(y)
 ans =
  3 4 5

The command depends.m gives the actual linear independent variables, while getvariables.m returns the variable indices to the so called relaxed variables used in YALMIP. When a nonlinear expression is defined in YALMIP, a new variable is introduced for every monomial term. For our quadratic variable y, we need 3 monomials.

getvariables(x(1)^2)
 ans = 
  3

getvariables(x(2)^2)
 ans = 
  4
getvariables(x(1)*x(2))
 ans = 
  5

The commands above only give the internal identifiers. To create an sdpvar variable using these variables, we use the command recover.m

variables_in_y = recover(depends(y))
Linear matrix variable 2x1 (full, real)

All sdpvar objects are defined by the variable indicies obtained with getvariables.m, and a basis. Let us begin with scalar variables to explain the concepts. The basis with respect to all variables returned from getvariables.m is obtained using getbase.m

x = sdpvar(1,1);
y = sdpvar(1,1);
z = pi+2*x+3*y;
full(getbase(z))

 ans =

  3.1416 2.0000 3.0000

A basis with respect to a particular variable is obtained using getbasematrix.m.

full(getbasematrix(z,getvariables(x)))

 ans =

  2

full(getbasematrix(z,0))

 ans =

  3.1416

full(getbasematrix(z,123456789))

 ans =

  0

Matrix variables are defined in the same way, except that all basis matrices are stored in a vectorized format.

x = pi*eye(2)+5*sdpvar(2,2);
base = full(getbase(x))
 base =

 3.1416 5.0000      0      0
      0      0 5.0000      0
      0      0 5.0000      0
 3.1416      0      0 5.0000

reshape(base(:,1),2,2)
 ans =

  3.1416      0
       0 3.1416

The command is can be useful to select parts of set objects

F = set(x>0) + lmi(x*x' > 0);
F_linear = F(find(is(F,'linear')))

replace.m can be useful in some situations to replace an sdpvar inside an sdpvar or set.

F0 = replace(F,x(1),0);
y0 = replace(y,x(1),0);

The terms in a set object can be extracted and converted to sdpvar objects.

xtimesx = sdpvar(F(2));

More to come but do not hesitate to ask.