Mercurial > hg > octave-nkf
annotate scripts/optimization/fzero.m @ 12632:2dbac27e0e40 stable
doc: miscellaneous touch-ups to documentation strings
* debug.txi: Correct line number for debug example.
* func.txi: Correct directory structure of functions shipped with Octave distribution.
* edit.m: Eliminate reference to editor used by bug_report.
* fzero.m: Add inline function to list of possible inputs.
* fplot.m: Add comma to break list of possible inputs.
* qqplot.m: Correct example so that it runs under current Octave distributions.
* assert.m: Add seealso reference to fail.
* fail.m: Add seealso reference to assert. Add additional calling forms of function.
* load-path.cc (addpath): Don't use @samp which created double double quotes around option.
* ov-fcn-inline.cc (vectorize): Add example usage.
* variables.cc (exist): Add seealso link to file_in_loadpath.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 25 Apr 2011 09:24:33 -0700 |
parents | c792872f8942 |
children | b9a89ca0fb75 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2008-2011 VZLU Prague, a.s. |
8305 | 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 | |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
21 ## -*- texinfo -*- |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
22 ## @deftypefn {Function File} {} fzero (@var{fun}, @var{x0}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
23 ## @deftypefnx {Function File} {} fzero (@var{fun}, @var{x0}, @var{options}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
24 ## @deftypefnx {Function File} {[@var{x}, @var{fval}, @var{info}, @var{output}] =} fzero (@dots{}) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
25 ## Find a zero of a univariate function. |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
26 ## |
12632
2dbac27e0e40
doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
27 ## @var{fun} is a function handle, inline function, or string |
2dbac27e0e40
doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
28 ## containing the name of the function to evaluate. |
2dbac27e0e40
doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
29 ## @var{x0} should be a two-element vector specifying two points which |
2dbac27e0e40
doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
30 ## bracket a zero. In other words, there must be a change in sign of the |
2dbac27e0e40
doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
31 ## function between @var{x0}(1) and @var{x0}(2). More mathematically, the |
2dbac27e0e40
doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
32 ## following must hold |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
33 ## |
10470 | 34 ## @example |
35 ## sign (@var{fun}(@var{x0}(1))) * sign (@var{fun}(@var{x0}(2))) <= 0 | |
36 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
37 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
38 ## If @var{x0} is a single scalar then several nearby and distant |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
39 ## values are probed in an attempt to obtain a valid bracketing. If this |
10470 | 40 ## is not successful, the function fails. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
41 ## @var{options} is a structure specifying additional options. |
10470 | 42 ## Currently, @code{fzero} |
8515
ec2715c76039
fzero.m, fsolve.m: additional doc fixes
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
43 ## recognizes these options: @code{"FunValCheck"}, @code{"OutputFcn"}, |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
44 ## @code{"TolX"}, @code{"MaxIter"}, @code{"MaxFunEvals"}. |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
45 ## For a description of these options, see @ref{doc-optimset,,optimset}. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
46 ## |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
47 ## On exit, the function returns @var{x}, the approximate zero point |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
48 ## and @var{fval}, the function value thereof. |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
49 ## @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
|
50 ## |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
51 ## @itemize |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
52 ## @item 1 |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
53 ## The algorithm converged to a solution. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
54 ## |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
55 ## @item 0 |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
56 ## Maximum number of iterations or function evaluations has been reached. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
57 ## |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
58 ## @item -1 |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
59 ## The algorithm has been terminated from user output function. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
60 ## |
10337
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
61 ## @item -5 |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
62 ## The algorithm may have converged to a singular point. |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
63 ## @end itemize |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
64 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
65 ## @var{output} is a structure containing runtime information about the |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
66 ## @code{fzero} algorithm. Fields in the structure are: |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
67 ## |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
68 ## @itemize |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
69 ## @item iterations |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
70 ## Number of iterations through loop. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
71 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
72 ## @item nfev |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
73 ## Number of function evaluations. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
74 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
75 ## @item bracketx |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
76 ## A two-element vector with the final bracketing of the zero along the x-axis. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
77 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
78 ## @item brackety |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
79 ## A two-element vector with the final bracketing of the zero along the y-axis. |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
80 ## @end itemize |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
81 ## @seealso{optimset, fsolve} |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
82 ## @end deftypefn |
8305 | 83 |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
84 ## This is essentially the ACM algorithm 748: Enclosing Zeros of |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
85 ## Continuous Functions due to Alefeld, Potra and Shi, ACM Transactions |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
86 ## on Mathematical Software, Vol. 21, No. 3, September 1995. Although |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
87 ## the workflow should be the same, the structure of the algorithm has |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
88 ## been transformed non-trivially; instead of the authors' approach of |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
89 ## sequentially calling building blocks subprograms we implement here a |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
90 ## FSM version using one interior point determination and one bracketing |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
91 ## per iteration, thus reducing the number of temporary variables and |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
92 ## simplifying the algorithm structure. Further, this approach reduces |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
93 ## the need for external functions and error handling. The algorithm has |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
94 ## also been slightly modified. |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
95 |
8648
ff61b53eb294
optimization: use PKG_ADD: comments instead of PKG_ADD file
John W. Eaton <jwe@octave.org>
parents:
8596
diff
changeset
|
96 ## PKG_ADD: __all_opts__ ("fzero"); |
ff61b53eb294
optimization: use PKG_ADD: comments instead of PKG_ADD file
John W. Eaton <jwe@octave.org>
parents:
8596
diff
changeset
|
97 |
8305 | 98 function [x, fval, info, output] = fzero (fun, x0, options = struct ()) |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
99 |
8596
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
100 ## Get default options if requested. |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
101 if (nargin == 1 && ischar (fun) && strcmp (fun, 'defaults')) |
10296 | 102 x = optimset ("MaxIter", Inf, "MaxFunEvals", Inf, "TolX", 1e-8, \ |
8596
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
103 "OutputFcn", [], "FunValCheck", "off"); |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
104 return; |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
105 endif |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
106 |
8305 | 107 if (nargin < 2 || nargin > 3) |
108 print_usage (); | |
109 endif | |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
110 |
8305 | 111 if (ischar (fun)) |
9464
e598248a060d
safer str2func use in optim functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9143
diff
changeset
|
112 fun = str2func (fun, "global"); |
8305 | 113 endif |
114 | |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
115 ## TODO |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
116 ## displev = optimget (options, "Display", "notify"); |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
117 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on"); |
8305 | 118 outfcn = optimget (options, "OutputFcn"); |
10296 | 119 tolx = optimget (options, "TolX", 1e-8); |
8305 | 120 maxiter = optimget (options, "MaxIter", Inf); |
121 maxfev = optimget (options, "MaxFunEvals", Inf); | |
122 | |
123 persistent mu = 0.5; | |
124 | |
125 if (funvalchk) | |
8507 | 126 ## Replace fun with a guarded version. |
8305 | 127 fun = @(x) guarded_eval (fun, x); |
128 endif | |
129 | |
8507 | 130 ## The default exit flag if exceeded number of iterations. |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
131 info = 0; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
132 niter = 0; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
133 nfev = 0; |
8305 | 134 |
135 x = fval = a = fa = b = fb = NaN; | |
10296 | 136 eps = eps (class (x0)); |
8305 | 137 |
8507 | 138 ## Prepare... |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
139 a = x0(1); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
140 fa = fun (a); |
8305 | 141 nfev = 1; |
142 if (length (x0) > 1) | |
143 b = x0(2); | |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
144 fb = fun (b); |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
145 nfev += 1; |
8305 | 146 else |
8507 | 147 ## Try to get b. |
8305 | 148 if (a == 0) |
149 aa = 1; | |
150 else | |
151 aa = a; | |
152 endif | |
153 for b = [0.9*aa, 1.1*aa, aa-1, aa+1, 0.5*aa 1.5*aa, -aa, 2*aa, -10*aa, 10*aa] | |
154 fb = fun (b); nfev += 1; | |
155 if (sign (fa) * sign (fb) <= 0) | |
156 break; | |
157 endif | |
158 endfor | |
159 endif | |
160 | |
161 if (b < a) | |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
162 u = a; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
163 a = b; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
164 b = u; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
165 |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
166 fu = fa; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
167 fa = fb; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
168 fb = fu; |
8305 | 169 endif |
170 | |
171 if (! (sign (fa) * sign (fb) <= 0)) | |
8468
866492035ecf
fsolve.m, fzero.m: undo part of previous change
John W. Eaton <jwe@octave.org>
parents:
8467
diff
changeset
|
172 error ("fzero:bracket", "fzero: not a valid initial bracketing"); |
8305 | 173 endif |
174 | |
10337
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
175 slope0 = (fb - fa) / (b - a); |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
176 |
10470 | 177 if (fa == 0) |
178 b = a; | |
179 fb = fa; | |
180 elseif (fb == 0) | |
181 a = b; | |
182 fa = fb; | |
183 endif | |
184 | |
8305 | 185 itype = 1; |
186 | |
187 if (abs (fa) < abs (fb)) | |
188 u = a; fu = fa; | |
189 else | |
190 u = b; fu = fb; | |
191 endif | |
192 | |
193 d = e = u; | |
194 fd = fe = fu; | |
195 mba = mu*(b - a); | |
196 while (niter < maxiter && nfev < maxfev) | |
197 switch (itype) | |
198 case 1 | |
8507 | 199 ## The initial test. |
8305 | 200 if (b - a <= 2*(2 * abs (u) * eps + tolx)) |
201 x = u; fval = fu; | |
202 info = 1; | |
203 break; | |
204 endif | |
205 if (abs (fa) <= 1e3*abs (fb) && abs (fb) <= 1e3*abs (fa)) | |
8507 | 206 ## Secant step. |
8305 | 207 c = u - (a - b) / (fa - fb) * fu; |
208 else | |
8507 | 209 ## Bisection step. |
8305 | 210 c = 0.5*(a + b); |
211 endif | |
212 d = u; fd = fu; | |
213 itype = 5; | |
214 case {2, 3} | |
215 l = length (unique ([fa, fb, fd, fe])); | |
216 if (l == 4) | |
8507 | 217 ## Inverse cubic interpolation. |
8305 | 218 q11 = (d - e) * fd / (fe - fd); |
219 q21 = (b - d) * fb / (fd - fb); | |
220 q31 = (a - b) * fa / (fb - fa); | |
221 d21 = (b - d) * fd / (fd - fb); | |
222 d31 = (a - b) * fb / (fb - fa); | |
223 q22 = (d21 - q11) * fb / (fe - fb); | |
224 q32 = (d31 - q21) * fa / (fd - fa); | |
225 d32 = (d31 - q21) * fd / (fd - fa); | |
226 q33 = (d32 - q22) * fa / (fe - fa); | |
227 c = a + q31 + q32 + q33; | |
228 endif | |
229 if (l < 4 || sign (c - a) * sign (c - b) > 0) | |
8507 | 230 ## Quadratic interpolation + newton. |
8305 | 231 a0 = fa; |
232 a1 = (fb - fa)/(b - a); | |
233 a2 = ((fd - fb)/(d - b) - a1) / (d - a); | |
8507 | 234 ## Modification 1: this is simpler and does not seem to be worse. |
8305 | 235 c = a - a0/a1; |
236 if (a2 != 0) | |
237 c = a - a0/a1; | |
238 for i = 1:itype | |
239 pc = a0 + (a1 + a2*(c - b))*(c - a); | |
240 pdc = a1 + a2*(2*c - a - b); | |
241 if (pdc == 0) | |
242 c = a - a0/a1; | |
243 break; | |
244 endif | |
245 c -= pc/pdc; | |
246 endfor | |
247 endif | |
248 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
249 itype += 1; |
8305 | 250 case 4 |
8507 | 251 ## Double secant step. |
8305 | 252 c = u - 2*(b - a)/(fb - fa)*fu; |
8507 | 253 ## Bisect if too far. |
8305 | 254 if (abs (c - u) > 0.5*(b - a)) |
255 c = 0.5 * (b + a); | |
256 endif | |
257 itype = 5; | |
258 case 5 | |
8507 | 259 ## Bisection step. |
8305 | 260 c = 0.5 * (b + a); |
261 itype = 2; | |
262 endswitch | |
263 | |
8507 | 264 ## Don't let c come too close to a or b. |
8305 | 265 delta = 2*0.7*(2 * abs (u) * eps + tolx); |
266 if ((b - a) <= 2*delta) | |
267 c = (a + b)/2; | |
268 else | |
269 c = max (a + delta, min (b - delta, c)); | |
270 endif | |
271 | |
8507 | 272 ## Calculate new point. |
8305 | 273 x = c; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
274 fval = fc = fun (c); |
8305 | 275 niter ++; nfev ++; |
276 | |
8507 | 277 ## Modification 2: skip inverse cubic interpolation if |
278 ## nonmonotonicity is detected. | |
8305 | 279 if (sign (fc - fa) * sign (fc - fb) >= 0) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
280 ## The new point broke monotonicity. |
8507 | 281 ## Disable inverse cubic. |
8305 | 282 fe = fc; |
283 else | |
284 e = d; fe = fd; | |
285 endif | |
286 | |
8507 | 287 ## Bracketing. |
8305 | 288 if (sign (fa) * sign (fc) < 0) |
289 d = b; fd = fb; | |
290 b = c; fb = fc; | |
291 elseif (sign (fb) * sign (fc) < 0) | |
292 d = a; fd = fa; | |
293 a = c; fa = fc; | |
294 elseif (fc == 0) | |
295 a = b = c; fa = fb = fc; | |
296 info = 1; | |
297 break; | |
298 else | |
8507 | 299 ## This should never happen. |
8468
866492035ecf
fsolve.m, fzero.m: undo part of previous change
John W. Eaton <jwe@octave.org>
parents:
8467
diff
changeset
|
300 error ("fzero:bracket", "fzero: zero point is not bracketed"); |
8305 | 301 endif |
302 | |
8507 | 303 ## If there's an output function, use it now. |
8305 | 304 if (outfcn) |
10296 | 305 optv.funccount = nfev; |
8305 | 306 optv.fval = fval; |
307 optv.iteration = niter; | |
308 if (outfcn (x, optv, "iter")) | |
309 info = -1; | |
310 break; | |
311 endif | |
312 endif | |
313 | |
314 if (abs (fa) < abs (fb)) | |
315 u = a; fu = fa; | |
316 else | |
317 u = b; fu = fb; | |
318 endif | |
319 if (b - a <= 2*(2 * abs (u) * eps + tolx)) | |
320 info = 1; | |
321 break; | |
322 endif | |
323 | |
8507 | 324 ## Skip bisection step if successful reduction. |
8305 | 325 if (itype == 5 && (b - a) <= mba) |
326 itype = 2; | |
327 endif | |
328 if (itype == 2) | |
329 mba = mu * (b - a); | |
330 endif | |
331 endwhile | |
332 | |
10337
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
333 ## Check solution for a singularity by examining slope |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
334 if (info == 1) |
10394
bc475cd49147
avoid div by zero in fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
10337
diff
changeset
|
335 if ((b - a) != 0 && abs ((fb - fa)/(b - a) / slope0) > max (1e6, 0.5/(eps+tolx))) |
10337
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
336 info = -5; |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
337 endif |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
338 endif |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
339 |
8305 | 340 output.iterations = niter; |
10296 | 341 output.funcCount = nfev; |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
342 output.bracketx = [a, b]; |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
343 output.brackety = [fa, fb]; |
8305 | 344 |
345 endfunction | |
346 | |
8507 | 347 ## An assistant function that evaluates a function handle and checks for |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
348 ## bad results. |
8305 | 349 function fx = guarded_eval (fun, x) |
350 fx = fun (x); | |
351 fx = fx(1); | |
352 if (! isreal (fx)) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
353 error ("fzero:notreal", "fzero: non-real value encountered"); |
8305 | 354 elseif (isnan (fx)) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
355 error ("fzero:isnan", "fzero: NaN value encountered"); |
8305 | 356 endif |
357 endfunction | |
358 | |
10296 | 359 %!shared opt0 |
360 %! opt0 = optimset ("tolx", 0); | |
361 %!assert(fzero(@cos, [0, 3], opt0), pi/2, 10*eps) | |
362 %!assert(fzero(@(x) x^(1/3) - 1e-8, [0,1], opt0), 1e-24, 1e-22*eps) |