[37] | 1 | clc |
---|
| 2 | echo on |
---|
| 3 | %********************************************************* |
---|
| 4 | % |
---|
| 5 | % Basic manipulation of variables and constraints |
---|
| 6 | % |
---|
| 7 | %********************************************************* |
---|
| 8 | % |
---|
| 9 | % Let us first introduce the central part in YALMIP, |
---|
| 10 | % the sdpvar object |
---|
| 11 | % |
---|
| 12 | % The sdpvar object is typically the decision variable |
---|
| 13 | % in the optimization problem |
---|
| 14 | pause % Strike any key to continue. |
---|
| 15 | |
---|
| 16 | % To define a symmetric 4x4 matrix, we write |
---|
| 17 | P = sdpvar(4,4,'symmetric'); |
---|
| 18 | pause % Strike any key to continue. |
---|
| 19 | |
---|
| 20 | % Square matrices are assumed to be symmetric by default, |
---|
| 21 | % so the same result is obtained with |
---|
| 22 | P = sdpvar(4,4); |
---|
| 23 | pause % Strike any key to continue. |
---|
| 24 | |
---|
| 25 | % If we want a fully parameterized matrix, |
---|
| 26 | % i.e. not neceserally symmetric, we write |
---|
| 27 | P = sdpvar(4,4,'full'); |
---|
| 28 | pause % Strike any key to continue. |
---|
| 29 | |
---|
| 30 | % Since non-square matrices are full by default, |
---|
| 31 | % the following two commands are equivalent |
---|
| 32 | P = sdpvar(3,4,'full'); |
---|
| 33 | P = sdpvar(3,4); |
---|
| 34 | pause % Strike any key to continue. |
---|
| 35 | |
---|
| 36 | % Of course, a scalar is defined as a 1x1 matrix |
---|
| 37 | x = sdpvar(1,1); |
---|
| 38 | pause % Strike any key to continue. |
---|
| 39 | |
---|
| 40 | % In addition to these two kind of matrices, YALMIP |
---|
| 41 | % also support Toeplitz, Hankel and skew-symmetric matrices |
---|
| 42 | % (all of these must be square) |
---|
| 43 | P = sdpvar(4,4,'toeplitz'); |
---|
| 44 | P = sdpvar(4,4,'hankel'); |
---|
| 45 | P = sdpvar(4,4,'skew'); |
---|
| 46 | pause % Strike any key to continue. |
---|
| 47 | |
---|
| 48 | clc |
---|
| 49 | |
---|
| 50 | % Now we know how to define symbolic variables, but how do we work |
---|
| 51 | % with them? Simple, just apply standard Matlab code! All linear operators |
---|
| 52 | % are overloaded. |
---|
| 53 | v = trace(P)+P(1,1)+sum(sum(P))*5; |
---|
| 54 | X = [P diag(P) v*eye(4)]; |
---|
| 55 | pause % Strike any key to continue. |
---|
| 56 | |
---|
| 57 | clc |
---|
| 58 | |
---|
| 59 | % Finally, a command that can be good in order to understand the basics |
---|
| 60 | % of the sdpvar object, the command see. All symbolic variables can be |
---|
| 61 | % written as a sum of base matrices |
---|
| 62 | % |
---|
| 63 | % P = P0+p1*P1+p2*P2+...+pn*Pn |
---|
| 64 | % |
---|
| 65 | % With see, it is possible to see what these base matrices look like. |
---|
| 66 | % Let us look at the base matrices needed to define a 2x2 symmetric matrix |
---|
| 67 | P = sdpvar(2,2); |
---|
| 68 | see(P) |
---|
| 69 | pause % Strike any key to continue. |
---|
| 70 | clc |
---|
| 71 | % If we perform any operation on a sdpvar object, this automatically changes |
---|
| 72 | % the base matrices |
---|
| 73 | see(eye(2)+4*P) |
---|
| 74 | pause % Strike any key to continue. |
---|
| 75 | |
---|
| 76 | clc |
---|
| 77 | %********************************************************* |
---|
| 78 | % |
---|
| 79 | % The second most important concept in YALMIP is the set |
---|
| 80 | % object. A set object is basically a collection of sdpvar |
---|
| 81 | % objects constrained to have some property, to be part |
---|
| 82 | % of a set, such as the set of positive semi-definite |
---|
| 83 | % matrices or the positive orthant. |
---|
| 84 | % |
---|
| 85 | % NOTE. |
---|
| 86 | % 1) The command SET when operating on sdpvar should not |
---|
| 87 | % be confused with the command SET used to alter properties |
---|
| 88 | % of, e.g, plots, in MATLAB. |
---|
| 89 | % 2) The command SET was previously called LMI in YALMIP, |
---|
| 90 | % and is based on the LMI object. |
---|
| 91 | % |
---|
| 92 | %********************************************************* |
---|
| 93 | |
---|
| 94 | pause % Strike any key to continue. |
---|
| 95 | |
---|
| 96 | % As a first example, constraining a Hermitian matrix to be |
---|
| 97 | % positive semi-definite is done by |
---|
| 98 | P = sdpvar(3,3); |
---|
| 99 | F = set(P > 0); |
---|
| 100 | pause % Strike any key to continue. |
---|
| 101 | |
---|
| 102 | % Element-wise inequalities are assumed if the argument is not Hermitian |
---|
| 103 | F = set(diag(P) > 0); |
---|
| 104 | pause % Strike any key to continue. |
---|
| 105 | |
---|
| 106 | % Element-wise constraints on a Hermitian matrix is most easily |
---|
| 107 | % done using the following code |
---|
| 108 | F = set(P(:) > 0); |
---|
| 109 | pause % Strike any key to continue. |
---|
| 110 | |
---|
| 111 | % A convenient extension in YALMIP 3 is constraint lists which |
---|
| 112 | % can be used, e.g., to define bounds easily |
---|
| 113 | F = set(0 < diag(P) < 5); |
---|
| 114 | pause % Strike any key to continue. |
---|
| 115 | |
---|
| 116 | % Finally, equality constraints are obtained with ==. |
---|
| 117 | % Constraining the diagonal elements to be zero is written as |
---|
| 118 | F = set(diag(P) == zeros(3,1)); |
---|
| 119 | pause % Strike any key to continue. |
---|
| 120 | |
---|
| 121 | % ...or (consistent with MATLABs notation) |
---|
| 122 | F = set(diag(P) == 0); |
---|
| 123 | pause % Strike any key to continue. |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | % Actually, comparasion between a scalar and a matrix is done in a |
---|
| 127 | % matlab fashion so the following more simple code is valid |
---|
| 128 | F = set(diag(P) == 0); |
---|
| 129 | pause % Strike any key to continue. |
---|
| 130 | |
---|
| 131 | % Alternatively, string notation can be used in all cases |
---|
| 132 | F = set('diag(P) == 0'); |
---|
| 133 | |
---|
| 134 | pause % Strike any key to continue. |
---|
| 135 | |
---|
| 136 | clc |
---|
| 137 | |
---|
| 138 | % Okey, we now know how to define one constraint. What if we have many? |
---|
| 139 | % Simple, just add the set objects (this gives the intersection of two constraints) |
---|
| 140 | F = set('P>0') + set(0 < diag(P) < 5) |
---|
| 141 | |
---|
| 142 | pause % Strike any key to continue. |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | clc |
---|
| 146 | % We can tag a set with a name |
---|
| 147 | F = set(P>0,'Positive definite')+set(diag(P)==0,'Zero diagonal'); |
---|
| 148 | pause % Strike any key to continue. |
---|
| 149 | |
---|
| 150 | % If we display the set, we see the tags |
---|
| 151 | F |
---|
| 152 | pause % Strike any key to continue. |
---|
| 153 | |
---|
| 154 | % The tags can be used for example when we want index the list |
---|
| 155 | % of sets. For example, let us extract the equality constraint |
---|
| 156 | F_equality = F('Zero diagonal') |
---|
| 157 | pause |
---|
| 158 | |
---|
| 159 | clc |
---|
| 160 | |
---|
| 161 | % To summarize, a constraint can be defined in two ways |
---|
| 162 | P = sdpvar(3,3); |
---|
| 163 | |
---|
| 164 | F = set('P>0'); |
---|
| 165 | |
---|
| 166 | F = set(P > 0); |
---|
| 167 | |
---|
| 168 | % ... are most easily built using overloaded + |
---|
| 169 | |
---|
| 170 | F = F + set(diag(P)==0) |
---|
| 171 | |
---|
| 172 | pause % Strike any key to continue. |
---|
| 173 | echo off |
---|