Mercurial > hg > octave-nkf
annotate scripts/optimization/fminbnd.m @ 11270:5dd8e80fb6c1
update contributors.in
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 18 Nov 2010 14:39:02 -0500 |
parents | 693e22af08ae |
children | fd0a3ac60b0e |
rev | line source |
---|---|
10296 | 1 ## Copyright (C) 2008, 2009, 2010 VZLU Prague, a.s. |
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}) | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
23 ## Find a minimum point of a univariate function. @var{fun} should be a |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
24 ## function |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
25 ## handle or name. @var{a}, @var{b} specify a starting interval. @var{options} |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
26 ## is a |
10296 | 27 ## structure specifying additional options. Currently, @code{fminbnd} |
28 ## recognizes these options: @code{"FunValCheck"}, @code{"OutputFcn"}, | |
29 ## @code{"TolX"}, @code{"MaxIter"}, @code{"MaxFunEvals"}. | |
30 ## For description of these options, see @ref{doc-optimset,,optimset}. | |
31 ## | |
32 ## On exit, the function returns @var{x}, the approximate minimum point | |
33 ## and @var{fval}, the function value thereof. | |
34 ## @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
|
35 ## |
10296 | 36 ## @itemize |
37 ## @item 1 | |
38 ## The algorithm converged to a solution. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
39 ## |
10296 | 40 ## @item 0 |
41 ## 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
|
42 ## |
10296 | 43 ## @item -1 |
44 ## The algorithm has been terminated from user output function. | |
45 ## @end itemize | |
46 ## @seealso{optimset, fzero, fminunc} | |
47 ## @end deftypefn | |
48 | |
49 ## This is patterned after opt/fmin.f from Netlib, which in turn is taken from | |
50 ## Richard Brent: Algorithms For Minimization Without Derivatives, Prentice-Hall (1973) | |
51 | |
52 ## PKG_ADD: __all_opts__ ("fminbnd"); | |
53 | |
54 function [x, fval, info, output] = fminbnd (fun, xmin, xmax, options = struct ()) | |
55 | |
56 ## Get default options if requested. | |
57 if (nargin == 1 && ischar (fun) && strcmp (fun, 'defaults')) | |
58 x = optimset ("MaxIter", Inf, "MaxFunEvals", Inf, "TolX", 1e-8, \ | |
59 "OutputFcn", [], "FunValCheck", "off"); | |
60 return; | |
61 endif | |
62 | |
63 if (nargin < 2 || nargin > 4) | |
64 print_usage (); | |
65 endif | |
66 | |
67 if (ischar (fun)) | |
68 fun = str2func (fun, "global"); | |
69 endif | |
70 | |
71 ## TODO | |
72 ## displev = optimget (options, "Display", "notify"); | |
73 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on"); | |
74 outfcn = optimget (options, "OutputFcn"); | |
75 tolx = optimget (options, "TolX", 1e-8); | |
76 maxiter = optimget (options, "MaxIter", Inf); | |
77 maxfev = optimget (options, "MaxFunEvals", Inf); | |
78 | |
79 if (funvalchk) | |
80 ## Replace fun with a guarded version. | |
81 fun = @(x) guarded_eval (fun, x); | |
82 endif | |
83 | |
84 ## The default exit flag if exceeded number of iterations. | |
85 info = 0; | |
86 niter = 0; | |
87 nfev = 0; | |
10392
b4e5dcf023c9
fix fminbnd termination tolerances
Jaroslav Hajek <highegg@gmail.com>
parents:
10297
diff
changeset
|
88 sqrteps = eps (class (xmin + xmax)); |
10296 | 89 |
90 c = 0.5*(3-sqrt(5)); | |
91 a = xmin; b = xmax; | |
92 v = a + c*(b-a); | |
93 w = x = v; | |
94 e = 0; | |
95 fv = fw = fval = fun (x); | |
96 nfev++; | |
97 | |
98 while (niter < maxiter && nfev < maxfev) | |
99 xm = 0.5*(a+b); | |
10392
b4e5dcf023c9
fix fminbnd termination tolerances
Jaroslav Hajek <highegg@gmail.com>
parents:
10297
diff
changeset
|
100 ## FIXME: the golden section search can actually get closer than sqrt(eps)... |
b4e5dcf023c9
fix fminbnd termination tolerances
Jaroslav Hajek <highegg@gmail.com>
parents:
10297
diff
changeset
|
101 ## sometimes. Sometimes not, it depends on the function. This is the strategy |
b4e5dcf023c9
fix fminbnd termination tolerances
Jaroslav Hajek <highegg@gmail.com>
parents:
10297
diff
changeset
|
102 ## from the Netlib code. Something yet smarter would be good. |
b4e5dcf023c9
fix fminbnd termination tolerances
Jaroslav Hajek <highegg@gmail.com>
parents:
10297
diff
changeset
|
103 tol = 2 * sqrteps * abs (x) + tolx / 3; |
10296 | 104 if (abs (x - xm) <= (2*tol - 0.5*(b-a))) |
105 info = 1; | |
106 break; | |
107 endif | |
108 | |
109 if (abs (e) > tol) | |
110 dogs = false; | |
111 ## Try inverse parabolic step. | |
112 r = (x - w)*(fval - fv); | |
113 q = (x - v)*(fval - fw); | |
114 p = (x - v)*q - (x - w)*r; | |
115 q = 2*(q - r); | |
116 p *= -sign (q); | |
117 q = abs (q); | |
118 r = e; | |
119 e = d; | |
120 | |
121 if (abs (p) < abs (0.5*q*r) && p > q*(a-x) && p < q*(b-x)) | |
122 ## The parabolic step is acceptable. | |
123 d = p / q; | |
124 u = x + d; | |
125 | |
126 ## 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
|
127 if (min (u-a, b-u) < 2*tol) |
10296 | 128 d = tol * (sign (xm - x) + (xm == x)); |
129 endif | |
130 else | |
131 dogs = true; | |
132 endif | |
133 else | |
134 dogs = true; | |
135 endif | |
136 if (dogs) | |
137 ## Default to golden section step. | |
138 e = ifelse (x >= xm, a - x, b - x); | |
139 d = c * e; | |
140 endif | |
141 | |
142 ## f must not be evaluated too close to x. | |
143 u = x + max (abs (d), tol) * (sign (d) + (d == 0)); | |
144 | |
145 fu = fun (u); | |
146 nfev++; | |
147 niter++; | |
148 | |
149 ## update a, b, v, w, and x | |
150 | |
151 if (fu <= fval) | |
152 if (u < x) | |
153 b = x; | |
154 else | |
155 a = x; | |
156 endif | |
157 v = w; fv = fw; | |
158 w = x; fw = fval; | |
159 x = u; fval = fu; | |
160 else | |
161 ## The following if-statement was originally executed even if fu == fval. | |
162 if (u < x) | |
163 a = u; | |
164 else | |
165 b = u; | |
166 endif | |
167 if (fu <= fw || w == x) | |
168 v = w; fv = fw; | |
169 w = u; fw = fu; | |
170 elseif (fu <= fv || v == x || v == w) | |
171 v = u; | |
172 fv = fu; | |
173 endif | |
174 endif | |
175 | |
176 ## If there's an output function, use it now. | |
177 if (outfcn) | |
178 optv.funccount = nfev; | |
179 optv.fval = fval; | |
180 optv.iteration = niter; | |
181 if (outfcn (x, optv, "iter")) | |
182 info = -1; | |
183 break; | |
184 endif | |
185 endif | |
186 endwhile | |
187 | |
188 output.iterations = niter; | |
189 output.funcCount = nfev; | |
190 output.bracket = [a, b]; | |
191 ## FIXME: bracketf possibly unavailable. | |
192 | |
193 endfunction | |
194 | |
195 ## An assistant function that evaluates a function handle and checks for | |
196 ## bad results. | |
197 function fx = guarded_eval (fun, x) | |
198 fx = fun (x); | |
199 fx = fx(1); | |
200 if (! isreal (fx)) | |
201 error ("fminbnd:notreal", "fminbnd: non-real value encountered"); | |
202 elseif (isnan (fx)) | |
203 error ("fminbnd:isnan", "fminbnd: NaN value encountered"); | |
204 endif | |
205 endfunction | |
206 | |
207 %!shared opt0 | |
208 %! opt0 = optimset ("tolx", 0); | |
10393 | 209 %!assert (fminbnd (@cos, pi/2, 3*pi/2, opt0), pi, 10*sqrt(eps)) |
210 %!assert (fminbnd (@(x) (x - 1e-3)^4, -1, 1, opt0), 1e-3, 10e-3*sqrt(eps)) | |
211 %!assert (fminbnd (@(x) abs(x-1e7), 0, 1e10, opt0), 1e7, 10e7*sqrt(eps)) | |
212 %!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)) |