Mercurial > octave
annotate scripts/optimization/fminbnd.m @ 27390:baeed03c3766
doc: Use common verbiage to describe input FUN in optimization functions.
* fminbnd.m, fminunc.m, fsolve.m: Use common verbiage to describe input FUN.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 12 Sep 2019 09:57:34 -0700 |
parents | 0a62d9a6aa2d |
children | b442ec6dda5c |
rev | line source |
---|---|
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
26138
diff
changeset
|
1 ## Copyright (C) 2008-2019 VZLU Prague, a.s. |
10296 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
5 ## Octave is free software: you can redistribute it and/or modify it |
10296 | 6 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
7 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
8 ## (at your option) any later version. |
10296 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## GNU General Public License for more details. |
10296 | 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 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
17 ## <https://www.gnu.org/licenses/>. |
10296 | 18 ## |
19 ## Author: Jaroslav Hajek <highegg@gmail.com> | |
20 | |
21 ## -*- texinfo -*- | |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
22 ## @deftypefn {} {@var{x} =} fminbnd (@var{fun}, @var{a}, @var{b}) |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
23 ## @deftypefnx {} {@var{x} =} fminbnd (@var{fun}, @var{a}, @var{b}, @var{options}) |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
24 ## @deftypefnx {} {[@var{x}, @var{fval}, @var{info}, @var{output}] =} fminbnd (@dots{}) |
14895
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
25 ## 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
|
26 ## |
27390
baeed03c3766
doc: Use common verbiage to describe input FUN in optimization functions.
Rik <rik@octave.org>
parents:
27069
diff
changeset
|
27 ## @var{fun} is a function handle, inline function, or string containing the |
baeed03c3766
doc: Use common verbiage to describe input FUN in optimization functions.
Rik <rik@octave.org>
parents:
27069
diff
changeset
|
28 ## name of the function to evaluate. |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
29 ## |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
30 ## The starting interval is specified by @var{a} (left boundary) and @var{b} |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
31 ## (right boundary). The endpoints must be finite. |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
32 ## |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
33 ## @var{options} is a structure specifying additional parameters which |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
34 ## control the algorithm. Currently, @code{fminbnd} recognizes these options: |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
35 ## @qcode{"Display"}, @qcode{"FunValCheck"}, @qcode{"MaxFunEvals"}, |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
36 ## @qcode{"MaxIter"}, @qcode{"OutputFcn"}, @qcode{"TolX"}. |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
37 ## |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
38 ## @qcode{"MaxFunEvals"} proscribes the maximum number of function evaluations |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
39 ## before optimization is halted. The default value is 500. |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
40 ## The value must be a positive integer. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
41 ## |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
42 ## @qcode{"MaxIter"} proscribes the maximum number of algorithm iterations |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
43 ## before optimization is halted. The default value is 500. |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
44 ## The value must be a positive integer. |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
45 ## |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
46 ## @qcode{"TolX"} specifies the termination tolerance for the solution @var{x}. |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
47 ## The default is @code{1e-4}. |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
48 ## |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
49 ## For a description of the other options, see @ref{XREFoptimset,,optimset}. |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
50 ## To initialize an options structure with default values for @code{fminbnd} |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
51 ## use @code{options = optimset ("fminbnd")}. |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
52 ## |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
53 ## On exit, the function returns @var{x}, the approximate minimum point, and |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
54 ## @var{fval}, the function evaluated @var{x}. |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
55 ## |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
56 ## The third output @var{info} reports whether the algorithm succeeded and may |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
57 ## take one of the following values: |
10297
ed88ea036716
improve docs of fzero/fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
10296
diff
changeset
|
58 ## |
10296 | 59 ## @itemize |
60 ## @item 1 | |
61 ## The algorithm converged to a solution. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
62 ## |
10296 | 63 ## @item 0 |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
64 ## Iteration limit (either @code{MaxIter} or @code{MaxFunEvals}) exceeded. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
65 ## |
10296 | 66 ## @item -1 |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
67 ## The algorithm was terminated by a user @code{OutputFcn}. |
10296 | 68 ## @end itemize |
14895
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
69 ## |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
70 ## Programming Notes: The search for a minimum is restricted to be in the |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
71 ## finite interval bound by @var{a} and @var{b}. If you have only one initial |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
72 ## point to begin searching from then you will need to use an unconstrained |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
73 ## minimization algorithm such as @code{fminunc} or @code{fminsearch}. |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
74 ## @code{fminbnd} internally uses a Golden Section search strategy. |
14895
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
75 ## @seealso{fzero, fminunc, fminsearch, optimset} |
10296 | 76 ## @end deftypefn |
77 | |
78 ## 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
|
79 ## 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
|
80 ## Prentice-Hall (1973) |
10296 | 81 |
13027
b9a89ca0fb75
prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
82 ## 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
|
83 ## PKG_ADD: [~] = __all_opts__ ("fminbnd"); |
10296 | 84 |
22765
01aae08a0105
maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
85 function [x, fval, info, output] = fminbnd (fun, a, b, options = struct ()) |
10296 | 86 |
87 ## Get default options if requested. | |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
88 if (nargin == 1 && ischar (fun) && strcmp (fun, "defaults")) |
26138
804e18e3e320
Reenable query of optimization options (bugs #54952 and #55089).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
26118
diff
changeset
|
89 x = struct ("Display", "notify", "FunValCheck", "off", |
804e18e3e320
Reenable query of optimization options (bugs #54952 and #55089).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
26118
diff
changeset
|
90 "MaxFunEvals", 500, "MaxIter", 500, |
804e18e3e320
Reenable query of optimization options (bugs #54952 and #55089).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
26118
diff
changeset
|
91 "OutputFcn", [], "TolX", 1e-4); |
10296 | 92 return; |
93 endif | |
94 | |
95 if (nargin < 2 || nargin > 4) | |
96 print_usage (); | |
97 endif | |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19443
diff
changeset
|
98 |
22765
01aae08a0105
maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
99 if (a > b) |
19443
0f79fa9b3a8c
fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18290
diff
changeset
|
100 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
|
101 "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
|
102 endif |
10296 | 103 |
104 if (ischar (fun)) | |
26118
7502fce4cd3a
str2func: eliminate optional second "global" argument
John W. Eaton <jwe@octave.org>
parents:
25740
diff
changeset
|
105 fun = str2func (fun); |
10296 | 106 endif |
107 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
108 displ = optimget (options, "Display", "notify"); |
10296 | 109 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on"); |
110 outfcn = optimget (options, "OutputFcn"); | |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
111 tolx = optimget (options, "TolX", 1e-4); |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
112 maxiter = optimget (options, "MaxIter", 500); |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
113 maxfev = optimget (options, "MaxFunEvals", 500); |
10296 | 114 |
115 if (funvalchk) | |
116 ## Replace fun with a guarded version. | |
117 fun = @(x) guarded_eval (fun, x); | |
118 endif | |
119 | |
120 ## The default exit flag if exceeded number of iterations. | |
121 info = 0; | |
122 niter = 0; | |
123 nfev = 0; | |
124 | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
125 c = 0.5*(3 - sqrt (5)); |
10296 | 126 v = a + c*(b-a); |
127 w = x = v; | |
128 e = 0; | |
129 fv = fw = fval = fun (x); | |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20727
diff
changeset
|
130 nfev += 1; |
10296 | 131 |
22765
01aae08a0105
maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
132 if (isa (a, "single") || isa (b, "single") || isa (fval, "single")) |
21099
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
133 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
|
134 else |
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
135 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
|
136 endif |
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
137 |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
138 ## Only for display purposes. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
139 iter(1).funccount = nfev; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
140 iter(1).x = x; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
141 iter(1).fx = fval; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
142 |
10296 | 143 while (niter < maxiter && nfev < maxfev) |
144 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
|
145 ## 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
|
146 ## 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
|
147 ## 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
|
148 tol = 2 * sqrteps * abs (x) + tolx / 3; |
10296 | 149 if (abs (x - xm) <= (2*tol - 0.5*(b-a))) |
150 info = 1; | |
151 break; | |
152 endif | |
153 | |
154 if (abs (e) > tol) | |
155 dogs = false; | |
156 ## Try inverse parabolic step. | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
157 iter(niter+1).procedure = "parabolic"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
158 |
10296 | 159 r = (x - w)*(fval - fv); |
160 q = (x - v)*(fval - fw); | |
161 p = (x - v)*q - (x - w)*r; | |
162 q = 2*(q - r); | |
163 p *= -sign (q); | |
164 q = abs (q); | |
165 r = e; | |
166 e = d; | |
167 | |
168 if (abs (p) < abs (0.5*q*r) && p > q*(a-x) && p < q*(b-x)) | |
169 ## The parabolic step is acceptable. | |
170 d = p / q; | |
171 u = x + d; | |
172 | |
173 ## 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
|
174 if (min (u-a, b-u) < 2*tol) |
10296 | 175 d = tol * (sign (xm - x) + (xm == x)); |
176 endif | |
177 else | |
178 dogs = true; | |
179 endif | |
180 else | |
181 dogs = true; | |
182 endif | |
183 if (dogs) | |
184 ## Default to golden section step. | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
185 |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
186 ## WARNING: This is also the "initial" procedure following MATLAB |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
187 ## nomenclature. After the loop we'll fix the string for the first step. |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
188 iter(niter+1).procedure = "golden"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
189 |
10296 | 190 e = ifelse (x >= xm, a - x, b - x); |
191 d = c * e; | |
192 endif | |
193 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
194 ## 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
|
195 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
|
196 fu = fun (u); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
197 |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20727
diff
changeset
|
198 niter += 1; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
199 |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
200 iter(niter).funccount = nfev++; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
201 iter(niter).x = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
202 iter(niter).fx = fu; |
10296 | 203 |
21562
6c2fd62db1f7
maint: Eliminate accidental double spaces in code.
Rik <rik@octave.org>
parents:
21099
diff
changeset
|
204 ## update a, b, v, w, and x |
10296 | 205 |
18290
61dab64aa5a6
fminbnd.m: Correctly handle certain functions with odd discontinuities (bug #36923)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
206 if (fu < fval) |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
207 if (u < x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
208 b = x; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
209 else |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
210 a = x; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
211 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
212 v = w; fv = fw; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
213 w = x; fw = fval; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
214 x = u; fval = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
215 else |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
216 ## 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
|
217 if (u < x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
218 a = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
219 else |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
220 b = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
221 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
222 if (fu <= fw || w == x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
223 v = w; fv = fw; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
224 w = u; fw = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
225 elseif (fu <= fv || v == x || v == w) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
226 v = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
227 fv = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
228 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
229 endif |
10296 | 230 |
231 ## If there's an output function, use it now. | |
232 if (outfcn) | |
233 optv.funccount = nfev; | |
234 optv.fval = fval; | |
235 optv.iteration = niter; | |
236 if (outfcn (x, optv, "iter")) | |
237 info = -1; | |
238 break; | |
239 endif | |
240 endif | |
241 endwhile | |
242 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
243 ## Fix the first step procedure. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
244 iter(1).procedure = "initial"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
245 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
246 ## Handle the "Display" option |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16772
diff
changeset
|
247 switch (displ) |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
248 case "iter" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
249 print_formatted_table (iter); |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
250 print_exit_msg (info, struct ("TolX", tolx, "fx", fval)); |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
251 case "notify" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
252 if (info == 0) |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
253 print_exit_msg (info, struct ("fx",fval)); |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
254 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
255 case "final" |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
256 print_exit_msg (info, struct ("TolX", tolx, "fx", fval)); |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
257 case "off" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
258 "skip"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
259 otherwise |
20727
a5949b3d2332
Preface warning() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20726
diff
changeset
|
260 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
|
261 endswitch |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
262 |
10296 | 263 output.iterations = niter; |
264 output.funcCount = nfev; | |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
265 output.algorithm = "golden section search, parabolic interpolation"; |
10296 | 266 output.bracket = [a, b]; |
267 ## FIXME: bracketf possibly unavailable. | |
268 | |
269 endfunction | |
270 | |
21759
b002b4331a12
maint: Use two newlines after endfunction and start of BIST tests.
Rik <rik@octave.org>
parents:
21751
diff
changeset
|
271 ## A helper function that evaluates a function and checks for bad results. |
10296 | 272 function fx = guarded_eval (fun, x) |
273 fx = fun (x); | |
274 fx = fx(1); | |
275 if (! isreal (fx)) | |
27069
0a62d9a6aa2d
Place Octave's warning and error IDs in to the "Octave" namespace (bug #56213).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
276 error ("Octave:fmindbnd:notreal", "fminbnd: non-real value encountered"); |
10296 | 277 elseif (isnan (fx)) |
27069
0a62d9a6aa2d
Place Octave's warning and error IDs in to the "Octave" namespace (bug #56213).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
278 error ("Octave:fmindbnd:isnan", "fminbnd: NaN value encountered"); |
10296 | 279 endif |
280 endfunction | |
281 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
282 ## A hack for printing a formatted table |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
283 function print_formatted_table (table) |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
284 printf ("\n Func-count x f(x) Procedure\n"); |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
285 for row=table |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
286 printf("%5.5s %7.7s %8.8s\t%s\n", |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
287 int2str (row.funccount), num2str (row.x,"%.5f"), |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
288 num2str (row.fx,"%.6f"), row.procedure); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
289 endfor |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
290 printf ("\n"); |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
291 endfunction |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
292 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
293 ## 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
|
294 function print_exit_msg (info, opt=struct()) |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
295 printf (""); |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16772
diff
changeset
|
296 switch (info) |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
297 case 1 |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
298 printf ("Optimization terminated:\n"); |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
299 printf (" the current x satisfies the termination criteria using OPTIONS.TolX of %e\n", opt.TolX); |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
300 case 0 |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
301 printf ("Exiting: Maximum number of iterations has been exceeded\n"); |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
302 printf (" - increase MaxIter option.\n"); |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
303 printf (" Current function value: %.6f\n", opt.fx); |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
304 case -1 |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
305 "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
|
306 otherwise |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
307 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
|
308 endswitch |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
309 printf ("\n"); |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
310 endfunction |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
311 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
312 |
10296 | 313 %!shared opt0 |
314 %! 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
|
315 %!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
|
316 %!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
|
317 %!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
|
318 %!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
|
319 %!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
|
320 %!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
|
321 |
19443
0f79fa9b3a8c
fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18290
diff
changeset
|
322 %!error <lower bound cannot be greater> fminbnd (@(x) sin (x), 0, -pi) |