annotate scripts/optimization/fminbnd.m @ 28789:28de41192f3c

Eliminate unneeded verification of nargin, nargout in m-files. * FIRfilter.m, FIRfilter_aggregation.m, get.m, polynomial.m, polynomial_superiorto.m, polynomial2.m, makeUniqueStrings.m, base64decode.m, base64encode.m, cd.m, lin2mu.m, record.m, sound.m, soundsc.m, accumarray.m, accumdim.m, bitcmp.m, bitset.m, cart2pol.m, celldisp.m, circshift.m, cplxpair.m, cumtrapz.m, flip.m, idivide.m, interpft.m, logspace.m, pol2cart.m, polyarea.m, postpad.m, prepad.m, rat.m, rot90.m, rotdim.m, shift.m, shiftdim.m, sortrows.m, trapz.m, dsearch.m, dsearchn.m, getappdata.m, getpixelposition.m, guidata.m, guihandles.m, isappdata.m, listfonts.m, uigetdir.m, waitforbuttonpress.m, __makeinfo__.m, doc.m, get_first_help_sentence.m, autumn.m, bone.m, brighten.m, cmpermute.m, cmunique.m, colorcube.m, contrast.m, cool.m, copper.m, cubehelix.m, flag.m, gray.m, gray2ind.m, hot.m, hsv.m, im2double.m, im2frame.m, imformats.m, jet.m, lines.m, ocean.m, pink.m, prism.m, rainbow.m, rgbplot.m, spinmap.m, spring.m, summer.m, viridis.m, white.m, winter.m, beep.m, importdata.m, is_valid_file_id.m, javachk.m, javaclasspath.m, findstr.m, genvarname.m, strmatch.m, bandwidth.m, commutation_matrix.m, cond.m, cross.m, isdefinite.m, ishermitian.m, issymmetric.m, krylov.m, linsolve.m, logm.m, lscov.m, null.m, ordeig.m, orth.m, rank.m, rref.m, vecnorm.m, bunzip2.m, citation.m, computer.m, copyfile.m, dir.m, dos.m, fileattrib.m, gunzip.m, inputParser.m, inputname.m, ismac.m, ispc.m, isunix.m, license.m, list_primes.m, methods.m, mkdir.m, movefile.m, nargchk.m, news.m, orderfields.m, recycle.m, tar.m, unix.m, unpack.m, untar.m, unzip.m, ver.m, version.m, what.m, zip.m, decic.m, fminbnd.m, fminunc.m, fsolve.m, fzero.m, glpk.m, humps.m, lsqnonneg.m, optimget.m, pqpnonneg.m, sqp.m, pathdef.m, camlookat.m, hidden.m, specular.m, plotmatrix.m, smooth3.m, sombrero.m, stemleaf.m, __gnuplot_drawnow__.m, __opengl_info__.m, ancestor.m, cla.m, close.m, closereq.m, copyobj.m, gca.m, gcf.m, ginput.m, graphics_toolkit.m, groot.m, hgload.m, hgsave.m, isgraphics.m, ishold.m, linkaxes.m, meshgrid.m, newplot.m, refresh.m, refreshdata.m, rotate.m, saveas.m, struct2hdl.m, conv.m, mkpp.m, mpoles.m, padecoef.m, pchip.m, polyder.m, polyfit.m, polygcd.m, polyint.m, polyout.m, polyval.m, ppder.m, ppint.m, getpref.m, ispref.m, rmpref.m, profexport.m, profshow.m, powerset.m, arch_fit.m, arma_rnd.m, blackman.m, detrend.m, diffpara.m, fftconv.m, fftfilt.m, filter2.m, freqz.m, freqz_plot.m, hamming.m, hanning.m, sinetone.m, sinewave.m, spectral_adf.m, spectral_xdf.m, stft.m, unwrap.m, gplot.m, ichol.m, ilu.m, spdiags.m, sprand.m, sprandn.m, spstats.m, svds.m, treelayout.m, treeplot.m, betainc.m, betaincinv.m, ellipke.m, gammainc.m, gammaincinv.m, legendre.m, pow2.m, hankel.m, pascal.m, rosser.m, toeplitz.m, bounds.m, corr.m, cov.m, histc.m, kendall.m, kurtosis.m, mad.m, mode.m, moment.m, prctile.m, quantile.m, range.m, ranks.m, run_count.m, skewness.m, spearman.m, std.m, var.m, zscore.m, dec2base.m, dec2bin.m, dec2hex.m, index.m, mat2str.m, native2unicode.m, ostrsplit.m, strjoin.m, strjust.m, strtok.m, substr.m, unicode2native.m, untabify.m, __debug_octave__.m, demo.m, example.m, fail.m, oruntests.m, dump_demos.m, speed.m, test.m, date.m, datenum.m, datestr.m, datevec.m, is_leap_year.m, now.m, weekday.m: Eliminate unneeded verification of nargin, nargout in m-files now that the interpreter checks these values.
author Rik <rik@octave.org>
date Thu, 24 Sep 2020 14:44:58 -0700
parents a4268efb7334
children 0de38a6ef693
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 ##
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
3 ## Copyright (C) 2008-2020 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 -*-
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 ##
25740
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
54 ## 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
55 ## 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
56 ## use @code{options = optimset ("fminbnd")}.
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
57 ##
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
58 ## 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
59 ## @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
60 ##
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
61 ## 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
62 ## take one of the following values:
10297
ed88ea036716 improve docs of fzero/fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents: 10296
diff changeset
63 ##
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
64 ## @itemize
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
65 ## @item 1
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
66 ## The algorithm converged to a solution.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
67 ##
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
68 ## @item 0
25740
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
69 ## 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
70 ##
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
71 ## @item -1
25740
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
72 ## The algorithm was terminated by a user @code{OutputFcn}.
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
73 ## @end itemize
14895
e0525ecf156e Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents: 14868
diff changeset
74 ##
25740
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
75 ## 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
76 ## 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
77 ## 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
78 ## 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
79 ## @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
80 ## @seealso{fzero, fminunc, fminsearch, optimset}
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
81 ## @end deftypefn
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
82
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
83 ## 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
84 ## 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
85 ## Prentice-Hall (1973)
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
86
13027
b9a89ca0fb75 prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
87 ## 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
88 ## PKG_ADD: [~] = __all_opts__ ("fminbnd");
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
89
22765
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
90 function [x, fval, info, output] = fminbnd (fun, a, b, options = struct ())
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
91
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
92 ## Get default options if requested.
25740
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
93 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
94 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
95 "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
96 "OutputFcn", [], "TolX", 1e-4);
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
97 return;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
98 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
99
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 27978
diff changeset
100 if (nargin < 2)
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
101 print_usage ();
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
102 endif
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19443
diff changeset
103
22765
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
104 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
105 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
106 "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
107 endif
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
108
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
109 if (ischar (fun))
26118
7502fce4cd3a str2func: eliminate optional second "global" argument
John W. Eaton <jwe@octave.org>
parents: 25740
diff changeset
110 fun = str2func (fun);
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
111 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
112
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
113 displ = optimget (options, "Display", "notify");
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
114 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on");
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
115 outfcn = optimget (options, "OutputFcn");
25740
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
116 tolx = optimget (options, "TolX", 1e-4);
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
117 maxiter = optimget (options, "MaxIter", 500);
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
118 maxfev = optimget (options, "MaxFunEvals", 500);
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
119
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
120 if (funvalchk)
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
121 ## Replace fun with a guarded version.
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
122 fun = @(x) guarded_eval (fun, x);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
123 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
124
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
125 ## The default exit flag if exceeded number of iterations.
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
126 info = 0;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
127 niter = 0;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
128 nfev = 0;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
129
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14552
diff changeset
130 c = 0.5*(3 - sqrt (5));
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
131 v = a + c*(b-a);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
132 w = x = v;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
133 e = 0;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
134 fv = fw = fval = fun (x);
20735
418ae0cb752f Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents: 20727
diff changeset
135 nfev += 1;
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
136
22765
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
137 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
138 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
139 else
52af4092f863 For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents: 20852
diff changeset
140 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
141 endif
52af4092f863 For optimization scripts, correctly choose tolerance (eps) based on class of fun and X0.
Rik <rik@octave.org>
parents: 20852
diff changeset
142
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
143 ## Only for display purposes.
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
144 iter(1).funccount = nfev;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
145 iter(1).x = x;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
146 iter(1).fx = fval;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
147
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
148 while (niter < maxiter && nfev < maxfev)
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
149 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
150 ## 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
151 ## 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
152 ## 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
153 tol = 2 * sqrteps * abs (x) + tolx / 3;
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
154 if (abs (x - xm) <= (2*tol - 0.5*(b-a)))
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
155 info = 1;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
156 break;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
157 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
158
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
159 if (abs (e) > tol)
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
160 dogs = false;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
161 ## Try inverse parabolic step.
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
162 iter(niter+1).procedure = "parabolic";
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
163
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
164 r = (x - w)*(fval - fv);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
165 q = (x - v)*(fval - fw);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
166 p = (x - v)*q - (x - w)*r;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
167 q = 2*(q - r);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
168 p *= -sign (q);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
169 q = abs (q);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
170 r = e;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
171 e = d;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
172
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
173 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
174 ## The parabolic step is acceptable.
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
175 d = p / q;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
176 u = x + d;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
177
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
178 ## 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
179 if (min (u-a, b-u) < 2*tol)
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
180 d = tol * (sign (xm - x) + (xm == x));
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
181 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
182 else
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
183 dogs = true;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
184 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
185 else
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
186 dogs = true;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
187 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
188 if (dogs)
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
189 ## Default to golden section step.
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
190
25740
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
191 ## 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
192 ## 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
193 iter(niter+1).procedure = "golden";
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
194
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
195 e = ifelse (x >= xm, a - x, b - x);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
196 d = c * e;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
197 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
198
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
199 ## 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
200 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
201 fu = fun (u);
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
202
20735
418ae0cb752f Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents: 20727
diff changeset
203 niter += 1;
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
204
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
205 iter(niter).funccount = nfev++;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
206 iter(niter).x = u;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
207 iter(niter).fx = fu;
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
208
21562
6c2fd62db1f7 maint: Eliminate accidental double spaces in code.
Rik <rik@octave.org>
parents: 21099
diff changeset
209 ## update a, b, v, w, and x
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
210
18290
61dab64aa5a6 fminbnd.m: Correctly handle certain functions with odd discontinuities (bug #36923)
Rik <rik@octave.org>
parents: 17744
diff changeset
211 if (fu < fval)
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
212 if (u < x)
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
213 b = x;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
214 else
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
215 a = x;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
216 endif
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
217 v = w; fv = fw;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
218 w = x; fw = fval;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
219 x = u; fval = fu;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
220 else
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
221 ## 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
222 if (u < x)
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
223 a = u;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
224 else
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
225 b = u;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
226 endif
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
227 if (fu <= fw || w == x)
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
228 v = w; fv = fw;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
229 w = u; fw = fu;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
230 elseif (fu <= fv || v == x || v == w)
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
231 v = u;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
232 fv = fu;
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
233 endif
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
234 endif
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
235
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
236 ## If there's an output function, use it now.
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
237 if (outfcn)
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
238 optv.funccount = nfev;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
239 optv.fval = fval;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
240 optv.iteration = niter;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
241 if (outfcn (x, optv, "iter"))
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
242 info = -1;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
243 break;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
244 endif
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 endwhile
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
247
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
248 ## Fix the first step procedure.
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
249 iter(1).procedure = "initial";
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
250
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
251 ## Handle the "Display" option
16933
e39f00a32dc7 maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents: 16772
diff changeset
252 switch (displ)
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
253 case "iter"
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
254 print_formatted_table (iter);
25740
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
255 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
256 case "notify"
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
257 if (info == 0)
25740
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
258 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
259 endif
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
260 case "final"
25740
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
261 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
262 case "off"
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
263 "skip";
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
264 otherwise
20727
a5949b3d2332 Preface warning() messages with name of function when possible.
Rik <rik@octave.org>
parents: 20726
diff changeset
265 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
266 endswitch
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
267
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
268 output.iterations = niter;
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
269 output.funcCount = nfev;
25740
b1e0e58971f9 fminbnd.m: Update solver options to be Matlab compatible.
Rik <rik@octave.org>
parents: 25054
diff changeset
270 output.algorithm = "golden section search, parabolic interpolation";
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
271 output.bracket = [a, b];
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
272 ## FIXME: bracketf possibly unavailable.
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
273
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
274 endfunction
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
275
21759
b002b4331a12 maint: Use two newlines after endfunction and start of BIST tests.
Rik <rik@octave.org>
parents: 21751
diff changeset
276 ## A helper function that evaluates a function and checks for bad results.
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
277 function fx = guarded_eval (fun, x)
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
278 fx = fun (x);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
279 fx = fx(1);
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
280 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
281 error ("Octave:fmindbnd:notreal", "fminbnd: non-real value encountered");
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
282 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
283 error ("Octave:fmindbnd:isnan", "fminbnd: NaN value encountered");
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
284 endif
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
285 endfunction
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
286
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
287 ## A hack for printing a formatted table
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
288 function print_formatted_table (table)
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
289 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
290 for row=table
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
291 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
292 int2str (row.funccount), num2str (row.x,"%.5f"),
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
293 num2str (row.fx,"%.6f"), row.procedure);
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
294 endfor
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
295 printf ("\n");
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
296 endfunction
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
297
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
298 ## 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
299 function print_exit_msg (info, opt=struct())
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
300 printf ("");
16933
e39f00a32dc7 maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents: 16772
diff changeset
301 switch (info)
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
302 case 1
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
303 printf ("Optimization terminated:\n");
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
304 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
305 case 0
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
306 printf ("Exiting: Maximum number of iterations has been exceeded\n");
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
307 printf (" - increase MaxIter option.\n");
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
308 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
309 case -1
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
310 "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
311 otherwise
20726
25d676f9619c Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents: 20165
diff changeset
312 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
313 endswitch
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
314 printf ("\n");
15706
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
315 endfunction
242e9efd4315 Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents: 14895
diff changeset
316
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
317
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
318 %!shared opt0
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
319 %! 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
320 %!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
321 %!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
322 %!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
323 %!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
324 %!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
325 %!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
326
19443
0f79fa9b3a8c fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18290
diff changeset
327 %!error <lower bound cannot be greater> fminbnd (@(x) sin (x), 0, -pi)