source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/opt/yalmip/demos/basicsex.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 4.8 KB
Line 
1clc
2echo 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
14pause % Strike any key to continue.
15
16% To define a symmetric 4x4 matrix, we write
17P = sdpvar(4,4,'symmetric');
18pause % Strike any key to continue.
19
20% Square matrices are assumed to be symmetric by default,
21% so the same result is obtained with
22P = sdpvar(4,4);
23pause % Strike any key to continue.
24
25% If we want a fully parameterized matrix,
26% i.e. not neceserally symmetric, we write
27P = sdpvar(4,4,'full');
28pause % Strike any key to continue.
29
30% Since non-square matrices are full by default,
31% the following two commands are equivalent
32P = sdpvar(3,4,'full');
33P = sdpvar(3,4);
34pause % Strike any key to continue.
35
36% Of course, a scalar is defined as a 1x1 matrix
37x = sdpvar(1,1);
38pause % 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)
43P = sdpvar(4,4,'toeplitz');
44P = sdpvar(4,4,'hankel');
45P = sdpvar(4,4,'skew');
46pause % Strike any key to continue.
47
48clc
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.
53v = trace(P)+P(1,1)+sum(sum(P))*5;
54X = [P diag(P) v*eye(4)];
55pause % Strike any key to continue.
56
57clc
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
67P = sdpvar(2,2);
68see(P)
69pause % Strike any key to continue.
70clc
71% If we perform any operation on a sdpvar object, this automatically changes
72% the base matrices
73see(eye(2)+4*P)
74pause % Strike any key to continue.
75
76clc
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
94pause % Strike any key to continue.
95
96% As a first example, constraining a Hermitian matrix to be
97% positive semi-definite is done by
98P = sdpvar(3,3);
99F = set(P > 0);
100pause % Strike any key to continue.
101
102% Element-wise inequalities are assumed if the argument is not Hermitian
103F = set(diag(P) > 0);
104pause % Strike any key to continue.
105
106% Element-wise constraints on a Hermitian matrix is most easily
107% done using the following code
108F = set(P(:) > 0);
109pause % 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
113F = set(0 < diag(P) < 5);
114pause % Strike any key to continue.
115
116% Finally, equality constraints are obtained with ==.
117% Constraining the diagonal elements to be zero is written as
118F = set(diag(P) == zeros(3,1));
119pause % Strike any key to continue.
120
121% ...or (consistent with MATLABs notation)
122F = set(diag(P) == 0);
123pause % 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
128F = set(diag(P) == 0);
129pause % Strike any key to continue.
130
131% Alternatively, string notation can be used in all cases
132F = set('diag(P) == 0');
133
134pause % Strike any key to continue.
135
136clc
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)
140F = set('P>0') + set(0 < diag(P) < 5)
141
142pause % Strike any key to continue.
143
144
145clc
146% We can tag a set with a name
147F = set(P>0,'Positive definite')+set(diag(P)==0,'Zero diagonal');
148pause % Strike any key to continue.
149
150% If we display the set, we see the tags
151F
152pause % 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
156F_equality = F('Zero diagonal')
157pause
158
159clc
160
161% To summarize, a constraint can be defined in two ways
162P = sdpvar(3,3);
163
164F = set('P>0');
165
166F = set(P > 0);
167
168% ... are most easily built using overloaded +
169
170F = F + set(diag(P)==0)
171
172pause % Strike any key to continue.
173echo off
Note: See TracBrowser for help on using the repository browser.