1 | clc |
---|
2 | echo on |
---|
3 | |
---|
4 | % Starting from YALMIP version 3, polynomial expressions are supported. |
---|
5 | % |
---|
6 | % These nonlinear expressions can be used for, e.g. |
---|
7 | % SDPs with BMI constraints, or to solve sum-of-squares problems |
---|
8 | % |
---|
9 | % The nonlinear expressions are built up from sdpvar objects, |
---|
10 | % and are manipulated in same way |
---|
11 | pause % Strike any key to continue. |
---|
12 | yalmip('clear') |
---|
13 | clc |
---|
14 | |
---|
15 | % To begin with, define a scalar variable |
---|
16 | x = sdpvar(1,1); |
---|
17 | pause % Strike any key to continue. |
---|
18 | |
---|
19 | % Nonlinear expressions are easily built |
---|
20 | p = 1+x+x^2+x^3; |
---|
21 | pause % Strike any key to continue. |
---|
22 | |
---|
23 | % Matrices can also be nonlinear |
---|
24 | Y = sdpvar(3,3); |
---|
25 | Z = Y*Y+Y.*Y; |
---|
26 | pause |
---|
27 | |
---|
28 | % Polynomials are displayed without any symbolic information... |
---|
29 | y = sdpvar(1,1); |
---|
30 | p = x*x+y^4+x*y |
---|
31 | pause |
---|
32 | |
---|
33 | % But can be displayed better with the command sdisplay |
---|
34 | % (this only works if the involved variables are explictely |
---|
35 | % defined as scalars) |
---|
36 | sdisplay(p) |
---|
37 | pause |
---|
38 | |
---|
39 | % Polynomials can be diffrentiated |
---|
40 | dp = jacobian(p); |
---|
41 | sdisplay(dp) |
---|
42 | pause |
---|
43 | |
---|
44 | % ...w.r.t a specific variable |
---|
45 | dp = jacobian(p,y); |
---|
46 | sdisplay(dp) |
---|
47 | pause |
---|
48 | |
---|
49 | |
---|
50 | % ...why not twice |
---|
51 | sdisplay(jacobian(jacobian(p)')) |
---|
52 | pause |
---|
53 | |
---|
54 | % ...or |
---|
55 | sdisplay(hessian(p)) |
---|
56 | pause |
---|
57 | |
---|
58 | % Of course, all standard linear operators applies |
---|
59 | % to the nonlinear objects |
---|
60 | x = sdpvar(3,1); |
---|
61 | p = trace(x*x') + sum(x.^2) |
---|
62 | pause |
---|
63 | |
---|
64 | clc |
---|
65 | % Finally, a word of caution. Use yalmip('clear') |
---|
66 | % while working with polynomial expressions. |
---|
67 | % |
---|
68 | % The reason is that every time a nonlinear variable is |
---|
69 | % defined, a description on how it is created is |
---|
70 | % saved inside YALMIP. With many nonlinear terms |
---|
71 | % this list grows fast, making YALMIP slower and slower |
---|
72 | % since the list has to be searched in when polynomial |
---|
73 | % expressions are manipulated. |
---|
74 | % |
---|
75 | % (after this short session, there are already 27 nonlinear terms) |
---|
76 | pause |
---|
77 | echo off |
---|