Mercurial > octave
annotate scripts/optimization/fminbnd.m @ 21099:52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
* fminbnd.m, fminsearch.m, fminunc.m, fsolve.m: Use 'isa (..., "single")' to
check both initial guess X0 and result of function evaluation to decide whether
eps should be single or double.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 18 Jan 2016 10:00:56 -0800 |
parents | 516bb87ea72e |
children | 6c2fd62db1f7 |
rev | line source |
---|---|
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19597
diff
changeset
|
1 ## Copyright (C) 2008-2015 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 -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
22 ## @deftypefn {} {[@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. |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
24 ## |
14895
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: |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
28 ## @qcode{"FunValCheck"}, @qcode{"OutputFcn"}, @qcode{"TolX"}, |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
29 ## @qcode{"MaxIter"}, @qcode{"MaxFunEvals"}. For a description of these |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
30 ## options, see @ref{XREFoptimset,,optimset}. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
31 ## |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
32 ## On exit, the function returns @var{x}, the approximate minimum point and |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
33 ## @var{fval}, the function value thereof. |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
34 ## |
10296 | 35 ## @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
|
36 ## |
10296 | 37 ## @itemize |
38 ## @item 1 | |
39 ## The algorithm converged to a solution. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
40 ## |
10296 | 41 ## @item 0 |
42 ## 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
|
43 ## |
10296 | 44 ## @item -1 |
45 ## The algorithm has been terminated from user output function. | |
46 ## @end itemize | |
14895
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
47 ## |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
48 ## Notes: The search for a minimum is restricted to be in the interval bound by |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
49 ## @var{a} and @var{b}. If you only have an initial point to begin searching |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
50 ## from you will need to use an unconstrained minimization algorithm such as |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
51 ## @code{fminunc} or @code{fminsearch}. @code{fminbnd} internally uses a |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
52 ## Golden Section search strategy. |
14895
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
53 ## @seealso{fzero, fminunc, fminsearch, optimset} |
10296 | 54 ## @end deftypefn |
55 | |
56 ## This is patterned after opt/fmin.f from Netlib, which in turn is taken from | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
57 ## Richard Brent: Algorithms For Minimization Without Derivatives, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
58 ## Prentice-Hall (1973) |
10296 | 59 |
13027
b9a89ca0fb75
prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
60 ## 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
|
61 ## PKG_ADD: [~] = __all_opts__ ("fminbnd"); |
10296 | 62 |
63 function [x, fval, info, output] = fminbnd (fun, xmin, xmax, options = struct ()) | |
64 | |
65 ## Get default options if requested. | |
66 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
|
67 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
|
68 "OutputFcn", [], "FunValCheck", "off"); |
10296 | 69 return; |
70 endif | |
71 | |
72 if (nargin < 2 || nargin > 4) | |
73 print_usage (); | |
74 endif | |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19443
diff
changeset
|
75 |
19443
0f79fa9b3a8c
fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18290
diff
changeset
|
76 if (xmin > xmax) |
0f79fa9b3a8c
fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18290
diff
changeset
|
77 error ("Octave:invalid-input-arg", |
0f79fa9b3a8c
fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18290
diff
changeset
|
78 "fminbnd: the lower bound cannot be greater than the upper one"); |
0f79fa9b3a8c
fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18290
diff
changeset
|
79 endif |
10296 | 80 |
81 if (ischar (fun)) | |
82 fun = str2func (fun, "global"); | |
83 endif | |
84 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
85 displ = optimget (options, "Display", "notify"); |
10296 | 86 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on"); |
87 outfcn = optimget (options, "OutputFcn"); | |
88 tolx = optimget (options, "TolX", 1e-8); | |
89 maxiter = optimget (options, "MaxIter", Inf); | |
90 maxfev = optimget (options, "MaxFunEvals", Inf); | |
91 | |
92 if (funvalchk) | |
93 ## Replace fun with a guarded version. | |
94 fun = @(x) guarded_eval (fun, x); | |
95 endif | |
96 | |
97 ## The default exit flag if exceeded number of iterations. | |
98 info = 0; | |
99 niter = 0; | |
100 nfev = 0; | |
101 | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
102 c = 0.5*(3 - sqrt (5)); |
10296 | 103 a = xmin; b = xmax; |
104 v = a + c*(b-a); | |
105 w = x = v; | |
106 e = 0; | |
107 fv = fw = fval = fun (x); | |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20727
diff
changeset
|
108 nfev += 1; |
10296 | 109 |
21099
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
110 if (isa (xmin, "single") || isa (xmax, "single") || isa (fval, "single")) |
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
111 sqrteps = eps ("single"); |
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
112 else |
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
113 sqrteps = eps ("double"); |
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
114 endif |
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
115 |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
116 ## Only for display purposes. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
117 iter(1).funccount = nfev; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
118 iter(1).x = x; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
119 iter(1).fx = fval; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
120 |
10296 | 121 while (niter < maxiter && nfev < maxfev) |
122 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
|
123 ## 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
|
124 ## sometimes. Sometimes not, it depends on the function. This is the |
21099
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
125 ## strategy from the Netlib code. Something smarter would be good. |
10392
b4e5dcf023c9
fix fminbnd termination tolerances
Jaroslav Hajek <highegg@gmail.com>
parents:
10297
diff
changeset
|
126 tol = 2 * sqrteps * abs (x) + tolx / 3; |
10296 | 127 if (abs (x - xm) <= (2*tol - 0.5*(b-a))) |
128 info = 1; | |
129 break; | |
130 endif | |
131 | |
132 if (abs (e) > tol) | |
133 dogs = false; | |
134 ## Try inverse parabolic step. | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
135 iter(niter+1).procedure = "parabolic"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
136 |
10296 | 137 r = (x - w)*(fval - fv); |
138 q = (x - v)*(fval - fw); | |
139 p = (x - v)*q - (x - w)*r; | |
140 q = 2*(q - r); | |
141 p *= -sign (q); | |
142 q = abs (q); | |
143 r = e; | |
144 e = d; | |
145 | |
146 if (abs (p) < abs (0.5*q*r) && p > q*(a-x) && p < q*(b-x)) | |
147 ## The parabolic step is acceptable. | |
148 d = p / q; | |
149 u = x + d; | |
150 | |
151 ## 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
|
152 if (min (u-a, b-u) < 2*tol) |
10296 | 153 d = tol * (sign (xm - x) + (xm == x)); |
154 endif | |
155 else | |
156 dogs = true; | |
157 endif | |
158 else | |
159 dogs = true; | |
160 endif | |
161 if (dogs) | |
162 ## Default to golden section step. | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
163 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
164 ## WARNING: This is also the "initial" procedure following |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
165 ## MATLAB nomenclature. After the loop we'll fix the string |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
166 ## for the first step. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
167 iter(niter+1).procedure = "golden"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
168 |
10296 | 169 e = ifelse (x >= xm, a - x, b - x); |
170 d = c * e; | |
171 endif | |
172 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
173 ## f must not be evaluated too close to x. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
174 u = x + max (abs (d), tol) * (sign (d) + (d == 0)); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
175 fu = fun (u); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
176 |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20727
diff
changeset
|
177 niter += 1; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
178 |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
179 iter(niter).funccount = nfev++; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
180 iter(niter).x = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
181 iter(niter).fx = fu; |
10296 | 182 |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
183 ## update a, b, v, w, and x |
10296 | 184 |
18290
61dab64aa5a6
fminbnd.m: Correctly handle certain functions with odd discontinuities (bug #36923)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
185 if (fu < fval) |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
186 if (u < x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
187 b = x; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
188 else |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
189 a = x; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
190 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
191 v = w; fv = fw; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
192 w = x; fw = fval; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
193 x = u; fval = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
194 else |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
195 ## The following if-statement was originally executed even if fu == fval. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
196 if (u < x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
197 a = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
198 else |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
199 b = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
200 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
201 if (fu <= fw || w == x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
202 v = w; fv = fw; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
203 w = u; fw = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
204 elseif (fu <= fv || v == x || v == w) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
205 v = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
206 fv = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
207 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
208 endif |
10296 | 209 |
210 ## If there's an output function, use it now. | |
211 if (outfcn) | |
212 optv.funccount = nfev; | |
213 optv.fval = fval; | |
214 optv.iteration = niter; | |
215 if (outfcn (x, optv, "iter")) | |
216 info = -1; | |
217 break; | |
218 endif | |
219 endif | |
220 endwhile | |
221 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
222 ## Fix the first step procedure. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
223 iter(1).procedure = "initial"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
224 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
225 ## Handle the "Display" option |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16772
diff
changeset
|
226 switch (displ) |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
227 case "iter" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
228 print_formatted_table (iter); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
229 print_exit_msg (info, struct("TolX", tolx, "fx", fval)); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
230 case "notify" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
231 if (info == 0) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
232 print_exit_msg (info, struct("fx",fval)); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
233 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
234 case "final" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
235 print_exit_msg (info, struct("TolX", tolx, "fx", fval)); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
236 case "off" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
237 "skip"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
238 otherwise |
20727
a5949b3d2332
Preface warning() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20726
diff
changeset
|
239 warning ("fminbnd: unknown option for Display: '%s'", displ); |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
240 endswitch |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
241 |
10296 | 242 output.iterations = niter; |
243 output.funcCount = nfev; | |
244 output.bracket = [a, b]; | |
245 ## FIXME: bracketf possibly unavailable. | |
246 | |
247 endfunction | |
248 | |
249 ## An assistant function that evaluates a function handle and checks for | |
250 ## bad results. | |
251 function fx = guarded_eval (fun, x) | |
252 fx = fun (x); | |
253 fx = fx(1); | |
254 if (! isreal (fx)) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
255 error ("fminbnd:notreal", "fminbnd: non-real value encountered"); |
10296 | 256 elseif (isnan (fx)) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
257 error ("fminbnd:isnan", "fminbnd: NaN value encountered"); |
10296 | 258 endif |
259 endfunction | |
260 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
261 ## A hack for printing a formatted table |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
262 function print_formatted_table (table) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
263 printf ("\n Func-count x f(x) Procedure\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
264 for row=table |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
265 printf("%5.5s %7.7s %8.8s\t%s\n", |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
266 int2str (row.funccount), num2str (row.x,"%.5f"), |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
267 num2str (row.fx,"%.6f"), row.procedure); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
268 endfor |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
269 printf ("\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
270 endfunction |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
271 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
272 ## Print either a success termination message or bad news |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
273 function print_exit_msg (info, opt=struct()) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
274 printf (""); |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16772
diff
changeset
|
275 switch (info) |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
276 case 1 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
277 printf ("Optimization terminated:\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
278 printf (" the current x satisfies the termination criteria using OPTIONS.TolX of %e\n", opt.TolX); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
279 case 0 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
280 printf ("Exiting: Maximum number of iterations has been exceeded\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
281 printf (" - increase MaxIter option.\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
282 printf (" Current function value: %.6f\n", opt.fx); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
283 case -1 |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
284 "FIXME"; # FIXME: what's the message MATLAB prints for this case? |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
285 otherwise |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
286 error ("fminbnd: internal error, info return code was %d", info); |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
287 endswitch |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
288 printf ("\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
289 endfunction |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
290 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
291 |
10296 | 292 %!shared opt0 |
293 %! 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
|
294 %!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
|
295 %!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
|
296 %!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
|
297 %!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)) |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19443
diff
changeset
|
298 %!assert (fminbnd (@(x) x > 0.3, 0, 1) < 0.3) |
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19443
diff
changeset
|
299 %!assert (fminbnd (@(x) sin (x), 0, 0), 0, eps) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
300 |
19443
0f79fa9b3a8c
fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18290
diff
changeset
|
301 %!error <lower bound cannot be greater> fminbnd (@(x) sin (x), 0, -pi) |
0f79fa9b3a8c
fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18290
diff
changeset
|
302 |