Mercurial > hg > octave-lyh
annotate scripts/optimization/fminbnd.m @ 17101:e7a059a9a644
doc: Use XREF as anchor prefix in documentation for clearer results in Info viewer.
Correctly use @xref, @pxref, @ref macros in documentation.
* doc/interpreter/arith.txi, doc/interpreter/basics.txi,
doc/interpreter/container.txi, doc/interpreter/debug.txi,
doc/interpreter/diagperm.txi, doc/interpreter/eval.txi,
doc/interpreter/external.txi, doc/interpreter/func.txi,
doc/interpreter/install.txi, doc/interpreter/interp.txi,
doc/interpreter/intro.txi, doc/interpreter/java.txi,
doc/interpreter/matrix.txi, doc/interpreter/munge-texi.pl,
doc/interpreter/numbers.txi, doc/interpreter/package.txi,
doc/interpreter/plot.txi, doc/interpreter/sparse.txi,
doc/interpreter/stats.txi, doc/interpreter/strings.txi,
doc/interpreter/system.txi, doc/interpreter/var.txi,
doc/interpreter/vectorize.txi, libinterp/corefcn/error.cc,
libinterp/corefcn/qz.cc, libinterp/corefcn/regexp.cc,
scripts/general/accumarray.m, scripts/general/structfun.m,
scripts/miscellaneous/edit.m, scripts/miscellaneous/setfield.m,
scripts/miscellaneous/warning_ids.m, scripts/optimization/fminbnd.m,
scripts/optimization/fzero.m, scripts/optimization/lsqnonneg.m:
Use XREF as anchor prefix in documentation for clearer results in Info viewer.
Correctly use @xref, @pxref, @ref macros in documentation.
* scripts/plot/line.m: Remove extra ')' in @deftypefn.
* libgui/src/qtinfo/parser.cc(find_ref): Change doc anchor search pattern to
XREF.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 27 Jul 2013 10:49:21 -0700 |
parents | e39f00a32dc7 |
children | bc924baa2c4e |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13027
diff
changeset
|
1 ## Copyright (C) 2008-2012 VZLU Prague, a.s. |
10296 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 ## | |
19 ## Author: Jaroslav Hajek <highegg@gmail.com> | |
20 | |
21 ## -*- texinfo -*- | |
22 ## @deftypefn {Function File} {[@var{x}, @var{fval}, @var{info}, @var{output}] =} fminbnd (@var{fun}, @var{a}, @var{b}, @var{options}) | |
14895
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
23 ## Find a minimum point of a univariate function. |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
24 ## |
14895
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
25 ## @var{fun} should be a function handle or name. @var{a}, @var{b} specify a |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
26 ## starting interval. @var{options} is a structure specifying additional |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
27 ## options. Currently, @code{fminbnd} recognizes these options: |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
28 ## "FunValCheck", "OutputFcn", "TolX", "MaxIter", "MaxFunEvals". For a |
17101
e7a059a9a644
doc: Use XREF as anchor prefix in documentation for clearer results in Info viewer.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
29 ## description of these options, see @ref{XREFoptimset,,optimset}. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
30 ## |
10296 | 31 ## On exit, the function returns @var{x}, the approximate minimum point |
32 ## and @var{fval}, the function value thereof. | |
33 ## @var{info} is an exit flag that can have these values: | |
10297
ed88ea036716
improve docs of fzero/fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
10296
diff
changeset
|
34 ## |
10296 | 35 ## @itemize |
36 ## @item 1 | |
37 ## The algorithm converged to a solution. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
38 ## |
10296 | 39 ## @item 0 |
40 ## Maximum number of iterations or function evaluations has been exhausted. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
41 ## |
10296 | 42 ## @item -1 |
43 ## The algorithm has been terminated from user output function. | |
44 ## @end itemize | |
14895
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
45 ## |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
46 ## Notes: The search for a minimum is restricted to be in the interval |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
47 ## bound by @var{a} and @var{b}. If you only have an initial point |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
48 ## to begin searching from you will need to use an unconstrained |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
49 ## minimization algorithm such as @code{fminunc} or @code{fminsearch}. |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
50 ## @code{fminbnd} internally uses a Golden Section search strategy. |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
51 ## @seealso{fzero, fminunc, fminsearch, optimset} |
10296 | 52 ## @end deftypefn |
53 | |
54 ## This is patterned after opt/fmin.f from Netlib, which in turn is taken from | |
55 ## Richard Brent: Algorithms For Minimization Without Derivatives, Prentice-Hall (1973) | |
56 | |
13027
b9a89ca0fb75
prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
57 ## 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
|
58 ## PKG_ADD: [~] = __all_opts__ ("fminbnd"); |
10296 | 59 |
60 function [x, fval, info, output] = fminbnd (fun, xmin, xmax, options = struct ()) | |
61 | |
62 ## Get default options if requested. | |
63 if (nargin == 1 && ischar (fun) && strcmp (fun, 'defaults')) | |
14552
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
64 x = optimset ("MaxIter", Inf, "MaxFunEvals", Inf, "TolX", 1e-8, |
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
65 "OutputFcn", [], "FunValCheck", "off"); |
10296 | 66 return; |
67 endif | |
68 | |
69 if (nargin < 2 || nargin > 4) | |
70 print_usage (); | |
71 endif | |
72 | |
73 if (ischar (fun)) | |
74 fun = str2func (fun, "global"); | |
75 endif | |
76 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
77 displ = optimget (options, "Display", "notify"); |
10296 | 78 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on"); |
79 outfcn = optimget (options, "OutputFcn"); | |
80 tolx = optimget (options, "TolX", 1e-8); | |
81 maxiter = optimget (options, "MaxIter", Inf); | |
82 maxfev = optimget (options, "MaxFunEvals", Inf); | |
83 | |
84 if (funvalchk) | |
85 ## Replace fun with a guarded version. | |
86 fun = @(x) guarded_eval (fun, x); | |
87 endif | |
88 | |
89 ## The default exit flag if exceeded number of iterations. | |
90 info = 0; | |
91 niter = 0; | |
92 nfev = 0; | |
10392
b4e5dcf023c9
fix fminbnd termination tolerances
Jaroslav Hajek <highegg@gmail.com>
parents:
10297
diff
changeset
|
93 sqrteps = eps (class (xmin + xmax)); |
10296 | 94 |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
95 c = 0.5*(3 - sqrt (5)); |
10296 | 96 a = xmin; b = xmax; |
97 v = a + c*(b-a); | |
98 w = x = v; | |
99 e = 0; | |
100 fv = fw = fval = fun (x); | |
101 nfev++; | |
102 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
103 ## Only for display purposes. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
104 iter(1).funccount = nfev; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
105 iter(1).x = x; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
106 iter(1).fx = fval; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
107 |
10296 | 108 while (niter < maxiter && nfev < maxfev) |
109 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
|
110 ## 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
|
111 ## sometimes. Sometimes not, it depends on the function. This is the |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
112 ## strategy from the Netlib code. Something yet smarter would be good. |
10392
b4e5dcf023c9
fix fminbnd termination tolerances
Jaroslav Hajek <highegg@gmail.com>
parents:
10297
diff
changeset
|
113 tol = 2 * sqrteps * abs (x) + tolx / 3; |
10296 | 114 if (abs (x - xm) <= (2*tol - 0.5*(b-a))) |
115 info = 1; | |
116 break; | |
117 endif | |
118 | |
119 if (abs (e) > tol) | |
120 dogs = false; | |
121 ## Try inverse parabolic step. | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
122 iter(niter+1).procedure = "parabolic"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
123 |
10296 | 124 r = (x - w)*(fval - fv); |
125 q = (x - v)*(fval - fw); | |
126 p = (x - v)*q - (x - w)*r; | |
127 q = 2*(q - r); | |
128 p *= -sign (q); | |
129 q = abs (q); | |
130 r = e; | |
131 e = d; | |
132 | |
133 if (abs (p) < abs (0.5*q*r) && p > q*(a-x) && p < q*(b-x)) | |
134 ## The parabolic step is acceptable. | |
135 d = p / q; | |
136 u = x + d; | |
137 | |
138 ## 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
|
139 if (min (u-a, b-u) < 2*tol) |
10296 | 140 d = tol * (sign (xm - x) + (xm == x)); |
141 endif | |
142 else | |
143 dogs = true; | |
144 endif | |
145 else | |
146 dogs = true; | |
147 endif | |
148 if (dogs) | |
149 ## Default to golden section step. | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
150 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
151 ## WARNING: This is also the "initial" procedure following |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
152 ## MATLAB nomenclature. After the loop we'll fix the string |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
153 ## for the first step. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
154 iter(niter+1).procedure = "golden"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
155 |
10296 | 156 e = ifelse (x >= xm, a - x, b - x); |
157 d = c * e; | |
158 endif | |
159 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
160 ## 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
|
161 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
|
162 fu = fun (u); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
163 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
164 niter++; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
165 |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
166 iter(niter).funccount = nfev++; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
167 iter(niter).x = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
168 iter(niter).fx = fu; |
10296 | 169 |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
170 ## update a, b, v, w, and x |
10296 | 171 |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
172 if (fu <= fval) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
173 if (u < x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
174 b = x; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
175 else |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
176 a = x; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
177 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
178 v = w; fv = fw; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
179 w = x; fw = fval; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
180 x = u; fval = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
181 else |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
182 ## 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
|
183 if (u < x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
184 a = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
185 else |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
186 b = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
187 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
188 if (fu <= fw || w == x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
189 v = w; fv = fw; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
190 w = u; fw = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
191 elseif (fu <= fv || v == x || v == w) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
192 v = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
193 fv = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
194 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
195 endif |
10296 | 196 |
197 ## If there's an output function, use it now. | |
198 if (outfcn) | |
199 optv.funccount = nfev; | |
200 optv.fval = fval; | |
201 optv.iteration = niter; | |
202 if (outfcn (x, optv, "iter")) | |
203 info = -1; | |
204 break; | |
205 endif | |
206 endif | |
207 endwhile | |
208 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
209 ## Fix the first step procedure. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
210 iter(1).procedure = "initial"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
211 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
212 ## Handle the "Display" option |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16772
diff
changeset
|
213 switch (displ) |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
214 case "iter" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
215 print_formatted_table (iter); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
216 print_exit_msg (info, struct("TolX", tolx, "fx", fval)); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
217 case "notify" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
218 if (info == 0) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
219 print_exit_msg (info, struct("fx",fval)); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
220 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
221 case "final" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
222 print_exit_msg (info, struct("TolX", tolx, "fx", fval)); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
223 case "off" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
224 "skip"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
225 otherwise |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
226 warning ("unknown option for Display: '%s'", displ); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
227 endswitch |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
228 |
10296 | 229 output.iterations = niter; |
230 output.funcCount = nfev; | |
231 output.bracket = [a, b]; | |
232 ## FIXME: bracketf possibly unavailable. | |
233 | |
234 endfunction | |
235 | |
236 ## An assistant function that evaluates a function handle and checks for | |
237 ## bad results. | |
238 function fx = guarded_eval (fun, x) | |
239 fx = fun (x); | |
240 fx = fx(1); | |
241 if (! isreal (fx)) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
242 error ("fminbnd:notreal", "fminbnd: non-real value encountered"); |
10296 | 243 elseif (isnan (fx)) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
244 error ("fminbnd:isnan", "fminbnd: NaN value encountered"); |
10296 | 245 endif |
246 endfunction | |
247 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
248 ## A hack for printing a formatted table |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
249 function print_formatted_table (table) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
250 printf ("\n Func-count x f(x) Procedure\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
251 for row=table |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
252 printf("%5.5s %7.7s %8.8s\t%s\n", |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
253 int2str (row.funccount), num2str (row.x,"%.5f"), |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
254 num2str (row.fx,"%.6f"), row.procedure); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
255 endfor |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
256 printf ("\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
257 endfunction |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
258 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
259 ## 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
|
260 function print_exit_msg (info, opt=struct()) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
261 printf (""); |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16772
diff
changeset
|
262 switch (info) |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
263 case 1 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
264 printf ("Optimization terminated:\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
265 printf (" the current x satisfies the termination criteria using OPTIONS.TolX of %e\n", opt.TolX); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
266 case 0 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
267 printf ("Exiting: Maximum number of iterations has been exceeded\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
268 printf (" - increase MaxIter option.\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
269 printf (" Current function value: %.6f\n", opt.fx); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
270 case -1 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
271 "FIXME"; ## FIXME: what's the message MATLAB prints for this case? |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
272 otherwise |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
273 error ("internal error - fminbnd() is bug, sorry!"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
274 endswitch |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
275 printf ("\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
276 endfunction |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
277 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
278 |
10296 | 279 %!shared opt0 |
280 %! 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
|
281 %!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
|
282 %!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
|
283 %!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
|
284 %!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)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
285 |