1 ## Copyright (C) 2008-2012 VZLU Prague, a.s.
3 ## This file is part of Octave.
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.
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.
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/>.
19 ## Author: Jaroslav Hajek <highegg@gmail.com>
22 ## @deftypefn {Function File} {[@var{x}, @var{fval}, @var{info}, @var{output}] =} fminbnd (@var{fun}, @var{a}, @var{b}, @var{options})
23 ## Find a minimum point of a univariate function. @var{fun} should be a
25 ## handle or name. @var{a}, @var{b} specify a starting interval. @var{options}
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}.
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:
38 ## The algorithm converged to a solution.
41 ## Maximum number of iterations or function evaluations has been exhausted.
44 ## The algorithm has been terminated from user output function.
46 ## @seealso{optimset, fzero, fminunc}
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)
52 ## PKG_ADD: ## Discard result to avoid polluting workspace with ans at startup.
53 ## PKG_ADD: [~] = __all_opts__ ("fminbnd");
55 function [x, fval, info, output] = fminbnd (fun, xmin, xmax, options = struct ())
57 ## Get default options if requested.
58 if (nargin == 1 && ischar (fun) && strcmp (fun, 'defaults'))
59 x = optimset ("MaxIter", Inf, "MaxFunEvals", Inf, "TolX", 1e-8, \
60 "OutputFcn", [], "FunValCheck", "off");
64 if (nargin < 2 || nargin > 4)
69 fun = str2func (fun, "global");
73 ## displev = optimget (options, "Display", "notify");
74 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on");
75 outfcn = optimget (options, "OutputFcn");
76 tolx = optimget (options, "TolX", 1e-8);
77 maxiter = optimget (options, "MaxIter", Inf);
78 maxfev = optimget (options, "MaxFunEvals", Inf);
81 ## Replace fun with a guarded version.
82 fun = @(x) guarded_eval (fun, x);
85 ## The default exit flag if exceeded number of iterations.
89 sqrteps = eps (class (xmin + xmax));
96 fv = fw = fval = fun (x);
99 while (niter < maxiter && nfev < maxfev)
101 ## FIXME: the golden section search can actually get closer than sqrt(eps)...
102 ## sometimes. Sometimes not, it depends on the function. This is the strategy
103 ## from the Netlib code. Something yet smarter would be good.
104 tol = 2 * sqrteps * abs (x) + tolx / 3;
105 if (abs (x - xm) <= (2*tol - 0.5*(b-a)))
112 ## Try inverse parabolic step.
113 r = (x - w)*(fval - fv);
114 q = (x - v)*(fval - fw);
115 p = (x - v)*q - (x - w)*r;
122 if (abs (p) < abs (0.5*q*r) && p > q*(a-x) && p < q*(b-x))
123 ## The parabolic step is acceptable.
127 ## f must not be evaluated too close to ax or bx.
128 if (min (u-a, b-u) < 2*tol)
129 d = tol * (sign (xm - x) + (xm == x));
138 ## Default to golden section step.
139 e = ifelse (x >= xm, a - x, b - x);
143 ## f must not be evaluated too close to x.
144 u = x + max (abs (d), tol) * (sign (d) + (d == 0));
150 ## update a, b, v, w, and x
162 ## The following if-statement was originally executed even if fu == fval.
168 if (fu <= fw || w == x)
171 elseif (fu <= fv || v == x || v == w)
177 ## If there's an output function, use it now.
179 optv.funccount = nfev;
181 optv.iteration = niter;
182 if (outfcn (x, optv, "iter"))
189 output.iterations = niter;
190 output.funcCount = nfev;
191 output.bracket = [a, b];
192 ## FIXME: bracketf possibly unavailable.
196 ## An assistant function that evaluates a function handle and checks for
198 function fx = guarded_eval (fun, x)
202 error ("fminbnd:notreal", "fminbnd: non-real value encountered");
204 error ("fminbnd:isnan", "fminbnd: NaN value encountered");
209 %! opt0 = optimset ("tolx", 0);
210 %!assert (fminbnd (@cos, pi/2, 3*pi/2, opt0), pi, 10*sqrt(eps))
211 %!assert (fminbnd (@(x) (x - 1e-3)^4, -1, 1, opt0), 1e-3, 10e-3*sqrt(eps))
212 %!assert (fminbnd (@(x) abs(x-1e7), 0, 1e10, opt0), 1e7, 10e7*sqrt(eps))
213 %!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))