Mercurial > octave
annotate scripts/optimization/fminbnd.m @ 30379:363fb10055df stable
maint: Style check m-files ahead of 7.1 release.
* Map.m, integral3.m, logspace.m, quad2d.m, quadgk.m, quadl.m, tsearchn.m,
get_first_help_sentence.m, print_usage.m, getframe.m, imformats.m,
javaclasspath.m, condest.m, null.m, ordeig.m, inputParser.m, license.m,
memory.m, methods.m, __publish_html_output__.m, __publish_latex_output__.m,
publish.m, ode15s.m, fminbnd.m, fzero.m, configure_make.m, get_description.m,
get_forge_pkg.m, annotation.m, camlookat.m, legend.m, __gnuplot_legend__.m,
bar.m, colorbar.m, fill3.m, isosurface.m, plotyy.m, polar.m, __bar__.m,
__ezplot__.m, __patch__.m, __pie__.m, __plt__.m, __scatter__.m, smooth3.m,
stemleaf.m, __gnuplot_drawnow__.m, print.m, printd.m, __add_default_menu__.m,
__gnuplot_draw_axes__.m, __gnuplot_print__.m, __print_parse_opts__.m,
struct2hdl.m, profexport.m, profile.m, movfun.m, sprandsym.m, betaincinv.m,
factor.m, nchoosek.m, gallery.m, hadamard.m, iqr.m, ranks.m,
__run_test_suite__.m, test.m, datevec.m, weboptions.m:
Style check m-files ahead of 7.1 release.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 26 Nov 2021 20:53:22 -0800 |
parents | 26f2daafa270 |
children | 796f54d4ddbf |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
29358
0a5b15007766
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29302
diff
changeset
|
3 ## Copyright (C) 2008-2021 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27390
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
10296 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
10296 | 11 ## 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
|
12 ## 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
|
13 ## (at your option) any later version. |
10296 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
10296 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
21 ## 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
|
22 ## <https://www.gnu.org/licenses/>. |
10296 | 23 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
10296 | 25 |
26 ## -*- texinfo -*- | |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
27 ## @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
|
28 ## @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
|
29 ## @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
|
30 ## 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
|
31 ## |
27390
baeed03c3766
doc: Use common verbiage to describe input FUN in optimization functions.
Rik <rik@octave.org>
parents:
27069
diff
changeset
|
32 ## @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
|
33 ## 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
|
34 ## |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
35 ## 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
|
36 ## (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
|
37 ## |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
38 ## @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
|
39 ## 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
|
40 ## @qcode{"Display"}, @qcode{"FunValCheck"}, @qcode{"MaxFunEvals"}, |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
41 ## @qcode{"MaxIter"}, @qcode{"OutputFcn"}, @qcode{"TolX"}. |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
42 ## |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
43 ## @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
|
44 ## 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
|
45 ## 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
|
46 ## |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
47 ## @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
|
48 ## 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
|
49 ## 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
|
50 ## |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
51 ## @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
|
52 ## 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
|
53 ## |
30377
26f2daafa270
doc: Use consistent styling for @ref, @xref, @pxref statements.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
54 ## For a description of the other options, |
26f2daafa270
doc: Use consistent styling for @ref, @xref, @pxref statements.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
55 ## @pxref{XREFoptimset,,@code{optimset}}. |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
56 ## 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
|
57 ## use @code{options = optimset ("fminbnd")}. |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
58 ## |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
59 ## 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
|
60 ## @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
|
61 ## |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
62 ## 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
|
63 ## take one of the following values: |
10297
ed88ea036716
improve docs of fzero/fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
10296
diff
changeset
|
64 ## |
10296 | 65 ## @itemize |
66 ## @item 1 | |
67 ## The algorithm converged to a solution. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
68 ## |
10296 | 69 ## @item 0 |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
70 ## 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
|
71 ## |
10296 | 72 ## @item -1 |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
73 ## The algorithm was terminated by a user @code{OutputFcn}. |
10296 | 74 ## @end itemize |
14895
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
75 ## |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
76 ## 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
|
77 ## 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
|
78 ## 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
|
79 ## 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
|
80 ## @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
|
81 ## @seealso{fzero, fminunc, fminsearch, optimset} |
10296 | 82 ## @end deftypefn |
83 | |
84 ## 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
|
85 ## 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
|
86 ## Prentice-Hall (1973) |
10296 | 87 |
13027
b9a89ca0fb75
prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
88 ## 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
|
89 ## PKG_ADD: [~] = __all_opts__ ("fminbnd"); |
10296 | 90 |
22765
01aae08a0105
maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
91 function [x, fval, info, output] = fminbnd (fun, a, b, options = struct ()) |
10296 | 92 |
93 ## Get default options if requested. | |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
94 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
|
95 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
|
96 "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
|
97 "OutputFcn", [], "TolX", 1e-4); |
10296 | 98 return; |
99 endif | |
100 | |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
101 if (nargin < 2) |
10296 | 102 print_usage (); |
103 endif | |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19443
diff
changeset
|
104 |
22765
01aae08a0105
maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
105 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
|
106 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
|
107 "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
|
108 endif |
10296 | 109 |
110 if (ischar (fun)) | |
26118
7502fce4cd3a
str2func: eliminate optional second "global" argument
John W. Eaton <jwe@octave.org>
parents:
25740
diff
changeset
|
111 fun = str2func (fun); |
10296 | 112 endif |
113 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
114 displ = optimget (options, "Display", "notify"); |
10296 | 115 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on"); |
116 outfcn = optimget (options, "OutputFcn"); | |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
117 tolx = optimget (options, "TolX", 1e-4); |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
118 maxiter = optimget (options, "MaxIter", 500); |
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
119 maxfev = optimget (options, "MaxFunEvals", 500); |
10296 | 120 |
121 if (funvalchk) | |
122 ## Replace fun with a guarded version. | |
123 fun = @(x) guarded_eval (fun, x); | |
124 endif | |
125 | |
126 ## The default exit flag if exceeded number of iterations. | |
127 info = 0; | |
128 niter = 0; | |
129 nfev = 0; | |
130 | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
131 c = 0.5*(3 - sqrt (5)); |
10296 | 132 v = a + c*(b-a); |
133 w = x = v; | |
134 e = 0; | |
135 fv = fw = fval = fun (x); | |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20727
diff
changeset
|
136 nfev += 1; |
10296 | 137 |
22765
01aae08a0105
maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
138 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
|
139 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
|
140 else |
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
141 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
|
142 endif |
52af4092f863
For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
143 |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
144 ## Only for display purposes. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
145 iter(1).funccount = nfev; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
146 iter(1).x = x; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
147 iter(1).fx = fval; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
148 |
10296 | 149 while (niter < maxiter && nfev < maxfev) |
150 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
|
151 ## 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
|
152 ## 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
|
153 ## 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
|
154 tol = 2 * sqrteps * abs (x) + tolx / 3; |
10296 | 155 if (abs (x - xm) <= (2*tol - 0.5*(b-a))) |
156 info = 1; | |
157 break; | |
158 endif | |
159 | |
160 if (abs (e) > tol) | |
161 dogs = false; | |
162 ## Try inverse parabolic step. | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
163 iter(niter+1).procedure = "parabolic"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
164 |
10296 | 165 r = (x - w)*(fval - fv); |
166 q = (x - v)*(fval - fw); | |
167 p = (x - v)*q - (x - w)*r; | |
168 q = 2*(q - r); | |
169 p *= -sign (q); | |
170 q = abs (q); | |
171 r = e; | |
172 e = d; | |
173 | |
174 if (abs (p) < abs (0.5*q*r) && p > q*(a-x) && p < q*(b-x)) | |
175 ## The parabolic step is acceptable. | |
176 d = p / q; | |
177 u = x + d; | |
178 | |
179 ## 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
|
180 if (min (u-a, b-u) < 2*tol) |
10296 | 181 d = tol * (sign (xm - x) + (xm == x)); |
182 endif | |
183 else | |
184 dogs = true; | |
185 endif | |
186 else | |
187 dogs = true; | |
188 endif | |
189 if (dogs) | |
190 ## Default to golden section step. | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
191 |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
192 ## 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
|
193 ## 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
|
194 iter(niter+1).procedure = "golden"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
195 |
10296 | 196 e = ifelse (x >= xm, a - x, b - x); |
197 d = c * e; | |
198 endif | |
199 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
200 ## 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
|
201 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
|
202 fu = fun (u); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
203 |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20727
diff
changeset
|
204 niter += 1; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
205 |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
206 iter(niter).funccount = nfev++; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
207 iter(niter).x = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
208 iter(niter).fx = fu; |
10296 | 209 |
21562
6c2fd62db1f7
maint: Eliminate accidental double spaces in code.
Rik <rik@octave.org>
parents:
21099
diff
changeset
|
210 ## update a, b, v, w, and x |
10296 | 211 |
18290
61dab64aa5a6
fminbnd.m: Correctly handle certain functions with odd discontinuities (bug #36923)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
212 if (fu < fval) |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
213 if (u < x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
214 b = x; |
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 a = x; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
217 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
218 v = w; fv = fw; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
219 w = x; fw = fval; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
220 x = u; fval = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
221 else |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
222 ## 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
|
223 if (u < x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
224 a = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
225 else |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
226 b = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
227 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
228 if (fu <= fw || w == x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
229 v = w; fv = fw; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
230 w = u; fw = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
231 elseif (fu <= fv || v == x || v == w) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
232 v = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
233 fv = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
234 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
235 endif |
10296 | 236 |
237 ## If there's an output function, use it now. | |
29302
a121ce5a515f
fminbnd.m: do not ignore "OutputFcn" (bug #59901)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
27978
diff
changeset
|
238 if (! isempty (outfcn)) |
10296 | 239 optv.funccount = nfev; |
240 optv.fval = fval; | |
241 optv.iteration = niter; | |
242 if (outfcn (x, optv, "iter")) | |
243 info = -1; | |
244 break; | |
245 endif | |
246 endif | |
247 endwhile | |
248 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
249 ## Fix the first step procedure. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
250 iter(1).procedure = "initial"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
251 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
252 ## Handle the "Display" option |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16772
diff
changeset
|
253 switch (displ) |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
254 case "iter" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
255 print_formatted_table (iter); |
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 "notify" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
258 if (info == 0) |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
259 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
|
260 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
261 case "final" |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
262 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
|
263 case "off" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
264 "skip"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
265 otherwise |
20727
a5949b3d2332
Preface warning() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20726
diff
changeset
|
266 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
|
267 endswitch |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
268 |
10296 | 269 output.iterations = niter; |
270 output.funcCount = nfev; | |
25740
b1e0e58971f9
fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
271 output.algorithm = "golden section search, parabolic interpolation"; |
10296 | 272 output.bracket = [a, b]; |
273 ## FIXME: bracketf possibly unavailable. | |
274 | |
275 endfunction | |
276 | |
21759
b002b4331a12
maint: Use two newlines after endfunction and start of BIST tests.
Rik <rik@octave.org>
parents:
21751
diff
changeset
|
277 ## A helper function that evaluates a function and checks for bad results. |
10296 | 278 function fx = guarded_eval (fun, x) |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30377
diff
changeset
|
279 |
10296 | 280 fx = fun (x); |
281 fx = fx(1); | |
282 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
|
283 error ("Octave:fmindbnd:notreal", "fminbnd: non-real value encountered"); |
10296 | 284 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
|
285 error ("Octave:fmindbnd:isnan", "fminbnd: NaN value encountered"); |
10296 | 286 endif |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30377
diff
changeset
|
287 |
10296 | 288 endfunction |
289 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
290 ## A hack for printing a formatted table |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
291 function print_formatted_table (table) |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
292 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
|
293 for row=table |
28912
0de38a6ef693
maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
294 printf ("%5.5s %7.7s %8.8s\t%s\n", |
0de38a6ef693
maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
295 int2str (row.funccount), num2str (row.x,"%.5f"), |
0de38a6ef693
maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
296 num2str (row.fx,"%.6f"), row.procedure); |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
297 endfor |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
298 printf ("\n"); |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
299 endfunction |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
300 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
301 ## Print either a success termination message or bad news |
28912
0de38a6ef693
maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
302 function print_exit_msg (info, opt=struct ()) |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30377
diff
changeset
|
303 |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
304 printf (""); |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16772
diff
changeset
|
305 switch (info) |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
306 case 1 |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
307 printf ("Optimization terminated:\n"); |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
308 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
|
309 case 0 |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
310 printf ("Exiting: Maximum number of iterations has been exceeded\n"); |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
311 printf (" - increase MaxIter option.\n"); |
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
312 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
|
313 case -1 |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
314 "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
|
315 otherwise |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
316 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
|
317 endswitch |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
318 printf ("\n"); |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30377
diff
changeset
|
319 |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
320 endfunction |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
321 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
322 |
10296 | 323 %!shared opt0 |
324 %! 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
|
325 %!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
|
326 %!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
|
327 %!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
|
328 %!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
|
329 %!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
|
330 %!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
|
331 |
19443
0f79fa9b3a8c
fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18290
diff
changeset
|
332 %!error <lower bound cannot be greater> fminbnd (@(x) sin (x), 0, -pi) |