annotate scripts/optimization/fminbnd.m @ 31548:c8ad083a5802 stable

maint: Clean up m-files before Octave 8.1 release. * external.txi, oop.txi, Table.h, documentation.cc, gui-preferences-ed.h, lo-specfun.cc, range.tst : Eliminate triple newlines. * Map.m, MemoizedFunction.m, delaunayn.m, inputParser.m, __publish_latex_output__.m, publish.m, unpack.m, fminbnd.m, __add_default_menu__.m, gammainc.m, gallery.m, hadamard.m, weboptions.m: Add newline after keyword "function" or before keyword "endfunction" for readability. * getaudiodata.m, pkg.m : Add semicolon to end of line for error() statement. * movegui.m: Combine mutliple calls to set() into one for performance. * __unimplemented__.m (missing_functions): Remove missing functions that have been implemented. * __vectorize__.m, check_default_input.m, betaincinv.m, gammaincinv.m: Remove semicolon at end of line with "function" declaration. * weboptions.m: Remove semicolon at end of line with "if" keyword. * integrate_adaptive.m, factor.m: Use keyword "endif" rather than bare "end".
author Rik <rik@octave.org>
date Fri, 25 Nov 2022 21:23:54 -0800
parents e1788b1a315f
children 597f3ee61a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 30379
diff changeset
3 ## Copyright (C) 2008-2022 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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7 ##
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 ## This file is part of Octave.
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 ##
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 ##
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
23 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
26 ## -*- texinfo -*-
30893
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
27 ## @deftypefn {} {@var{x} =} fminbnd (@var{fcn}, @var{a}, @var{b})
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
28 ## @deftypefnx {} {@var{x} =} fminbnd (@var{fcn}, @var{a}, @var{b}, @var{options})
25740
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 ##
30893
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
32 ## @var{fcn} is a function handle, inline function, or string containing the
27390
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
65 ## @itemize
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
66 ## @item 1
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
82 ## @end deftypefn
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
83
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
90
30893
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
91 function [x, fval, info, output] = fminbnd (fcn, a, b, options = struct ())
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
92
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
93 ## Get default options if requested.
30893
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
94 if (nargin == 1 && ischar (fcn) && strcmp (fcn, "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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
98 return;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
99 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
102 print_usage ();
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
109
30893
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
110 if (ischar (fcn))
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
111 fcn = str2func (fcn);
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
112 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
115 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on");
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
120
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
121 if (funvalchk)
30893
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
122 ## Replace fcn with a guarded version.
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
123 fcn = @(x) guarded_eval (fcn, x);
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
124 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
125
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
126 ## The default exit flag if exceeded number of iterations.
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
127 info = 0;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
128 niter = 0;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
129 nfev = 0;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
132 v = a + c*(b-a);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
133 w = x = v;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
134 e = 0;
30893
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
135 fv = fw = fval = fcn (x);
20735
418ae0cb752f Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents: 20727
diff changeset
136 nfev += 1;
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
149 while (niter < maxiter && nfev < maxfev)
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
155 if (abs (x - xm) <= (2*tol - 0.5*(b-a)))
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
156 info = 1;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
157 break;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
158 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
159
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
160 if (abs (e) > tol)
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
161 dogs = false;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
165 r = (x - w)*(fval - fv);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
166 q = (x - v)*(fval - fw);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
167 p = (x - v)*q - (x - w)*r;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
168 q = 2*(q - r);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
169 p *= -sign (q);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
170 q = abs (q);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
171 r = e;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
172 e = d;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
173
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
174 if (abs (p) < abs (0.5*q*r) && p > q*(a-x) && p < q*(b-x))
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
175 ## The parabolic step is acceptable.
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
176 d = p / q;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
177 u = x + d;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
178
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
181 d = tol * (sign (xm - x) + (xm == x));
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
182 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
183 else
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
184 dogs = true;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
185 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
186 else
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
187 dogs = true;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
188 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
189 if (dogs)
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
196 e = ifelse (x >= xm, a - x, b - x);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
197 d = c * e;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
198 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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));
30893
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
202 fu = fcn (u);
15706
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
236
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
239 optv.funccount = nfev;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
240 optv.fval = fval;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
241 optv.iteration = niter;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
242 if (outfcn (x, optv, "iter"))
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
243 info = -1;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
244 break;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
245 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
246 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
247 endwhile
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
269 output.iterations = niter;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
272 output.bracket = [a, b];
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
273 ## FIXME: bracketf possibly unavailable.
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
274
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
275 endfunction
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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.
30893
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
278 function fx = guarded_eval (fcn, x)
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
279
30893
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
280 fx = fcn (x);
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
281 fx = fx(1);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
286 endif
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
287
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
288 endfunction
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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)
31548
c8ad083a5802 maint: Clean up m-files before Octave 8.1 release.
Rik <rik@octave.org>
parents: 30893
diff changeset
292
30893
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
293 printf ("\n Fcn-count x f(x) Procedure\n");
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
294 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
295 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
296 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
297 num2str (row.fx,"%.6f"), row.procedure);
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
298 endfor
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
299 printf ("\n");
31548
c8ad083a5802 maint: Clean up m-files before Octave 8.1 release.
Rik <rik@octave.org>
parents: 30893
diff changeset
300
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
301 endfunction
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
302
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
303 ## 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
304 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
305
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
306 printf ("");
16933
e39f00a32dc7 maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents: 16772
diff changeset
307 switch (info)
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
308 case 1
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
309 printf ("Optimization terminated:\n");
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
310 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
311 case 0
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
312 printf ("Exiting: Maximum number of iterations has been exceeded\n");
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
313 printf (" - increase MaxIter option.\n");
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
314 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
315 case -1
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
316 "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
317 otherwise
20726
25d676f9619c Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents: 20165
diff changeset
318 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
319 endswitch
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
320 printf ("\n");
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
321
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
322 endfunction
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
323
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
324
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
325 %!shared opt0
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
326 %! 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
327 %!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
328 %!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
329 %!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
330 %!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
331 %!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
332 %!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
333
19443
0f79fa9b3a8c fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18290
diff changeset
334 %!error <lower bound cannot be greater> fminbnd (@(x) sin (x), 0, -pi)