Mercurial > hg > octave-nkf
annotate scripts/optimization/fminbnd.m @ 20076:781adfc2958c draft obsolete nkf
Build qt graphics toolkit menus with uimenu
* libgui/graphics/Figure.cc, libgui/graphics/Figure.h: Revert cset 7335cc071ab0
and remove all menu items.
* scripts/gui/private/__get_funcname__.m: Fix typo
* scripts/plot/util/private/__add_default_menu__.m: Enable uimenu for qt
graphics toolkit and fix setting pan/rotate/zoom for qt and fltk
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Fri, 27 Feb 2015 18:36:05 +0100 |
parents | 9fc020886ae9 |
children | f1d0f506ee78 |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19794
diff
changeset
|
1 ## Copyright (C) 2008-2015 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: |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
28 ## @qcode{"FunValCheck"}, @qcode{"OutputFcn"}, @qcode{"TolX"}, |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
29 ## @qcode{"MaxIter"}, @qcode{"MaxFunEvals"}. For a description of these |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
30 ## 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
|
31 ## |
10296 | 32 ## On exit, the function returns @var{x}, the approximate minimum point |
33 ## and @var{fval}, the function value thereof. | |
34 ## @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
|
35 ## |
10296 | 36 ## @itemize |
37 ## @item 1 | |
38 ## The algorithm converged to a solution. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
39 ## |
10296 | 40 ## @item 0 |
41 ## 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
|
42 ## |
10296 | 43 ## @item -1 |
44 ## The algorithm has been terminated from user output function. | |
45 ## @end itemize | |
14895
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
46 ## |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
47 ## 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
|
48 ## 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
|
49 ## 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
|
50 ## minimization algorithm such as @code{fminunc} or @code{fminsearch}. |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
51 ## @code{fminbnd} internally uses a Golden Section search strategy. |
e0525ecf156e
Add new function fminsearch.m
Andy Adler <andy@analyti.ca>
parents:
14868
diff
changeset
|
52 ## @seealso{fzero, fminunc, fminsearch, optimset} |
10296 | 53 ## @end deftypefn |
54 | |
55 ## This is patterned after opt/fmin.f from Netlib, which in turn is taken from | |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
56 ## Richard Brent: Algorithms For Minimization Without Derivatives, |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
57 ## Prentice-Hall (1973) |
10296 | 58 |
13027
b9a89ca0fb75
prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
59 ## 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
|
60 ## PKG_ADD: [~] = __all_opts__ ("fminbnd"); |
10296 | 61 |
62 function [x, fval, info, output] = fminbnd (fun, xmin, xmax, options = struct ()) | |
63 | |
64 ## Get default options if requested. | |
65 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
|
66 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
|
67 "OutputFcn", [], "FunValCheck", "off"); |
10296 | 68 return; |
69 endif | |
70 | |
71 if (nargin < 2 || nargin > 4) | |
72 print_usage (); | |
73 endif | |
19794
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19640
diff
changeset
|
74 |
19640
0f79fa9b3a8c
fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18315
diff
changeset
|
75 if (xmin > xmax) |
0f79fa9b3a8c
fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18315
diff
changeset
|
76 error ("Octave:invalid-input-arg", |
0f79fa9b3a8c
fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18315
diff
changeset
|
77 "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:
18315
diff
changeset
|
78 endif |
10296 | 79 |
80 if (ischar (fun)) | |
81 fun = str2func (fun, "global"); | |
82 endif | |
83 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
84 displ = optimget (options, "Display", "notify"); |
10296 | 85 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on"); |
86 outfcn = optimget (options, "OutputFcn"); | |
87 tolx = optimget (options, "TolX", 1e-8); | |
88 maxiter = optimget (options, "MaxIter", Inf); | |
89 maxfev = optimget (options, "MaxFunEvals", Inf); | |
90 | |
91 if (funvalchk) | |
92 ## Replace fun with a guarded version. | |
93 fun = @(x) guarded_eval (fun, x); | |
94 endif | |
95 | |
96 ## The default exit flag if exceeded number of iterations. | |
97 info = 0; | |
98 niter = 0; | |
99 nfev = 0; | |
10392
b4e5dcf023c9
fix fminbnd termination tolerances
Jaroslav Hajek <highegg@gmail.com>
parents:
10297
diff
changeset
|
100 sqrteps = eps (class (xmin + xmax)); |
10296 | 101 |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
102 c = 0.5*(3 - sqrt (5)); |
10296 | 103 a = xmin; b = xmax; |
104 v = a + c*(b-a); | |
105 w = x = v; | |
106 e = 0; | |
107 fv = fw = fval = fun (x); | |
108 nfev++; | |
109 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
110 ## Only for display purposes. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
111 iter(1).funccount = nfev; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
112 iter(1).x = x; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
113 iter(1).fx = fval; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
114 |
10296 | 115 while (niter < maxiter && nfev < maxfev) |
116 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
|
117 ## 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
|
118 ## 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
|
119 ## 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
|
120 tol = 2 * sqrteps * abs (x) + tolx / 3; |
10296 | 121 if (abs (x - xm) <= (2*tol - 0.5*(b-a))) |
122 info = 1; | |
123 break; | |
124 endif | |
125 | |
126 if (abs (e) > tol) | |
127 dogs = false; | |
128 ## Try inverse parabolic step. | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
129 iter(niter+1).procedure = "parabolic"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
130 |
10296 | 131 r = (x - w)*(fval - fv); |
132 q = (x - v)*(fval - fw); | |
133 p = (x - v)*q - (x - w)*r; | |
134 q = 2*(q - r); | |
135 p *= -sign (q); | |
136 q = abs (q); | |
137 r = e; | |
138 e = d; | |
139 | |
140 if (abs (p) < abs (0.5*q*r) && p > q*(a-x) && p < q*(b-x)) | |
141 ## The parabolic step is acceptable. | |
142 d = p / q; | |
143 u = x + d; | |
144 | |
145 ## 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
|
146 if (min (u-a, b-u) < 2*tol) |
10296 | 147 d = tol * (sign (xm - x) + (xm == x)); |
148 endif | |
149 else | |
150 dogs = true; | |
151 endif | |
152 else | |
153 dogs = true; | |
154 endif | |
155 if (dogs) | |
156 ## Default to golden section step. | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
157 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
158 ## 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
|
159 ## 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
|
160 ## for the first step. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
161 iter(niter+1).procedure = "golden"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
162 |
10296 | 163 e = ifelse (x >= xm, a - x, b - x); |
164 d = c * e; | |
165 endif | |
166 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
167 ## 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
|
168 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
|
169 fu = fun (u); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
170 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
171 niter++; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
172 |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
173 iter(niter).funccount = nfev++; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
174 iter(niter).x = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
175 iter(niter).fx = fu; |
10296 | 176 |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
177 ## update a, b, v, w, and x |
10296 | 178 |
18315
61dab64aa5a6
fminbnd.m: Correctly handle certain functions with odd discontinuities (bug #36923)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
179 if (fu < fval) |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
180 if (u < x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
181 b = x; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
182 else |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
183 a = x; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
184 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
185 v = w; fv = fw; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
186 w = x; fw = fval; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
187 x = u; fval = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
188 else |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
189 ## 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
|
190 if (u < x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
191 a = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
192 else |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
193 b = u; |
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 if (fu <= fw || w == x) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
196 v = w; fv = fw; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
197 w = u; fw = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
198 elseif (fu <= fv || v == x || v == w) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
199 v = u; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
200 fv = fu; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
201 endif |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
202 endif |
10296 | 203 |
204 ## If there's an output function, use it now. | |
205 if (outfcn) | |
206 optv.funccount = nfev; | |
207 optv.fval = fval; | |
208 optv.iteration = niter; | |
209 if (outfcn (x, optv, "iter")) | |
210 info = -1; | |
211 break; | |
212 endif | |
213 endif | |
214 endwhile | |
215 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
216 ## Fix the first step procedure. |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
217 iter(1).procedure = "initial"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
218 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
219 ## Handle the "Display" option |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16772
diff
changeset
|
220 switch (displ) |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
221 case "iter" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
222 print_formatted_table (iter); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
223 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
|
224 case "notify" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
225 if (info == 0) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
226 print_exit_msg (info, struct("fx",fval)); |
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 case "final" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
229 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
|
230 case "off" |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
231 "skip"; |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
232 otherwise |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
233 warning ("unknown option for Display: '%s'", displ); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
234 endswitch |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
235 |
10296 | 236 output.iterations = niter; |
237 output.funcCount = nfev; | |
238 output.bracket = [a, b]; | |
239 ## FIXME: bracketf possibly unavailable. | |
240 | |
241 endfunction | |
242 | |
243 ## An assistant function that evaluates a function handle and checks for | |
244 ## bad results. | |
245 function fx = guarded_eval (fun, x) | |
246 fx = fun (x); | |
247 fx = fx(1); | |
248 if (! isreal (fx)) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
249 error ("fminbnd:notreal", "fminbnd: non-real value encountered"); |
10296 | 250 elseif (isnan (fx)) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
251 error ("fminbnd:isnan", "fminbnd: NaN value encountered"); |
10296 | 252 endif |
253 endfunction | |
254 | |
15706
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
255 ## A hack for printing a formatted table |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
256 function print_formatted_table (table) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
257 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
|
258 for row=table |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
259 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
|
260 int2str (row.funccount), num2str (row.x,"%.5f"), |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
261 num2str (row.fx,"%.6f"), row.procedure); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
262 endfor |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
263 printf ("\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
264 endfunction |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
265 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
266 ## 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
|
267 function print_exit_msg (info, opt=struct()) |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
268 printf (""); |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16772
diff
changeset
|
269 switch (info) |
15706
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 printf ("Optimization terminated:\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
272 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
|
273 case 0 |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
274 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
|
275 printf (" - increase MaxIter option.\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
276 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
|
277 case -1 |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
278 "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
|
279 otherwise |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
280 error ("internal error - fminbnd() is bug, sorry!"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
281 endswitch |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
282 printf ("\n"); |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
283 endfunction |
242e9efd4315
Added Display option for fminbnd()
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
14895
diff
changeset
|
284 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
285 |
10296 | 286 %!shared opt0 |
287 %! 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
|
288 %!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
|
289 %!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
|
290 %!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
|
291 %!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)) |
19794
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19640
diff
changeset
|
292 %!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:
19640
diff
changeset
|
293 %!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
|
294 |
19640
0f79fa9b3a8c
fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18315
diff
changeset
|
295 %!error <lower bound cannot be greater> fminbnd (@(x) sin (x), 0, -pi) |
0f79fa9b3a8c
fmindbnd.m: Check input range is low to high (bug #43219).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18315
diff
changeset
|
296 |