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>fminsearchbnd_demo</title> |
---|
10 | <meta name="generator" content="MATLAB 7.0.1"> |
---|
11 | <meta name="date" content="2006-07-24"> |
---|
12 | <meta name="m-file" content="fminsearchbnd_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, with no constraints</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">Provide your own fminsearch options</a></li> |
---|
80 | <li><a href="#7">Exactly fix one variable, constrain some others, and set a tolerance</a></li> |
---|
81 | <li><a href="#8">All the standard outputs from fminsearch are still returned</a></li> |
---|
82 | </ul> |
---|
83 | </div> |
---|
84 | <h2>Optimization of a simple (Rosenbrock) function, with no constraints<a name="1"></a></h2><pre class="codeinput">rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2; |
---|
85 | |
---|
86 | <span class="comment">% With no constraints, operation simply passes through</span> |
---|
87 | <span class="comment">% directly to fminsearch. The solution should be [1 1]</span> |
---|
88 | xsol = fminsearchbnd(rosen,[3 3]) |
---|
89 | </pre><pre class="codeoutput"> |
---|
90 | xsol = |
---|
91 | |
---|
92 | 0.99998 0.99995 |
---|
93 | |
---|
94 | </pre><h2>Only lower bound constraints<a name="2"></a></h2><pre class="codeinput">xsol = fminsearchbnd(rosen,[3 3],[2 2]) |
---|
95 | </pre><pre class="codeoutput"> |
---|
96 | xsol = |
---|
97 | |
---|
98 | 2 4 |
---|
99 | |
---|
100 | </pre><h2>Only upper bound constraints<a name="3"></a></h2><pre class="codeinput">xsol = fminsearchbnd(rosen,[-5 -5],[],[0 0]) |
---|
101 | </pre><pre class="codeoutput"> |
---|
102 | xsol = |
---|
103 | |
---|
104 | -1.0447e-13 -1.4451e-08 |
---|
105 | |
---|
106 | </pre><h2>Dual constraints<a name="4"></a></h2><pre class="codeinput">xsol = fminsearchbnd(rosen,[2.5 2.5],[2 2],[3 3]) |
---|
107 | </pre><pre class="codeoutput"> |
---|
108 | xsol = |
---|
109 | |
---|
110 | 2 3 |
---|
111 | |
---|
112 | </pre><h2>Mixed constraints<a name="5"></a></h2><pre class="codeinput">xsol = fminsearchbnd(rosen,[0 0],[2 -inf],[inf 3]) |
---|
113 | </pre><pre class="codeoutput"> |
---|
114 | xsol = |
---|
115 | |
---|
116 | 2 3 |
---|
117 | |
---|
118 | </pre><h2>Provide your own fminsearch options<a name="6"></a></h2><pre class="codeinput">opts = optimset(<span class="string">'fminsearch'</span>); |
---|
119 | opts.Display = <span class="string">'iter'</span>; |
---|
120 | opts.TolX = 1.e-12; |
---|
121 | opts.MaxFunEvals = 100; |
---|
122 | |
---|
123 | n = [10,5]; |
---|
124 | H = randn(n); |
---|
125 | H=H'*H; |
---|
126 | Quadraticfun = @(x) x*H*x'; |
---|
127 | |
---|
128 | <span class="comment">% Global minimizer is at [0 0 0 0 0].</span> |
---|
129 | <span class="comment">% Set all lower bound constraints, all of which will</span> |
---|
130 | <span class="comment">% be active in this test.</span> |
---|
131 | LB = [.5 .5 .5 .5 .5]; |
---|
132 | xsol = fminsearchbnd(Quadraticfun,[1 2 3 4 5],LB,[],opts) |
---|
133 | </pre><pre class="codeoutput"> |
---|
134 | Iteration Func-count min f(x) Procedure |
---|
135 | 0 1 173.731 |
---|
136 | 1 6 172.028 initial simplex |
---|
137 | 2 8 162.698 expand |
---|
138 | 3 9 162.698 reflect |
---|
139 | 4 11 151.902 expand |
---|
140 | 5 13 138.235 expand |
---|
141 | 6 14 138.235 reflect |
---|
142 | 7 16 126.604 expand |
---|
143 | 8 17 126.604 reflect |
---|
144 | 9 19 97.3266 expand |
---|
145 | 10 20 97.3266 reflect |
---|
146 | 11 21 97.3266 reflect |
---|
147 | 12 22 97.3266 reflect |
---|
148 | 13 24 73.7178 expand |
---|
149 | 14 25 73.7178 reflect |
---|
150 | 15 26 73.7178 reflect |
---|
151 | 16 28 50.8236 expand |
---|
152 | 17 29 50.8236 reflect |
---|
153 | 18 31 41.6294 expand |
---|
154 | 19 33 30.4252 expand |
---|
155 | 20 34 30.4252 reflect |
---|
156 | 21 36 27.782 reflect |
---|
157 | 22 37 27.782 reflect |
---|
158 | 23 39 27.782 contract inside |
---|
159 | 24 41 22.6509 reflect |
---|
160 | 25 42 22.6509 reflect |
---|
161 | 26 43 22.6509 reflect |
---|
162 | 27 44 22.6509 reflect |
---|
163 | 28 45 22.6509 reflect |
---|
164 | 29 47 21.0211 reflect |
---|
165 | 30 48 21.0211 reflect |
---|
166 | 31 49 21.0211 reflect |
---|
167 | 32 51 21.0211 contract inside |
---|
168 | 33 52 21.0211 reflect |
---|
169 | 34 54 20.7613 contract inside |
---|
170 | 35 55 20.7613 reflect |
---|
171 | 36 56 20.7613 reflect |
---|
172 | 37 57 20.7613 reflect |
---|
173 | 38 59 20.6012 contract inside |
---|
174 | 39 61 20.5324 contract inside |
---|
175 | 40 63 20.4961 contract inside |
---|
176 | 41 65 20.3886 contract inside |
---|
177 | 42 67 20.2121 reflect |
---|
178 | 43 69 20.0876 contract inside |
---|
179 | 44 71 19.9164 reflect |
---|
180 | 45 72 19.9164 reflect |
---|
181 | 46 74 19.9164 contract inside |
---|
182 | 47 76 19.9164 contract outside |
---|
183 | 48 78 19.3349 expand |
---|
184 | 49 80 19.3349 contract inside |
---|
185 | 50 81 19.3349 reflect |
---|
186 | 51 82 19.3349 reflect |
---|
187 | 52 84 18.8721 expand |
---|
188 | 53 85 18.8721 reflect |
---|
189 | 54 87 18.6427 expand |
---|
190 | 55 89 17.4548 expand |
---|
191 | 56 90 17.4548 reflect |
---|
192 | 57 92 16.0113 expand |
---|
193 | 58 93 16.0113 reflect |
---|
194 | 59 94 16.0113 reflect |
---|
195 | 60 96 14.6134 expand |
---|
196 | 61 98 12.5445 expand |
---|
197 | 62 99 12.5445 reflect |
---|
198 | 63 101 10.7311 expand |
---|
199 | |
---|
200 | Exiting: Maximum number of function evaluations has been exceeded |
---|
201 | - increase MaxFunEvals option. |
---|
202 | Current function value: 10.731146 |
---|
203 | |
---|
204 | |
---|
205 | xsol = |
---|
206 | |
---|
207 | 1.7022 1.0787 1.2034 0.5006 0.64666 |
---|
208 | |
---|
209 | </pre><h2>Exactly fix one variable, constrain some others, and set a tolerance<a name="7"></a></h2><pre class="codeinput">opts = optimset(<span class="string">'fminsearch'</span>); |
---|
210 | opts.TolFun = 1.e-12; |
---|
211 | |
---|
212 | LB = [-inf 2 1 -10]; |
---|
213 | UB = [ inf inf 1 inf]; |
---|
214 | xsol = fminsearchbnd(@(x) norm(x),[1 3 1 1],LB,UB,opts) |
---|
215 | </pre><pre class="codeoutput"> |
---|
216 | xsol = |
---|
217 | |
---|
218 | -4.9034e-07 2 1 5.1394e-07 |
---|
219 | |
---|
220 | </pre><h2>All the standard outputs from fminsearch are still returned<a name="8"></a></h2><pre class="codeinput">[xsol,fval,exitflag,output] = fminsearchbnd(@(x) norm(x),[1 3 1 1],LB,UB) |
---|
221 | </pre><pre class="codeoutput"> |
---|
222 | xsol = |
---|
223 | |
---|
224 | 3.1094e-05 2 1 -5.1706e-05 |
---|
225 | |
---|
226 | |
---|
227 | fval = |
---|
228 | |
---|
229 | 2.2361 |
---|
230 | |
---|
231 | |
---|
232 | exitflag = |
---|
233 | |
---|
234 | 1 |
---|
235 | |
---|
236 | |
---|
237 | output = |
---|
238 | |
---|
239 | iterations: 77 |
---|
240 | funcCount: 138 |
---|
241 | algorithm: 'Nelder-Mead simplex direct search' |
---|
242 | message: [1x194 char] |
---|
243 | |
---|
244 | </pre><p class="footer"><br> |
---|
245 | Published with MATLAB® 7.0.1<br></p> |
---|
246 | <!-- |
---|
247 | ##### SOURCE BEGIN ##### |
---|
248 | %% Optimization of a simple (Rosenbrock) function, with no constraints |
---|
249 | rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2; |
---|
250 | |
---|
251 | % With no constraints, operation simply passes through |
---|
252 | % directly to fminsearch. The solution should be [1 1] |
---|
253 | xsol = fminsearchbnd(rosen,[3 3]) |
---|
254 | |
---|
255 | %% Only lower bound constraints |
---|
256 | xsol = fminsearchbnd(rosen,[3 3],[2 2]) |
---|
257 | |
---|
258 | %% Only upper bound constraints |
---|
259 | xsol = fminsearchbnd(rosen,[-5 -5],[],[0 0]) |
---|
260 | |
---|
261 | %% Dual constraints |
---|
262 | xsol = fminsearchbnd(rosen,[2.5 2.5],[2 2],[3 3]) |
---|
263 | |
---|
264 | %% Mixed constraints |
---|
265 | xsol = fminsearchbnd(rosen,[0 0],[2 -inf],[inf 3]) |
---|
266 | |
---|
267 | %% Provide your own fminsearch options |
---|
268 | opts = optimset('fminsearch'); |
---|
269 | opts.Display = 'iter'; |
---|
270 | opts.TolX = 1.e-12; |
---|
271 | opts.MaxFunEvals = 100; |
---|
272 | |
---|
273 | n = [10,5]; |
---|
274 | H = randn(n); |
---|
275 | H=H'*H; |
---|
276 | Quadraticfun = @(x) x*H*x'; |
---|
277 | |
---|
278 | % Global minimizer is at [0 0 0 0 0]. |
---|
279 | % Set all lower bound constraints, all of which will |
---|
280 | % be active in this test. |
---|
281 | LB = [.5 .5 .5 .5 .5]; |
---|
282 | xsol = fminsearchbnd(Quadraticfun,[1 2 3 4 5],LB,[],opts) |
---|
283 | |
---|
284 | %% Exactly fix one variable, constrain some others, and set a tolerance |
---|
285 | opts = optimset('fminsearch'); |
---|
286 | opts.TolFun = 1.e-12; |
---|
287 | |
---|
288 | LB = [-inf 2 1 -10]; |
---|
289 | UB = [ inf inf 1 inf]; |
---|
290 | xsol = fminsearchbnd(@(x) norm(x),[1 3 1 1],LB,UB,opts) |
---|
291 | |
---|
292 | %% All the standard outputs from fminsearch are still returned |
---|
293 | [xsol,fval,exitflag,output] = fminsearchbnd(@(x) norm(x),[1 3 1 1],LB,UB) |
---|
294 | |
---|
295 | |
---|
296 | ##### SOURCE END ##### |
---|
297 | --> |
---|
298 | </body> |
---|
299 | </html> |
---|