Mercurial > octave
annotate scripts/optimization/fminsearch.m @ 23084:ef4d915df748
maint: Merge stable to default.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 23 Jan 2017 14:27:48 -0500 |
parents | 3c896180df16 e9a0469dedd9 |
children | dffa2b8c9482 |
rev | line source |
---|---|
22323
bac0d6f07a3e
maint: Update copyright notices for 2016.
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
1 ## Copyright (C) 2003-2016 Andy Adler |
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17281
diff
changeset
|
2 ## Copyright (C) 2002, 2013 N.J.Higham |
14895 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
8 ## the Free Software Foundation; either version 3 of the License, or |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
9 ## (at your option) any later version. |
14895 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
14 ## GNU General Public License for more details. |
14895 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
17 ## along with Octave; see the file COPYING. If not, see | |
18 ## <http://www.gnu.org/licenses/>. | |
19 | |
20 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
21 ## @deftypefn {} {@var{x} =} fminsearch (@var{fun}, @var{x0}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
22 ## @deftypefnx {} {@var{x} =} fminsearch (@var{fun}, @var{x0}, @var{options}) |
22967
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
23 ## @deftypefnx {} {@var{x} =} fminsearch (@var{fun}, @var{x0}, @var{options}, @var{fun_arg1}, @var{fun_arg2}, @dots{}) |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
24 ## @deftypefnx {} {[@var{x}, @var{fval}] =} fminsearch (@dots{}) |
14895 | 25 ## |
26 ## Find a value of @var{x} which minimizes the function @var{fun}. | |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19596
diff
changeset
|
27 ## |
14895 | 28 ## The search begins at the point @var{x0} and iterates using the |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
29 ## @nospell{Nelder & Mead} Simplex algorithm (a derivative-free method). This |
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
30 ## algorithm is better-suited to functions which have discontinuities or for |
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
31 ## which a gradient-based search such as @code{fminunc} fails. |
14895 | 32 ## |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19596
diff
changeset
|
33 ## Options for the search are provided in the parameter @var{options} using the |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19596
diff
changeset
|
34 ## function @code{optimset}. Currently, @code{fminsearch} accepts the options: |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19596
diff
changeset
|
35 ## @qcode{"TolX"}, @qcode{"MaxFunEvals"}, @qcode{"MaxIter"}, @qcode{"Display"}. |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19596
diff
changeset
|
36 ## For a description of these options, see @code{optimset}. |
14895 | 37 ## |
22967
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
38 ## Additional inputs for the function @var{fun} can be passed as the fourth |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
39 ## and higher arguments. To pass function arguments while using the default |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
40 ## @var{options} values, use @code{[]} for @var{options}. |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
41 ## |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19596
diff
changeset
|
42 ## On exit, the function returns @var{x}, the minimum point, and @var{fval}, |
22967
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
43 ## the function value at the minimum. |
14895 | 44 ## |
45 ## Example usages: | |
46 ## | |
47 ## @example | |
48 ## @group | |
49 ## fminsearch (@@(x) (x(1)-5).^2+(x(2)-8).^4, [0;0]) | |
50 ## | |
51 ## fminsearch (inline ("(x(1)-5).^2+(x(2)-8).^4", "x"), [0;0]) | |
52 ## @end group | |
53 ## @end example | |
54 ## @seealso{fminbnd, fminunc, optimset} | |
55 ## @end deftypefn | |
56 | |
57 ## PKG_ADD: ## Discard result to avoid polluting workspace with ans at startup. | |
58 ## PKG_ADD: [~] = __all_opts__ ("fminsearch"); | |
59 | |
21099
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
60 ## FIXME: Add support for "exitflag" output variable. |
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
61 ## FIXME: Add support for "output" output variable. |
14895 | 62 ## FIXME: For Display option, add 'final' and 'notify' options. Not too hard. |
21099
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
63 ## FIXME: Add support for OutputFcn. See fminunc for a template. |
14895 | 64 ## FIXME: Add support for exiting based on TolFun. See fminunc for an idea. |
65 | |
22967
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
66 function [x, fval] = fminsearch (fun, x0, options, varargin) |
14895 | 67 |
68 ## Get default options if requested. | |
69 if (nargin == 1 && ischar (fun) && strcmp (fun, "defaults")) | |
70 x = optimset ("Display", "notify", "FunValCheck", "off", | |
71 "MaxFunEvals", 400, "MaxIter", 400, | |
72 "OutputFcn", [], | |
73 "TolFun", 1e-7, "TolX", 1e-4); | |
74 return; | |
75 endif | |
76 | |
22967
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
77 if (nargin < 2) |
14895 | 78 print_usage (); |
79 endif | |
22967
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
80 |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
81 if (nargin < 3 || isempty (options)) |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
82 options = struct (); |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
83 endif |
14895 | 84 |
22967
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
85 x = nmsmax (fun, x0, options, varargin{:}); |
14895 | 86 |
87 if (isargout (2)) | |
88 fval = feval (fun, x); | |
89 endif | |
90 | |
91 endfunction | |
92 | |
93 ##NMSMAX Nelder-Mead simplex method for direct search optimization. | |
94 ## [x, fmax, nf] = NMSMAX(FUN, x0, STOPIT, SAVIT) attempts to | |
95 ## maximize the function FUN, using the starting vector x0. | |
96 ## The Nelder-Mead direct search method is used. | |
97 ## Output arguments: | |
98 ## x = vector yielding largest function value found, | |
99 ## fmax = function value at x, | |
100 ## nf = number of function evaluations. | |
101 ## The iteration is terminated when either | |
102 ## - the relative size of the simplex is <= STOPIT(1) | |
103 ## (default 1e-3), | |
104 ## - STOPIT(2) function evaluations have been performed | |
105 ## (default inf, i.e., no limit), or | |
106 ## - a function value equals or exceeds STOPIT(3) | |
107 ## (default inf, i.e., no test on function values). | |
108 ## The form of the initial simplex is determined by STOPIT(4): | |
109 ## STOPIT(4) = 0: regular simplex (sides of equal length, the default) | |
110 ## STOPIT(4) = 1: right-angled simplex. | |
111 ## Progress of the iteration is not shown if STOPIT(5) = 0 (default 1). | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21099
diff
changeset
|
112 ## STOPIT(6) indicates the direction (i.e., minimization or |
14895 | 113 ## maximization.) Default is 1, maximization. |
114 ## set STOPIT(6)=-1 for minimization | |
115 ## If a non-empty fourth parameter string SAVIT is present, then | |
15468
6437fa7263dd
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14895
diff
changeset
|
116 ## 'SAVE SAVIT x fmax nf' is executed after each inner iteration. |
14895 | 117 ## NB: x0 can be a matrix. In the output argument, in SAVIT saves, |
118 ## and in function calls, x has the same shape as x0. | |
119 ## NMSMAX(fun, x0, STOPIT, SAVIT, P1, P2,...) allows additional | |
120 ## arguments to be passed to fun, via feval(fun,x,P1,P2,...). | |
121 ## References: | |
122 ## N. J. Higham, Optimization by direct search in matrix computations, | |
123 ## SIAM J. Matrix Anal. Appl, 14(2): 317-333, 1993. | |
124 ## C. T. Kelley, Iterative Methods for Optimization, Society for Industrial | |
125 ## and Applied Mathematics, Philadelphia, PA, 1999. | |
126 | |
127 ## From Matrix Toolbox | |
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17281
diff
changeset
|
128 ## Copyright (C) 2002, 2013 N.J.Higham |
14895 | 129 ## www.maths.man.ac.uk/~higham/mctoolbox |
130 ## | |
131 ## Modifications for Octave by A.Adler 2003 | |
132 | |
22967
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
133 function [stopit, savit, dirn, trace, tol, maxiter] = parse_options (options, x) |
14895 | 134 |
135 ## Tolerance for cgce test based on relative size of simplex. | |
136 stopit(1) = tol = optimget (options, "TolX", 1e-4); | |
137 | |
138 ## Max no. of f-evaluations. | |
139 stopit(2) = optimget (options, "MaxFunEvals", length (x) * 200); | |
140 | |
141 ## Max no. of iterations | |
142 maxiter = optimget (options, "MaxIter", length (x) * 200); | |
143 | |
144 ## Default target for f-values. | |
145 stopit(3) = Inf; # FIXME: expose this parameter to the outside | |
146 | |
147 ## Default initial simplex. | |
148 stopit(4) = 0; # FIXME: expose this parameter to the outside | |
149 | |
150 ## Default: show progress. | |
151 display = optimget (options, "Display", "notify"); | |
152 if (strcmp (display, "iter")) | |
153 stopit(5) = 1; | |
154 else | |
155 stopit(5) = 0; | |
156 endif | |
157 trace = stopit(5); | |
158 | |
159 ## Use function to minimize, not maximize | |
160 stopit(6) = dirn = -1; | |
161 | |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
20231
diff
changeset
|
162 ## Filename for snapshots. |
14895 | 163 savit = []; # FIXME: expose this parameter to the outside |
164 | |
165 endfunction | |
166 | |
22967
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
167 function [x, fmax, nf] = nmsmax (fun, x, options, varargin) |
14895 | 168 |
169 [stopit, savit, dirn, trace, tol, maxiter] = parse_options (options, x); | |
170 | |
171 if (strcmpi (optimget (options, "FunValCheck", "off"), "on")) | |
172 ## Replace fcn with a guarded version. | |
173 fun = @(x) guarded_eval (fun, x); | |
174 endif | |
175 | |
176 x0 = x(:); # Work with column vector internally. | |
177 n = length (x0); | |
178 | |
179 V = [zeros(n,1) eye(n)]; | |
180 f = zeros (n+1,1); | |
181 V(:,1) = x0; | |
182 f(1) = dirn * feval (fun,x,varargin{:}); | |
183 fmax_old = f(1); | |
184 | |
185 if (trace) | |
186 fprintf ("f(x0) = %9.4e\n", f(1)); | |
187 endif | |
188 | |
189 k = 0; m = 0; | |
190 | |
191 ## Set up initial simplex. | |
192 scale = max (norm (x0,Inf), 1); | |
193 if (stopit(4) == 0) | |
194 ## Regular simplex - all edges have same length. | |
195 ## Generated from construction given in reference [18, pp. 80-81] of [1]. | |
196 alpha = scale / (n*sqrt (2)) * [sqrt(n+1)-1+n, sqrt(n+1)-1]; | |
197 V(:,2:n+1) = (x0 + alpha(2)*ones (n,1)) * ones (1,n); | |
198 for j = 2:n+1 | |
199 V(j-1,j) = x0(j-1) + alpha(1); | |
200 x(:) = V(:,j); | |
201 f(j) = dirn * feval (fun,x,varargin{:}); | |
202 endfor | |
203 else | |
204 ## Right-angled simplex based on co-ordinate axes. | |
205 alpha = scale * ones(n+1,1); | |
206 for j=2:n+1 | |
207 V(:,j) = x0 + alpha(j)*V(:,j); | |
208 x(:) = V(:,j); | |
209 f(j) = dirn * feval (fun,x,varargin{:}); | |
210 endfor | |
211 endif | |
212 nf = n+1; | |
213 how = "initial "; | |
214 | |
215 [~,j] = sort (f); | |
216 j = j(n+1:-1:1); | |
217 f = f(j); | |
218 V = V(:,j); | |
219 | |
220 alpha = 1; beta = 1/2; gamma = 2; | |
221 | |
222 while (1) # Outer (and only) loop. | |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
223 k += 1; |
14895 | 224 |
225 if (k > maxiter) | |
226 msg = "Exceeded maximum iterations...quitting\n"; | |
227 break; | |
228 endif | |
229 | |
230 fmax = f(1); | |
231 if (fmax > fmax_old) | |
232 if (! isempty (savit)) | |
233 x(:) = V(:,1); | |
234 eval (["save " savit " x fmax nf"]); | |
235 endif | |
236 endif | |
237 if (trace) | |
238 fprintf ("Iter. %2.0f,", k); | |
239 fprintf ([" how = " how " "]); | |
240 fprintf ("nf = %3.0f, f = %9.4e (%2.1f%%)\n", nf, fmax, ... | |
241 100*(fmax-fmax_old)/(abs(fmax_old)+eps)); | |
242 endif | |
243 fmax_old = fmax; | |
244 | |
245 ## Three stopping tests from MDSMAX.M | |
246 | |
247 ## Stopping Test 1 - f reached target value? | |
248 if (fmax >= stopit(3)) | |
249 msg = "Exceeded target...quitting\n"; | |
250 break; | |
251 endif | |
252 | |
253 ## Stopping Test 2 - too many f-evals? | |
254 if (nf >= stopit(2)) | |
255 msg = "Max no. of function evaluations exceeded...quitting\n"; | |
256 break; | |
257 endif | |
258 | |
259 ## Stopping Test 3 - converged? This is test (4.3) in [1]. | |
260 v1 = V(:,1); | |
261 size_simplex = norm (V(:,2:n+1)-v1(:,ones (1,n)),1) / max (1, norm (v1,1)); | |
262 if (size_simplex <= tol) | |
263 msg = sprintf ("Simplex size %9.4e <= %9.4e...quitting\n", ... | |
264 size_simplex, tol); | |
265 break; | |
266 endif | |
267 | |
268 ## One step of the Nelder-Mead simplex algorithm | |
269 ## NJH: Altered function calls and changed CNT to NF. | |
15468
6437fa7263dd
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14895
diff
changeset
|
270 ## Changed each 'fr < f(1)' type test to '>' for maximization |
14895 | 271 ## and re-ordered function values after sort. |
272 | |
273 vbar = (sum (V(:,1:n)')/n)'; # Mean value | |
274 vr = (1 + alpha)*vbar - alpha*V(:,n+1); | |
275 x(:) = vr; | |
276 fr = dirn * feval (fun,x,varargin{:}); | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
277 nf += 1; |
14895 | 278 vk = vr; fk = fr; how = "reflect, "; |
279 if (fr > f(n)) | |
280 if (fr > f(1)) | |
281 ve = gamma*vr + (1-gamma)*vbar; | |
282 x(:) = ve; | |
283 fe = dirn * feval (fun,x,varargin{:}); | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
284 nf += 1; |
14895 | 285 if (fe > f(1)) |
286 vk = ve; | |
287 fk = fe; | |
288 how = "expand, "; | |
289 endif | |
290 endif | |
291 else | |
292 vt = V(:,n+1); | |
293 ft = f(n+1); | |
294 if (fr > ft) | |
295 vt = vr; | |
296 ft = fr; | |
297 endif | |
298 vc = beta*vt + (1-beta)*vbar; | |
299 x(:) = vc; | |
300 fc = dirn * feval (fun,x,varargin{:}); | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
301 nf += 1; |
14895 | 302 if (fc > f(n)) |
303 vk = vc; fk = fc; | |
304 how = "contract,"; | |
305 else | |
306 for j = 2:n | |
307 V(:,j) = (V(:,1) + V(:,j))/2; | |
308 x(:) = V(:,j); | |
309 f(j) = dirn * feval (fun,x,varargin{:}); | |
310 endfor | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
311 nf += n-1; |
14895 | 312 vk = (V(:,1) + V(:,n+1))/2; |
313 x(:) = vk; | |
314 fk = dirn * feval (fun,x,varargin{:}); | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
315 nf += 1; |
14895 | 316 how = "shrink, "; |
317 endif | |
318 endif | |
319 V(:,n+1) = vk; | |
320 f(n+1) = fk; | |
321 [~,j] = sort(f); | |
322 j = j(n+1:-1:1); | |
323 f = f(j); | |
324 V = V(:,j); | |
325 | |
326 endwhile # End of outer (and only) loop. | |
327 | |
328 ## Finished. | |
329 if (trace) | |
330 fprintf (msg); | |
331 endif | |
332 x(:) = V(:,1); | |
333 | |
334 endfunction | |
335 | |
336 ## A helper function that evaluates a function and checks for bad results. | |
337 function y = guarded_eval (fun, x) | |
338 | |
339 y = fun (x); | |
340 | |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
341 if (! (isreal (y))) |
14895 | 342 error ("fminsearch:notreal", "fminsearch: non-real value encountered"); |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
343 elseif (any (isnan (y(:)))) |
14895 | 344 error ("fminsearch:isnan", "fminsearch: NaN value encountered"); |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
345 elseif (any (isinf (y(:)))) |
14895 | 346 error ("fminsearch:isinf", "fminsearch: Inf value encountered"); |
347 endif | |
348 | |
349 endfunction | |
350 | |
351 | |
352 %!demo | |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
353 %! fcn = @(x) (x(1)-5).^2 + (x(2)-8).^4; |
14895 | 354 %! x0 = [0;0]; |
355 %! [xmin, fval] = fminsearch (fcn, x0) | |
356 | |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
357 %!assert (fminsearch (@sin, 3, optimset ("MaxIter", 30)), 3*pi/2, 1e-4) |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
358 %!test |
14895 | 359 %! c = 1.5; |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
360 %! assert (fminsearch (@(x) x(1).^2+c*x(2).^2,[1;1]), [0;0], 1e-4); |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
361 |
22967
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
362 ## Additional argument |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
363 %!test |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
364 %! x1 = fminsearch (@(x, c) x(1).^2 + c*x(2).^2, [1;1], [], 1.5); |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
365 %! assert (x1, [0;0], 1e-4); |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
366 %! x1 = fminsearch (@(x, c) x(1).^2 + c*x(2).^2, [1;1], ... |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
367 %! optimset ("Display", "none"), 1.5); |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
368 %! assert (x1, [0;0], 1e-4); |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
369 |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
370 ## Test input validation |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
371 %!error fminsearch () |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
372 %!error fminsearch (1) |
14895 | 373 |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
374 ## Tests for guarded_eval |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
375 %!error <non-real value encountered> |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
376 %! fminsearch (@(x) ([0 2i]), 0, optimset ("FunValCheck", "on")); |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
377 %!error <NaN value encountered> |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
378 %! fminsearch (@(x) (NaN), 0, optimset ("FunValCheck", "on")); |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
379 %!error <Inf value encountered> |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
380 %! fminsearch (@(x) (Inf), 0, optimset ("FunValCheck", "on")); |