Mercurial > hg > octave-nkf
annotate scripts/optimization/fminbnd.m @ 14895:e0525ecf156e
Add new function fminsearch.m
* fminsearch.m: new function.
* optimization/module.mk: Add fminsearch to build system.
* NEWS: Add fminsearch to list of new functions in 3.8.0.
* nonlin.txi, fminbnd.m, fminunc.m: Add fminsearch to documentation.
Update other optimization functions to reference fminsearch.
author | Andy Adler <andy@analyti.ca> |
---|---|
date | Fri, 20 Jul 2012 09:25:37 -0700 |
parents | 5d3a684236b0 |
children | 242e9efd4315 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13027
diff
changeset
|
1 ## Copyright (C) 2008-2012 VZLU Prague, a.s. |
10296 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 ## | |
19 ## Author: Jaroslav Hajek <highegg@gmail.com> | |
20 | |
21 ## -*- texinfo -*- | |
22 ## @deftypefn {Function File} {[@var{x}, @var{fval}, @var{info}, @var{output}] =} fminbnd (@var{fun}, @var{a}, @var{b}, @var{options}) | |
14895
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
23 ## Find a minimum point of a univariate function. |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
24 ## |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
25 ## @var{fun} should be a function handle or name. @var{a}, @var{b} specify a |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
26 ## starting interval. @var{options} is a structure specifying additional |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
27 ## options. Currently, @code{fminbnd} recognizes these options: |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
28 ## "FunValCheck", "OutputFcn", "TolX", "MaxIter", "MaxFunEvals". For a |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
29 ## description of these options, see @ref{doc-optimset,,optimset}. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
30 ## |
10296 | 31 ## On exit, the function returns @var{x}, the approximate minimum point |
32 ## and @var{fval}, the function value thereof. | |
33 ## @var{info} is an exit flag that can have these values: | |
10297
ed88ea036716
improve docs of fzero/fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
10296
diff
changeset
|
34 ## |
10296 | 35 ## @itemize |
36 ## @item 1 | |
37 ## The algorithm converged to a solution. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
38 ## |
10296 | 39 ## @item 0 |
40 ## Maximum number of iterations or function evaluations has been exhausted. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
41 ## |
10296 | 42 ## @item -1 |
43 ## The algorithm has been terminated from user output function. | |
44 ## @end itemize | |
14895
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
45 ## |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
46 ## Notes: The search for a minimum is restricted to be in the interval |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
47 ## bound by @var{a} and @var{b}. If you only have an initial point |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
48 ## to begin searching from you will need to use an unconstrained |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
49 ## minimization algorithm such as @code{fminunc} or @code{fminsearch}. |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
50 ## @code{fminbnd} internally uses a Golden Section search strategy. |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
51 ## @seealso{fzero, fminunc, fminsearch, optimset} |
10296 | 52 ## @end deftypefn |
53 | |
54 ## This is patterned after opt/fmin.f from Netlib, which in turn is taken from | |
55 ## Richard Brent: Algorithms For Minimization Without Derivatives, Prentice-Hall (1973) | |
56 | |
13027
b9a89ca0fb75
prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
57 ## PKG_ADD: ## Discard result to avoid polluting workspace with ans at startup. |
b9a89ca0fb75
prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
58 ## PKG_ADD: [~] = __all_opts__ ("fminbnd"); |
10296 | 59 |
60 function [x, fval, info, output] = fminbnd (fun, xmin, xmax, options = struct ()) | |
61 | |
62 ## Get default options if requested. | |
63 if (nargin == 1 && ischar (fun) && strcmp (fun, 'defaults')) | |
14552
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
64 x = optimset ("MaxIter", Inf, "MaxFunEvals", Inf, "TolX", 1e-8, |
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
65 "OutputFcn", [], "FunValCheck", "off"); |
10296 | 66 return; |
67 endif | |
68 | |
69 if (nargin < 2 || nargin > 4) | |
70 print_usage (); | |
71 endif | |
72 | |
73 if (ischar (fun)) | |
74 fun = str2func (fun, "global"); | |
75 endif | |
76 | |
77 ## TODO | |
78 ## displev = optimget (options, "Display", "notify"); | |
79 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on"); | |
80 outfcn = optimget (options, "OutputFcn"); | |
81 tolx = optimget (options, "TolX", 1e-8); | |
82 maxiter = optimget (options, "MaxIter", Inf); | |
83 maxfev = optimget (options, "MaxFunEvals", Inf); | |
84 | |
85 if (funvalchk) | |
86 ## Replace fun with a guarded version. | |
87 fun = @(x) guarded_eval (fun, x); | |
88 endif | |
89 | |
90 ## The default exit flag if exceeded number of iterations. | |
91 info = 0; | |
92 niter = 0; | |
93 nfev = 0; | |
10392
b4e5dcf023c9
fix fminbnd termination tolerances
Jaroslav Hajek <highegg@gmail.com>
parents:
10297
diff
changeset
|
94 sqrteps = eps (class (xmin + xmax)); |
10296 | 95 |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
96 c = 0.5*(3 - sqrt (5)); |
10296 | 97 a = xmin; b = xmax; |
98 v = a + c*(b-a); | |
99 w = x = v; | |
100 e = 0; | |
101 fv = fw = fval = fun (x); | |
102 nfev++; | |
103 | |
104 while (niter < maxiter && nfev < maxfev) | |
105 xm = 0.5*(a+b); | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
106 ## FIXME: the golden section search can actually get closer than sqrt(eps) |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
107 ## sometimes. Sometimes not, it depends on the function. This is the |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
108 ## strategy from the Netlib code. Something yet smarter would be good. |
10392
b4e5dcf023c9
fix fminbnd termination tolerances
Jaroslav Hajek <highegg@gmail.com>
parents:
10297
diff
changeset
|
109 tol = 2 * sqrteps * abs (x) + tolx / 3; |
10296 | 110 if (abs (x - xm) <= (2*tol - 0.5*(b-a))) |
111 info = 1; | |
112 break; | |
113 endif | |
114 | |
115 if (abs (e) > tol) | |
116 dogs = false; | |
117 ## Try inverse parabolic step. | |
118 r = (x - w)*(fval - fv); | |
119 q = (x - v)*(fval - fw); | |
120 p = (x - v)*q - (x - w)*r; | |
121 q = 2*(q - r); | |
122 p *= -sign (q); | |
123 q = abs (q); | |
124 r = e; | |
125 e = d; | |
126 | |
127 if (abs (p) < abs (0.5*q*r) && p > q*(a-x) && p < q*(b-x)) | |
128 ## The parabolic step is acceptable. | |
129 d = p / q; | |
130 u = x + d; | |
131 | |
132 ## f must not be evaluated too close to ax or bx. | |
10392
b4e5dcf023c9
fix fminbnd termination tolerances
Jaroslav Hajek <highegg@gmail.com>
parents:
10297
diff
changeset
|
133 if (min (u-a, b-u) < 2*tol) |
10296 | 134 d = tol * (sign (xm - x) + (xm == x)); |
135 endif | |
136 else | |
137 dogs = true; | |
138 endif | |
139 else | |
140 dogs = true; | |
141 endif | |
142 if (dogs) | |
143 ## Default to golden section step. | |
144 e = ifelse (x >= xm, a - x, b - x); | |
145 d = c * e; | |
146 endif | |
147 | |
148 ## f must not be evaluated too close to x. | |
149 u = x + max (abs (d), tol) * (sign (d) + (d == 0)); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
150 |
10296 | 151 fu = fun (u); |
152 nfev++; | |
153 niter++; | |
154 | |
155 ## update a, b, v, w, and x | |
156 | |
157 if (fu <= fval) | |
158 if (u < x) | |
159 b = x; | |
160 else | |
161 a = x; | |
162 endif | |
163 v = w; fv = fw; | |
164 w = x; fw = fval; | |
165 x = u; fval = fu; | |
166 else | |
167 ## The following if-statement was originally executed even if fu == fval. | |
168 if (u < x) | |
169 a = u; | |
170 else | |
171 b = u; | |
172 endif | |
173 if (fu <= fw || w == x) | |
174 v = w; fv = fw; | |
175 w = u; fw = fu; | |
176 elseif (fu <= fv || v == x || v == w) | |
177 v = u; | |
178 fv = fu; | |
179 endif | |
180 endif | |
181 | |
182 ## If there's an output function, use it now. | |
183 if (outfcn) | |
184 optv.funccount = nfev; | |
185 optv.fval = fval; | |
186 optv.iteration = niter; | |
187 if (outfcn (x, optv, "iter")) | |
188 info = -1; | |
189 break; | |
190 endif | |
191 endif | |
192 endwhile | |
193 | |
194 output.iterations = niter; | |
195 output.funcCount = nfev; | |
196 output.bracket = [a, b]; | |
197 ## FIXME: bracketf possibly unavailable. | |
198 | |
199 endfunction | |
200 | |
201 ## An assistant function that evaluates a function handle and checks for | |
202 ## bad results. | |
203 function fx = guarded_eval (fun, x) | |
204 fx = fun (x); | |
205 fx = fx(1); | |
206 if (! isreal (fx)) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
207 error ("fminbnd:notreal", "fminbnd: non-real value encountered"); |
10296 | 208 elseif (isnan (fx)) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
209 error ("fminbnd:isnan", "fminbnd: NaN value encountered"); |
10296 | 210 endif |
211 endfunction | |
212 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
213 |
10296 | 214 %!shared opt0 |
215 %! opt0 = optimset ("tolx", 0); | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
216 %!assert (fminbnd (@cos, pi/2, 3*pi/2, opt0), pi, 10*sqrt (eps)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
217 %!assert (fminbnd (@(x) (x - 1e-3)^4, -1, 1, opt0), 1e-3, 10e-3*sqrt (eps)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
218 %!assert (fminbnd (@(x) abs (x-1e7), 0, 1e10, opt0), 1e7, 10e7*sqrt (eps)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
219 %!assert (fminbnd (@(x) x^2 + sin (2*pi*x), 0.4, 1, opt0), fzero (@(x) 2*x + 2*pi*cos (2*pi*x), [0.4, 1], opt0), sqrt (eps)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
220 |