[37] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
---|
| 2 | <html> |
---|
| 3 | |
---|
| 4 | <head> |
---|
| 5 | <meta http-equiv="Content-Language" content="en-us"> |
---|
| 6 | <title>YALMIP Example : Advanced programming</title> |
---|
| 7 | <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"> |
---|
| 8 | <meta content="Microsoft FrontPage 6.0" name="GENERATOR"> |
---|
| 9 | <meta name="ProgId" content="FrontPage.Editor.Document"> |
---|
| 10 | <link href="yalmip.css" type="text/css" rel="stylesheet"> |
---|
| 11 | <base target="_self"> |
---|
| 12 | </head> |
---|
| 13 | |
---|
| 14 | <body leftMargin="0" topMargin="0"> |
---|
| 15 | |
---|
| 16 | <div align="left"> |
---|
| 17 | <table border="0" cellpadding="4" cellspacing="3" style="border-collapse: collapse" bordercolor="#000000" width="100%" align="left" height="100%"> |
---|
| 18 | <tr> |
---|
| 19 | <td width="100%" align="left" height="100%" valign="top"> |
---|
| 20 | <h2>Advanced YALMIP programming</h2> |
---|
| 21 | <hr noShade SIZE="1" color="#000000"> |
---|
| 22 | <p>In some cases, it might be necessary to do more advanced YALMIP coding. |
---|
| 23 | YALMIP comes with a number of commands to facilitate this.</p> |
---|
| 24 | <p>Let us begin by defining a set of variables.</p> |
---|
| 25 | <table cellPadding="10" width="100%"> |
---|
| 26 | <tr> |
---|
| 27 | <td class="xmpcode"> |
---|
| 28 | <pre>x = sdpvar(2,1); |
---|
| 29 | y = x'*randn(2)*x;</pre> |
---|
| 30 | </td> |
---|
| 31 | </tr> |
---|
| 32 | </table> |
---|
| 33 | <p>A common task is to find variables used in an expression. To do this, there are |
---|
| 34 | two important commands, <code>depends.m </code>and <code>getvariables.m</code>.</p> |
---|
| 35 | <table cellPadding="10" width="100%"> |
---|
| 36 | <tr> |
---|
| 37 | <td class="xmpcode"> |
---|
| 38 | <pre>depends(x) |
---|
| 39 | <font color="#000000">ans = |
---|
| 40 | 1 2</font> |
---|
| 41 | |
---|
| 42 | depends(y) |
---|
| 43 | <font color="#000000"> ans = |
---|
| 44 | 1 2</font> |
---|
| 45 | |
---|
| 46 | getvariables(x) |
---|
| 47 | <font color="#000000"> ans = |
---|
| 48 | 1 2</font> |
---|
| 49 | |
---|
| 50 | getvariables(y) |
---|
| 51 | <font color="#000000"> ans = |
---|
| 52 | 3 4 5</font></pre> |
---|
| 53 | </td> |
---|
| 54 | </tr> |
---|
| 55 | </table> |
---|
| 56 | <p>The command <code>depends.m</code> gives the actual linear independent variables, while <code>getvariables.m</code> returns the |
---|
| 57 | variable indices to the so called relaxed variables used in YALMIP. When |
---|
| 58 | a nonlinear expression is defined in YALMIP, a new variable is introduced |
---|
| 59 | for every monomial term. For our quadratic variable y, we need 3 monomials.</p> |
---|
| 60 | <table cellPadding="10" width="100%"> |
---|
| 61 | <tr> |
---|
| 62 | <td class="xmpcode"> |
---|
| 63 | <pre>getvariables(x(1)^2) |
---|
| 64 | <font color="#000000"> ans = |
---|
| 65 | 3</font> |
---|
| 66 | |
---|
| 67 | getvariables(x(2)^2) |
---|
| 68 | <font color="#000000">ans = |
---|
| 69 | 4</font></pre> |
---|
| 70 | <pre>getvariables(x(1)*x(2)) |
---|
| 71 | <font color="#000000"> ans = |
---|
| 72 | 5</font></pre> |
---|
| 73 | </td> |
---|
| 74 | </tr> |
---|
| 75 | </table> |
---|
| 76 | <p>The commands above only give the internal identifiers. To create an |
---|
| 77 | <a href="reference.htm#sdpvar">sdpvar</a> variable using these variables, |
---|
| 78 | we use the command <code>recover.m</code></p> |
---|
| 79 | <table cellPadding="10" width="100%"> |
---|
| 80 | <tr> |
---|
| 81 | <td class="xmpcode"> |
---|
| 82 | <pre>variables_in_y = recover(depends(y)) |
---|
| 83 | <font color="#000000">Linear matrix variable 2x1 (full, real)</font></pre> |
---|
| 84 | </td> |
---|
| 85 | </tr> |
---|
| 86 | </table> |
---|
| 87 | <p>All <a href="reference.htm#sdpvar">sdpvar</a> |
---|
| 88 | objects are defined by the variable indicies obtained with <code>getvariables.m</code>, |
---|
| 89 | and a basis. Let us begin with scalar variables to explain the concepts. |
---|
| 90 | The basis with respect to all variables returned from <code>getvariables.m </code> |
---|
| 91 | is obtained using <code>getbase.m</code></p> |
---|
| 92 | <table cellPadding="10" width="100%"> |
---|
| 93 | <tr> |
---|
| 94 | <td class="xmpcode"> |
---|
| 95 | <pre>x = sdpvar(1,1); |
---|
| 96 | y = sdpvar(1,1); |
---|
| 97 | z = pi+2*x+3*y; |
---|
| 98 | full(getbase(z)) |
---|
| 99 | <font color="#000000"> |
---|
| 100 | ans = |
---|
| 101 | |
---|
| 102 | 3.1416 2.0000 3.0000</font></pre> |
---|
| 103 | </td> |
---|
| 104 | </tr> |
---|
| 105 | </table> |
---|
| 106 | <p>A basis with respect to a particular variable is obtained using <code>getbasematrix.m.</code></p> |
---|
| 107 | <table cellPadding="10" width="100%"> |
---|
| 108 | <tr> |
---|
| 109 | <td class="xmpcode"> |
---|
| 110 | <pre>full(getbasematrix(z,getvariables(x))) |
---|
| 111 | <font color="#000000"> |
---|
| 112 | ans = |
---|
| 113 | |
---|
| 114 | 2 |
---|
| 115 | </font> |
---|
| 116 | full(getbasematrix(z,0)) |
---|
| 117 | <font color="#000000"> |
---|
| 118 | ans = |
---|
| 119 | |
---|
| 120 | 3.1416</font> |
---|
| 121 | |
---|
| 122 | full(getbasematrix(z,123456789)) |
---|
| 123 | <font color="#000000"> |
---|
| 124 | ans = |
---|
| 125 | |
---|
| 126 | 0</font></pre> |
---|
| 127 | </td> |
---|
| 128 | </tr> |
---|
| 129 | </table> |
---|
| 130 | <p>Matrix variables are defined in the same way, except that all basis |
---|
| 131 | matrices are stored in a vectorized format.</p> |
---|
| 132 | <table cellPadding="10" width="100%"> |
---|
| 133 | <tr> |
---|
| 134 | <td class="xmpcode"> |
---|
| 135 | <pre>x = pi*eye(2)+5*sdpvar(2,2); |
---|
| 136 | base = full(getbase(x)) |
---|
| 137 | <font color="#000000"> base = |
---|
| 138 | |
---|
| 139 | 3.1416 5.0000 0 0 |
---|
| 140 | 0 0 5.0000 0 |
---|
| 141 | 0 0 5.0000 0 |
---|
| 142 | 3.1416 0 0 5.0000</font> |
---|
| 143 | |
---|
| 144 | reshape(base(:,1),2,2) |
---|
| 145 | <font color="#000000"> ans = |
---|
| 146 | |
---|
| 147 | 3.1416 0 |
---|
| 148 | 0 3.1416</font></pre> |
---|
| 149 | </td> |
---|
| 150 | </tr> |
---|
| 151 | </table> |
---|
| 152 | <p>The command <a href="reference.htm#is">is</a> can be useful to select |
---|
| 153 | parts of <a href="reference.htm#set">set</a> objects</p> |
---|
| 154 | <table cellPadding="10" width="100%"> |
---|
| 155 | <tr> |
---|
| 156 | <td class="xmpcode"> |
---|
| 157 | <pre>F = set(x>0) + lmi(x*x' > 0); |
---|
| 158 | F_linear = F(find(is(F,'linear')))</pre> |
---|
| 159 | </td> |
---|
| 160 | </tr> |
---|
| 161 | </table> |
---|
| 162 | <p><code>replace.m</code> can be useful in some situations to replace an |
---|
| 163 | <a href="reference.htm#sdpvar">sdpvar</a> inside an |
---|
| 164 | <a href="reference.htm#sdpvar">sdpvar</a> or <a href="reference.htm#set"> |
---|
| 165 | set</a>.</p> |
---|
| 166 | <table cellPadding="10" width="100%"> |
---|
| 167 | <tr> |
---|
| 168 | <td class="xmpcode"> |
---|
| 169 | <pre>F0 = replace(F,x(1),0); |
---|
| 170 | y0 = replace(y,x(1),0);</pre> |
---|
| 171 | </td> |
---|
| 172 | </tr> |
---|
| 173 | </table> |
---|
| 174 | <p>The terms in a <a href="reference.htm#set">set</a> object can be |
---|
| 175 | extracted and converted to <a href="reference.htm#sdpvar">sdpvar</a> |
---|
| 176 | objects.</p> |
---|
| 177 | <table cellPadding="10" width="100%"> |
---|
| 178 | <tr> |
---|
| 179 | <td class="xmpcode"> |
---|
| 180 | <pre>xtimesx = sdpvar(F(2));</pre> |
---|
| 181 | </td> |
---|
| 182 | </tr> |
---|
| 183 | </table> |
---|
| 184 | <p>More to come but do not hesitate to ask.</td> |
---|
| 185 | </tr> |
---|
| 186 | </table> |
---|
| 187 | </div> |
---|
| 188 | |
---|
| 189 | </body> |
---|
| 190 | |
---|
| 191 | </html> |
---|