1 | <html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"> |
---|
2 | <head> |
---|
3 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
---|
4 | |
---|
5 | <!-- |
---|
6 | This HTML is auto-generated from an M-file. |
---|
7 | To make changes, update the M-file and republish this document. |
---|
8 | --> |
---|
9 | <title>fminsearchcon_demo</title> |
---|
10 | <meta name="generator" content="MATLAB 7.0.1"> |
---|
11 | <meta name="date" content="2006-12-16"> |
---|
12 | <meta name="m-file" content="fminsearchcon_demo"><style> |
---|
13 | body { |
---|
14 | background-color: white; |
---|
15 | margin:10px; |
---|
16 | } |
---|
17 | h1 { |
---|
18 | color: #990000; |
---|
19 | font-size: x-large; |
---|
20 | } |
---|
21 | h2 { |
---|
22 | color: #990000; |
---|
23 | font-size: medium; |
---|
24 | } |
---|
25 | p.footer { |
---|
26 | text-align: right; |
---|
27 | font-size: xx-small; |
---|
28 | font-weight: lighter; |
---|
29 | font-style: italic; |
---|
30 | color: gray; |
---|
31 | } |
---|
32 | |
---|
33 | pre.codeinput { |
---|
34 | margin-left: 30px; |
---|
35 | } |
---|
36 | |
---|
37 | span.keyword {color: #0000FF} |
---|
38 | span.comment {color: #228B22} |
---|
39 | span.string {color: #A020F0} |
---|
40 | span.untermstring {color: #B20000} |
---|
41 | span.syscmd {color: #B28C00} |
---|
42 | |
---|
43 | pre.showbuttons { |
---|
44 | margin-left: 30px; |
---|
45 | border: solid black 2px; |
---|
46 | padding: 4px; |
---|
47 | background: #EBEFF3; |
---|
48 | } |
---|
49 | |
---|
50 | pre.codeoutput { |
---|
51 | color: gray; |
---|
52 | font-style: italic; |
---|
53 | } |
---|
54 | pre.error { |
---|
55 | color: red; |
---|
56 | } |
---|
57 | |
---|
58 | /* Make the text shrink to fit narrow windows, but not stretch too far in |
---|
59 | wide windows. On Gecko-based browsers, the shrink-to-fit doesn't work. */ |
---|
60 | p,h1,h2,div { |
---|
61 | /* for MATLAB's browser */ |
---|
62 | width: 600px; |
---|
63 | /* for Mozilla, but the "width" tag overrides it anyway */ |
---|
64 | max-width: 600px; |
---|
65 | /* for IE */ |
---|
66 | width:expression(document.body.clientWidth > 620 ? "600px": "auto" ); |
---|
67 | } |
---|
68 | |
---|
69 | </style></head> |
---|
70 | <body> |
---|
71 | <h2>Contents</h2> |
---|
72 | <div> |
---|
73 | <ul> |
---|
74 | <li><a href="#1">Optimization of a simple (Rosenbrock) function</a></li> |
---|
75 | <li><a href="#2">Only lower bound constraints</a></li> |
---|
76 | <li><a href="#3">Only upper bound constraints</a></li> |
---|
77 | <li><a href="#4">Dual constraints</a></li> |
---|
78 | <li><a href="#5">Mixed constraints</a></li> |
---|
79 | <li><a href="#6">Fix a variable as constant, x(2) == 3</a></li> |
---|
80 | <li><a href="#7">Linear inequality, x(1) + x(2) <= 1</a></li> |
---|
81 | <li><a href="#8">Nonlinear inequality, norm(x) <= 1</a></li> |
---|
82 | <li><a href="#9">Minimize a linear objective, subject to a nonlinear constraints.</a></li> |
---|
83 | <li><a href="#10">Provide your own fminsearch options</a></li> |
---|
84 | <li><a href="#11">Exactly fix one variable, constrain some others, and set a tolerance</a></li> |
---|
85 | <li><a href="#12">All the standard outputs from fminsearch are still returned</a></li> |
---|
86 | </ul> |
---|
87 | </div> |
---|
88 | <h2>Optimization of a simple (Rosenbrock) function<a name="1"></a></h2><pre class="codeinput">rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2; |
---|
89 | |
---|
90 | <span class="comment">% With no constraints, operation simply passes through</span> |
---|
91 | <span class="comment">% directly to fminsearch. The solution should be [1 1]</span> |
---|
92 | xsol = fminsearchcon(rosen,[3 3]) |
---|
93 | </pre><pre class="codeoutput"> |
---|
94 | xsol = |
---|
95 | |
---|
96 | 0.99998 0.99995 |
---|
97 | |
---|
98 | </pre><h2>Only lower bound constraints<a name="2"></a></h2><pre class="codeinput">xsol = fminsearchcon(rosen,[3 3],[2 2]) |
---|
99 | </pre><pre class="codeoutput"> |
---|
100 | xsol = |
---|
101 | |
---|
102 | 2 4 |
---|
103 | |
---|
104 | </pre><h2>Only upper bound constraints<a name="3"></a></h2><pre class="codeinput">xsol = fminsearchcon(rosen,[-5 -5],[],[0 0]) |
---|
105 | </pre><pre class="codeoutput"> |
---|
106 | xsol = |
---|
107 | |
---|
108 | -1.0447e-13 -1.4451e-08 |
---|
109 | |
---|
110 | </pre><h2>Dual constraints<a name="4"></a></h2><pre class="codeinput">xsol = fminsearchcon(rosen,[2.5 2.5],[2 2],[3 3]) |
---|
111 | </pre><pre class="codeoutput"> |
---|
112 | xsol = |
---|
113 | |
---|
114 | 2 3 |
---|
115 | |
---|
116 | </pre><h2>Mixed constraints<a name="5"></a></h2><pre class="codeinput">xsol = fminsearchcon(rosen,[0 0],[2 -inf],[inf 3]) |
---|
117 | </pre><pre class="codeoutput"> |
---|
118 | xsol = |
---|
119 | |
---|
120 | 2 3 |
---|
121 | |
---|
122 | </pre><h2>Fix a variable as constant, x(2) == 3<a name="6"></a></h2><pre class="codeinput">fminsearchcon(rosen,[3 3],[-inf 3],[inf,3]) |
---|
123 | </pre><pre class="codeoutput"> |
---|
124 | ans = |
---|
125 | |
---|
126 | 1.7314 3 |
---|
127 | |
---|
128 | </pre><h2>Linear inequality, x(1) + x(2) <= 1<a name="7"></a></h2><pre class="codeinput">fminsearchcon(rosen,[0 0],[],[],[1 1],1) |
---|
129 | </pre><pre class="codeoutput"> |
---|
130 | ans = |
---|
131 | |
---|
132 | 0.6187 0.3813 |
---|
133 | |
---|
134 | </pre><h2>Nonlinear inequality, norm(x) <= 1<a name="8"></a></h2><pre class="codeinput">fminsearchcon(rosen,[0 0],[],[],[],[],@(x) norm(x) - 1) |
---|
135 | </pre><pre class="codeoutput"> |
---|
136 | ans = |
---|
137 | |
---|
138 | 0.78633 0.61778 |
---|
139 | |
---|
140 | </pre><h2>Minimize a linear objective, subject to a nonlinear constraints.<a name="9"></a></h2><pre class="codeinput">fun = @(x) x*[-2;1]; |
---|
141 | nonlcon = @(x) [norm(x) - 1;sin(sum(x))]; |
---|
142 | fminsearchcon(fun,[0 0],[],[],[],[],nonlcon) |
---|
143 | </pre><pre class="codeoutput"> |
---|
144 | ans = |
---|
145 | |
---|
146 | 0.70707 -0.70713 |
---|
147 | |
---|
148 | </pre><h2>Provide your own fminsearch options<a name="10"></a></h2><pre class="codeinput">opts = optimset(<span class="string">'fminsearch'</span>); |
---|
149 | opts.Display = <span class="string">'iter'</span>; |
---|
150 | opts.TolX = 1.e-12; |
---|
151 | opts.MaxFunEvals = 100; |
---|
152 | |
---|
153 | n = [10,5]; |
---|
154 | H = randn(n); |
---|
155 | H=H'*H; |
---|
156 | Quadraticfun = @(x) x*H*x'; |
---|
157 | |
---|
158 | <span class="comment">% Global minimizer is at [0 0 0 0 0].</span> |
---|
159 | <span class="comment">% Set all lower bound constraints, all of which will</span> |
---|
160 | <span class="comment">% be active in this test.</span> |
---|
161 | LB = [.5 .5 .5 .5 .5]; |
---|
162 | xsol = fminsearchcon(Quadraticfun,[1 2 3 4 5],LB,[],[],[],[],opts) |
---|
163 | </pre><pre class="codeoutput"> |
---|
164 | Iteration Func-count min f(x) Procedure |
---|
165 | 0 1 351.442 |
---|
166 | 1 6 351.442 initial simplex |
---|
167 | 2 8 296.007 expand |
---|
168 | 3 9 296.007 reflect |
---|
169 | 4 10 296.007 reflect |
---|
170 | 5 11 296.007 reflect |
---|
171 | 6 13 248.853 expand |
---|
172 | 7 15 209.119 expand |
---|
173 | 8 16 209.119 reflect |
---|
174 | 9 17 209.119 reflect |
---|
175 | 10 19 171.673 expand |
---|
176 | 11 21 132.166 expand |
---|
177 | 12 22 132.166 reflect |
---|
178 | 13 23 132.166 reflect |
---|
179 | 14 24 132.166 reflect |
---|
180 | 15 25 132.166 reflect |
---|
181 | 16 27 129.206 reflect |
---|
182 | 17 28 129.206 reflect |
---|
183 | 18 30 124.943 reflect |
---|
184 | 19 32 124.943 contract inside |
---|
185 | 20 34 123.809 reflect |
---|
186 | 21 36 107.233 expand |
---|
187 | 22 38 107.233 contract outside |
---|
188 | 23 39 107.233 reflect |
---|
189 | 24 41 98.5266 expand |
---|
190 | 25 42 98.5266 reflect |
---|
191 | 26 44 94.0621 expand |
---|
192 | 27 46 87.7403 expand |
---|
193 | 28 48 86.0587 reflect |
---|
194 | 29 49 86.0587 reflect |
---|
195 | 30 50 86.0587 reflect |
---|
196 | 31 51 86.0587 reflect |
---|
197 | 32 53 85.8138 reflect |
---|
198 | 33 55 85.8138 contract inside |
---|
199 | 34 57 73.0352 expand |
---|
200 | 35 59 73.0352 contract inside |
---|
201 | 36 60 73.0352 reflect |
---|
202 | 37 61 73.0352 reflect |
---|
203 | 38 62 73.0352 reflect |
---|
204 | 39 64 64.9347 expand |
---|
205 | 40 66 62.3724 expand |
---|
206 | 41 68 62.3126 reflect |
---|
207 | 42 69 62.3126 reflect |
---|
208 | 43 71 57.2349 reflect |
---|
209 | 44 73 49.999 expand |
---|
210 | 45 74 49.999 reflect |
---|
211 | 46 75 49.999 reflect |
---|
212 | 47 76 49.999 reflect |
---|
213 | 48 78 48.1326 reflect |
---|
214 | 49 79 48.1326 reflect |
---|
215 | 50 81 48.1326 contract inside |
---|
216 | 51 83 46.1196 reflect |
---|
217 | 52 85 46.1196 contract inside |
---|
218 | 53 87 43.1862 reflect |
---|
219 | 54 88 43.1862 reflect |
---|
220 | 55 90 43.1862 contract inside |
---|
221 | 56 91 43.1862 reflect |
---|
222 | 57 92 43.1862 reflect |
---|
223 | 58 94 42.6876 reflect |
---|
224 | 59 95 42.6876 reflect |
---|
225 | 60 97 42.6876 contract outside |
---|
226 | 61 99 41.6298 reflect |
---|
227 | 62 100 41.6298 reflect |
---|
228 | |
---|
229 | Exiting: Maximum number of function evaluations has been exceeded |
---|
230 | - increase MaxFunEvals option. |
---|
231 | Current function value: 41.629797 |
---|
232 | |
---|
233 | |
---|
234 | xsol = |
---|
235 | |
---|
236 | 1.652 0.54831 2.4744 1.2291 0.56971 |
---|
237 | |
---|
238 | </pre><h2>Exactly fix one variable, constrain some others, and set a tolerance<a name="11"></a></h2><pre class="codeinput">opts = optimset(<span class="string">'fminsearch'</span>); |
---|
239 | opts.TolFun = 1.e-12; |
---|
240 | |
---|
241 | LB = [-inf 2 1 -10]; |
---|
242 | UB = [ inf inf 1 inf]; |
---|
243 | xsol = fminsearchcon(@(x) norm(x),[1 3 1 1],LB,UB,[],[],[],opts) |
---|
244 | </pre><pre class="codeoutput"> |
---|
245 | xsol = |
---|
246 | |
---|
247 | -4.9034e-07 2 1 5.1394e-07 |
---|
248 | |
---|
249 | </pre><h2>All the standard outputs from fminsearch are still returned<a name="12"></a></h2><pre class="codeinput">[xsol,fval,exitflag,output] = fminsearchcon(@(x) norm(x),[1 3 1 1],LB,UB) |
---|
250 | </pre><pre class="codeoutput"> |
---|
251 | xsol = |
---|
252 | |
---|
253 | 3.1094e-05 2 1 -5.1706e-05 |
---|
254 | |
---|
255 | |
---|
256 | fval = |
---|
257 | |
---|
258 | 2.2361 |
---|
259 | |
---|
260 | |
---|
261 | exitflag = |
---|
262 | |
---|
263 | 1 |
---|
264 | |
---|
265 | |
---|
266 | output = |
---|
267 | |
---|
268 | iterations: 77 |
---|
269 | funcCount: 138 |
---|
270 | algorithm: 'Nelder-Mead simplex direct search' |
---|
271 | message: [1x194 char] |
---|
272 | |
---|
273 | </pre><p class="footer"><br> |
---|
274 | Published with MATLAB® 7.0.1<br></p> |
---|
275 | <!-- |
---|
276 | ##### SOURCE BEGIN ##### |
---|
277 | %% Optimization of a simple (Rosenbrock) function |
---|
278 | rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2; |
---|
279 | |
---|
280 | % With no constraints, operation simply passes through |
---|
281 | % directly to fminsearch. The solution should be [1 1] |
---|
282 | xsol = fminsearchcon(rosen,[3 3]) |
---|
283 | |
---|
284 | %% Only lower bound constraints |
---|
285 | xsol = fminsearchcon(rosen,[3 3],[2 2]) |
---|
286 | |
---|
287 | %% Only upper bound constraints |
---|
288 | xsol = fminsearchcon(rosen,[-5 -5],[],[0 0]) |
---|
289 | |
---|
290 | %% Dual constraints |
---|
291 | xsol = fminsearchcon(rosen,[2.5 2.5],[2 2],[3 3]) |
---|
292 | |
---|
293 | %% Mixed constraints |
---|
294 | xsol = fminsearchcon(rosen,[0 0],[2 -inf],[inf 3]) |
---|
295 | |
---|
296 | %% Fix a variable as constant, x(2) == 3 |
---|
297 | fminsearchcon(rosen,[3 3],[-inf 3],[inf,3]) |
---|
298 | |
---|
299 | %% Linear inequality, x(1) + x(2) <= 1 |
---|
300 | fminsearchcon(rosen,[0 0],[],[],[1 1],1) |
---|
301 | |
---|
302 | %% Nonlinear inequality, norm(x) <= 1 |
---|
303 | fminsearchcon(rosen,[0 0],[],[],[],[],@(x) norm(x) - 1) |
---|
304 | |
---|
305 | %% Minimize a linear objective, subject to a nonlinear constraints. |
---|
306 | fun = @(x) x*[-2;1]; |
---|
307 | nonlcon = @(x) [norm(x) - 1;sin(sum(x))]; |
---|
308 | fminsearchcon(fun,[0 0],[],[],[],[],nonlcon) |
---|
309 | |
---|
310 | %% Provide your own fminsearch options |
---|
311 | opts = optimset('fminsearch'); |
---|
312 | opts.Display = 'iter'; |
---|
313 | opts.TolX = 1.e-12; |
---|
314 | opts.MaxFunEvals = 100; |
---|
315 | |
---|
316 | n = [10,5]; |
---|
317 | H = randn(n); |
---|
318 | H=H'*H; |
---|
319 | Quadraticfun = @(x) x*H*x'; |
---|
320 | |
---|
321 | % Global minimizer is at [0 0 0 0 0]. |
---|
322 | % Set all lower bound constraints, all of which will |
---|
323 | % be active in this test. |
---|
324 | LB = [.5 .5 .5 .5 .5]; |
---|
325 | xsol = fminsearchcon(Quadraticfun,[1 2 3 4 5],LB,[],[],[],[],opts) |
---|
326 | |
---|
327 | %% Exactly fix one variable, constrain some others, and set a tolerance |
---|
328 | opts = optimset('fminsearch'); |
---|
329 | opts.TolFun = 1.e-12; |
---|
330 | |
---|
331 | LB = [-inf 2 1 -10]; |
---|
332 | UB = [ inf inf 1 inf]; |
---|
333 | xsol = fminsearchcon(@(x) norm(x),[1 3 1 1],LB,UB,[],[],[],opts) |
---|
334 | |
---|
335 | %% All the standard outputs from fminsearch are still returned |
---|
336 | [xsol,fval,exitflag,output] = fminsearchcon(@(x) norm(x),[1 3 1 1],LB,UB) |
---|
337 | |
---|
338 | |
---|
339 | ##### SOURCE END ##### |
---|
340 | --> |
---|
341 | </body> |
---|
342 | </html> |
---|