Mercurial > octave
annotate scripts/optimization/fzero.m @ 27919:1891570abac8
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2020.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 06 Jan 2020 22:29:51 -0500 |
parents | b442ec6dda5c |
children | bd51beb6205e |
rev | line source |
---|---|
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
1 ## 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:
27501
diff
changeset
|
2 ## |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27501
diff
changeset
|
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27501
diff
changeset
|
4 ## or <https://octave.org/COPYRIGHT.html/>. |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27501
diff
changeset
|
5 ## |
8305 | 6 ## |
7 ## This file is part of Octave. | |
8 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
9 ## Octave is free software: you can redistribute it and/or modify it |
8305 | 10 ## 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
|
11 ## 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
|
12 ## (at your option) any later version. |
8305 | 13 ## |
14 ## Octave is distributed in the hope that it will be useful, but | |
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## GNU General Public License for more details. |
8305 | 18 ## |
19 ## You should have received a copy of the GNU General Public License | |
20 ## 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
|
21 ## <https://www.gnu.org/licenses/>. |
8305 | 22 ## |
23 ## Author: Jaroslav Hajek <highegg@gmail.com> | |
24 | |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
25 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
26 ## @deftypefn {} {} fzero (@var{fun}, @var{x0}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
27 ## @deftypefnx {} {} fzero (@var{fun}, @var{x0}, @var{options}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
28 ## @deftypefnx {} {[@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
|
29 ## Find a zero of a univariate function. |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
30 ## |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
31 ## @var{fun} is a function handle, inline function, or string containing the |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
32 ## name of the function to evaluate. |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
33 ## |
12632
2dbac27e0e40
doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
34 ## @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
|
35 ## 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
|
36 ## 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
|
37 ## following must hold |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
38 ## |
10470 | 39 ## @example |
40 ## sign (@var{fun}(@var{x0}(1))) * sign (@var{fun}(@var{x0}(2))) <= 0 | |
41 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
42 ## |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
43 ## If @var{x0} is a single scalar then several nearby and distant values are |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
44 ## probed in an attempt to obtain a valid bracketing. If this is not |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
45 ## successful, the function fails. |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
46 ## |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
47 ## @var{options} is a structure specifying additional options. Currently, |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
48 ## @code{fzero} recognizes these options: |
27501
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
49 ## @qcode{"Display"}, @qcode{"FunValCheck"}, @qcode{"MaxFunEvals"}, |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
50 ## @qcode{"MaxIter"}, @qcode{"OutputFcn"}, and @qcode{"TolX"}. |
25747 | 51 ## |
52 ## @qcode{"MaxFunEvals"} proscribes the maximum number of function evaluations | |
53 ## before the search is halted. The default value is @code{Inf}. | |
54 ## The value must be a positive integer. | |
55 ## | |
56 ## @qcode{"MaxIter"} proscribes the maximum number of algorithm iterations | |
57 ## before the search is halted. The default value is @code{Inf}. | |
58 ## 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
|
59 ## |
25747 | 60 ## @qcode{"TolX"} specifies the termination tolerance for the solution @var{x}. |
61 ## The default value is @code{eps}. | |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
62 ## |
25747 | 63 ## For a description of the other options, see @ref{XREFoptimset,,optimset}. |
64 ## To initialize an options structure with default values for @code{fzero} use | |
65 ## @code{options = optimset ("fzero")}. | |
66 ## | |
67 ## On exit, the function returns @var{x}, the approximate zero point, and | |
68 ## @var{fval}, the function evaluated at @var{x}. | |
69 ## | |
70 ## The third output @var{info} reports whether the algorithm succeeded and | |
71 ## may take one of the following values: | |
10297
ed88ea036716
improve docs of fzero/fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
10296
diff
changeset
|
72 ## |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
73 ## @itemize |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
74 ## @item 1 |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
75 ## The algorithm converged to a solution. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
76 ## |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
77 ## @item 0 |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
78 ## 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
|
79 ## |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
80 ## @item -1 |
25747 | 81 ## The algorithm has been terminated by a user @code{OutputFcn}. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
82 ## |
10337
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
83 ## @item -5 |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
84 ## 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
|
85 ## @end itemize |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
86 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
87 ## @var{output} is a structure containing runtime information about the |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
88 ## @code{fzero} algorithm. Fields in the structure are: |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
89 ## |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
90 ## @itemize |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
91 ## @item iterations |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
92 ## Number of iterations through loop. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
93 ## |
26048 | 94 ## @item @nospell{funcCount} |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
95 ## Number of function evaluations. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
96 ## |
25747 | 97 ## @item algorithm |
26043
5cef57130cb9
doc: grammar check m-files in scripts/ directory.
Rik <rik@octave.org>
parents:
25747
diff
changeset
|
98 ## The string @qcode{"bisection, interpolation"}. |
25747 | 99 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
100 ## @item bracketx |
21546
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
21089
diff
changeset
|
101 ## A two-element vector with the final bracketing of the zero along the |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
21089
diff
changeset
|
102 ## x-axis. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
103 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
104 ## @item brackety |
21546
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
21089
diff
changeset
|
105 ## A two-element vector with the final bracketing of the zero along the |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
21089
diff
changeset
|
106 ## y-axis. |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
107 ## @end itemize |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
108 ## @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
|
109 ## @end deftypefn |
8305 | 110 |
25003
2365c2661b3c
doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
111 ## This is essentially the @nospell{ACM} algorithm 748: Enclosing Zeros of |
2365c2661b3c
doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
112 ## Continuous Functions due to Alefeld, Potra and Shi, @nospell{ACM} |
2365c2661b3c
doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
113 ## Transactions on Mathematical Software, Vol. 21, No. 3, September 1995. |
2365c2661b3c
doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
114 ## Although the workflow should be the same, the structure of the algorithm has |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
115 ## 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
|
116 ## 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
|
117 ## 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
|
118 ## per iteration, thus reducing the number of temporary variables and |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21578
diff
changeset
|
119 ## simplifying the algorithm structure. Further, this approach reduces |
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21578
diff
changeset
|
120 ## the need for external functions and error handling. The algorithm has |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
121 ## 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
|
122 |
13027
b9a89ca0fb75
prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents:
12632
diff
changeset
|
123 ## 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:
12632
diff
changeset
|
124 ## PKG_ADD: [~] = __all_opts__ ("fzero"); |
8648
ff61b53eb294
optimization: use PKG_ADD: comments instead of PKG_ADD file
John W. Eaton <jwe@octave.org>
parents:
8596
diff
changeset
|
125 |
8305 | 126 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
|
127 |
8596
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
128 ## Get default options if requested. |
25747 | 129 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
|
130 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
|
131 "MaxFunEvals", Inf, "MaxIter", Inf, |
804e18e3e320
Reenable query of optimization options (bugs #54952 and #55089).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
26118
diff
changeset
|
132 "OutputFcn", [], "TolX", eps); |
8596
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
133 return; |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
134 endif |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
135 |
8305 | 136 if (nargin < 2 || nargin > 3) |
137 print_usage (); | |
138 endif | |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
139 |
8305 | 140 if (ischar (fun)) |
26118
7502fce4cd3a
str2func: eliminate optional second "global" argument
John W. Eaton <jwe@octave.org>
parents:
26048
diff
changeset
|
141 fun = str2func (fun); |
8305 | 142 endif |
143 | |
27501
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
144 displev = optimget (options, "Display", "notify"); |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
145 switch (displev) |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
146 case "iter" |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
147 displev = 1; |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
148 case "final" |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
149 displev = 2; |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
150 case "notify" |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
151 displev = 3; |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
152 otherwise # "none" |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
153 displev = 0; |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
154 endswitch |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
155 |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
156 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on"); |
25747 | 157 maxfev = optimget (options, "MaxFunEvals", Inf); |
158 maxiter = optimget (options, "MaxIter", Inf); | |
8305 | 159 outfcn = optimget (options, "OutputFcn"); |
18187
6e0686858b6a
Make fzero default tolerance Matlab compatible (bug #41000)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents:
18133
diff
changeset
|
160 tolx = optimget (options, "TolX", eps); |
8305 | 161 |
25747 | 162 mu = 0.5; |
8305 | 163 |
164 if (funvalchk) | |
8507 | 165 ## Replace fun with a guarded version. |
8305 | 166 fun = @(x) guarded_eval (fun, x); |
167 endif | |
168 | |
25747 | 169 info = 0; # The default exit flag if number of iterations exceeded. |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
170 niter = 0; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
171 nfev = 0; |
8305 | 172 |
173 x = fval = a = fa = b = fb = NaN; | |
174 | |
8507 | 175 ## Prepare... |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
176 a = x0(1); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
177 fa = fun (a); |
8305 | 178 nfev = 1; |
179 if (length (x0) > 1) | |
180 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
|
181 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
|
182 nfev += 1; |
8305 | 183 else |
25158
57f150c4fe6d
fzero.m: Re-architect search for bracketing endpoint b if none is given (bug #53583).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
184 ## Try to find a value for b which brackets a zero-crossing |
27501
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
185 if (displev == 1) |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
186 printf ( ... |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
187 "\nSearch for an interval around %g containing a sign change:\n", a); |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
188 printf (" Func-count a f(a) b "); |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
189 printf ("f(b) Procedure\n"); |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
190 fmt_str = " %4d %13.6g %13.6g %13.6g %13.6g %s\n"; |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
191 endif |
25158
57f150c4fe6d
fzero.m: Re-architect search for bracketing endpoint b if none is given (bug #53583).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
192 |
57f150c4fe6d
fzero.m: Re-architect search for bracketing endpoint b if none is given (bug #53583).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
193 ## For very small values, switch to absolute rather than relative search |
25745
092bb49b6962
Fix initial bracketing for fzero (bug #54445).
Marco Caliari <marco.caliari@univr.it>
parents:
25436
diff
changeset
|
194 if (abs (a) < .001) |
092bb49b6962
Fix initial bracketing for fzero (bug #54445).
Marco Caliari <marco.caliari@univr.it>
parents:
25436
diff
changeset
|
195 aa = ifelse (a == 0, 0.1, sign (a) * 0.1); |
092bb49b6962
Fix initial bracketing for fzero (bug #54445).
Marco Caliari <marco.caliari@univr.it>
parents:
25436
diff
changeset
|
196 else |
092bb49b6962
Fix initial bracketing for fzero (bug #54445).
Marco Caliari <marco.caliari@univr.it>
parents:
25436
diff
changeset
|
197 aa = a; |
092bb49b6962
Fix initial bracketing for fzero (bug #54445).
Marco Caliari <marco.caliari@univr.it>
parents:
25436
diff
changeset
|
198 endif |
27501
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
199 if (displev == 1) |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
200 printf (fmt_str, nfev, a, fa, a, fa, "initial interval"); |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
201 endif |
25158
57f150c4fe6d
fzero.m: Re-architect search for bracketing endpoint b if none is given (bug #53583).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
202 ## Search in an ever-widening range around the initial point. |
25436
996d78102a71
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
25158
diff
changeset
|
203 for srch = [-.01 +.025 -.05 +.10 -.25 +.50 -1 +2.5 -5 +10 -50 +100 -500 +1000] |
996d78102a71
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
25158
diff
changeset
|
204 b = aa + aa*srch; |
25158
57f150c4fe6d
fzero.m: Re-architect search for bracketing endpoint b if none is given (bug #53583).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
205 fb = fun (b); |
57f150c4fe6d
fzero.m: Re-architect search for bracketing endpoint b if none is given (bug #53583).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
206 nfev += 1; |
27501
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
207 if (displev == 1) |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
208 printf (fmt_str, nfev, a, fa, b, fb, "search"); |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
209 endif |
8305 | 210 if (sign (fa) * sign (fb) <= 0) |
211 break; | |
212 endif | |
213 endfor | |
214 endif | |
215 | |
216 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
|
217 u = a; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
218 a = b; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
219 b = u; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
220 |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
221 fu = fa; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
222 fa = fb; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
223 fb = fu; |
8305 | 224 endif |
225 | |
226 if (! (sign (fa) * sign (fb) <= 0)) | |
27069
0a62d9a6aa2d
Place Octave's warning and error IDs in to the "Octave" namespace (bug #56213).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
227 error ("Octave:fzero:bracket", "fzero: not a valid initial bracketing"); |
8305 | 228 endif |
229 | |
27501
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
230 if (displev == 1) |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
231 printf ("\nSearch for a zero in the interval [%g, %g]:\n", a, b); |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
232 disp (" Func-count x f(x) Procedure"); |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
233 fmt_str = " %4d %13.6g %13.6g %s\n"; |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
234 endif |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
235 |
10337
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
236 slope0 = (fb - fa) / (b - a); |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
237 |
10470 | 238 if (fa == 0) |
239 b = a; | |
240 fb = fa; | |
241 elseif (fb == 0) | |
242 a = b; | |
243 fa = fb; | |
244 endif | |
245 | |
8305 | 246 itype = 1; |
247 | |
248 if (abs (fa) < abs (fb)) | |
249 u = a; fu = fa; | |
250 else | |
251 u = b; fu = fb; | |
252 endif | |
27501
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
253 if (displev == 1) |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
254 printf (fmt_str, nfev, u, fu, "initial"); |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
255 endif |
8305 | 256 |
21088
323e92c4589f
fzero.m: Correctly choose tolerance (eps) based on class of fun and X0 (bug #46658).
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
257 if (isa (x0, "single") || isa (fa, "single")) |
323e92c4589f
fzero.m: Correctly choose tolerance (eps) based on class of fun and X0 (bug #46658).
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
258 macheps = eps ("single"); |
323e92c4589f
fzero.m: Correctly choose tolerance (eps) based on class of fun and X0 (bug #46658).
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
259 else |
323e92c4589f
fzero.m: Correctly choose tolerance (eps) based on class of fun and X0 (bug #46658).
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
260 macheps = eps ("double"); |
323e92c4589f
fzero.m: Correctly choose tolerance (eps) based on class of fun and X0 (bug #46658).
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
261 endif |
323e92c4589f
fzero.m: Correctly choose tolerance (eps) based on class of fun and X0 (bug #46658).
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
262 |
8305 | 263 d = e = u; |
264 fd = fe = fu; | |
265 mba = mu*(b - a); | |
266 while (niter < maxiter && nfev < maxfev) | |
267 switch (itype) | |
17174
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
268 case 1 |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
269 ## The initial test. |
21088
323e92c4589f
fzero.m: Correctly choose tolerance (eps) based on class of fun and X0 (bug #46658).
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
270 if (b - a <= 2*(2 * abs (u) * macheps + tolx)) |
17174
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
271 x = u; fval = fu; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
272 info = 1; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
273 break; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
274 endif |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
275 if (abs (fa) <= 1e3*abs (fb) && abs (fb) <= 1e3*abs (fa)) |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
276 ## Secant step. |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
277 c = u - (a - b) / (fa - fb) * fu; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
278 else |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
279 ## Bisection step. |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
280 c = 0.5*(a + b); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
281 endif |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
282 d = u; fd = fu; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
283 itype = 5; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
284 case {2, 3} |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
285 l = length (unique ([fa, fb, fd, fe])); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
286 if (l == 4) |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
287 ## Inverse cubic interpolation. |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
288 q11 = (d - e) * fd / (fe - fd); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
289 q21 = (b - d) * fb / (fd - fb); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
290 q31 = (a - b) * fa / (fb - fa); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
291 d21 = (b - d) * fd / (fd - fb); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
292 d31 = (a - b) * fb / (fb - fa); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
293 q22 = (d21 - q11) * fb / (fe - fb); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
294 q32 = (d31 - q21) * fa / (fd - fa); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
295 d32 = (d31 - q21) * fd / (fd - fa); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
296 q33 = (d32 - q22) * fa / (fe - fa); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
297 c = a + q31 + q32 + q33; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
298 endif |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
299 if (l < 4 || sign (c - a) * sign (c - b) > 0) |
25747 | 300 ## Quadratic interpolation + Newton. |
17174
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
301 a0 = fa; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
302 a1 = (fb - fa)/(b - a); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
303 a2 = ((fd - fb)/(d - b) - a1) / (d - a); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
304 ## Modification 1: this is simpler and does not seem to be worse. |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
305 c = a - a0/a1; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
306 if (a2 != 0) |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
307 c = a - a0/a1; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
308 for i = 1:itype |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
309 pc = a0 + (a1 + a2*(c - b))*(c - a); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
310 pdc = a1 + a2*(2*c - a - b); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
311 if (pdc == 0) |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
312 c = a - a0/a1; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
313 break; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
314 endif |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
315 c -= pc/pdc; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
316 endfor |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
317 endif |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
318 endif |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
319 itype += 1; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
320 case 4 |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
321 ## Double secant step. |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
322 c = u - 2*(b - a)/(fb - fa)*fu; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
323 ## Bisect if too far. |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
324 if (abs (c - u) > 0.5*(b - a)) |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
325 c = 0.5 * (b + a); |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
326 endif |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
327 itype = 5; |
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
328 case 5 |
8507 | 329 ## Bisection step. |
8305 | 330 c = 0.5 * (b + a); |
17174
c3c1ebfaa7dc
maint: Use common indentation for switch statement.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
331 itype = 2; |
8305 | 332 endswitch |
333 | |
8507 | 334 ## Don't let c come too close to a or b. |
21088
323e92c4589f
fzero.m: Correctly choose tolerance (eps) based on class of fun and X0 (bug #46658).
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
335 delta = 2*0.7*(2 * abs (u) * macheps + tolx); |
8305 | 336 if ((b - a) <= 2*delta) |
337 c = (a + b)/2; | |
338 else | |
339 c = max (a + delta, min (b - delta, c)); | |
340 endif | |
341 | |
8507 | 342 ## Calculate new point. |
8305 | 343 x = c; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
344 fval = fc = fun (c); |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20714
diff
changeset
|
345 niter += 1; nfev += 1; |
27501
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
346 if (displev == 1) |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
347 printf (fmt_str, nfev, x, fc, "interpolation"); |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
348 endif |
8305 | 349 |
8507 | 350 ## Modification 2: skip inverse cubic interpolation if |
351 ## nonmonotonicity is detected. | |
8305 | 352 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
|
353 ## The new point broke monotonicity. |
8507 | 354 ## Disable inverse cubic. |
8305 | 355 fe = fc; |
356 else | |
357 e = d; fe = fd; | |
358 endif | |
359 | |
8507 | 360 ## Bracketing. |
8305 | 361 if (sign (fa) * sign (fc) < 0) |
362 d = b; fd = fb; | |
363 b = c; fb = fc; | |
364 elseif (sign (fb) * sign (fc) < 0) | |
365 d = a; fd = fa; | |
366 a = c; fa = fc; | |
367 elseif (fc == 0) | |
368 a = b = c; fa = fb = fc; | |
369 info = 1; | |
370 break; | |
371 else | |
8507 | 372 ## This should never happen. |
27069
0a62d9a6aa2d
Place Octave's warning and error IDs in to the "Octave" namespace (bug #56213).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
373 error ("Octave:fzero:bracket", "fzero: zero point is not bracketed"); |
8305 | 374 endif |
375 | |
8507 | 376 ## If there's an output function, use it now. |
18133
8ca7b1906a41
fzero.m: Correctly detct if OutputFcn is present (bug #40889).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
377 if (! isempty (outfcn)) |
10296 | 378 optv.funccount = nfev; |
8305 | 379 optv.fval = fval; |
380 optv.iteration = niter; | |
381 if (outfcn (x, optv, "iter")) | |
382 info = -1; | |
383 break; | |
384 endif | |
385 endif | |
386 | |
387 if (abs (fa) < abs (fb)) | |
388 u = a; fu = fa; | |
389 else | |
390 u = b; fu = fb; | |
391 endif | |
21088
323e92c4589f
fzero.m: Correctly choose tolerance (eps) based on class of fun and X0 (bug #46658).
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
392 if (b - a <= 2*(2 * abs (u) * macheps + tolx)) |
8305 | 393 info = 1; |
394 break; | |
395 endif | |
396 | |
8507 | 397 ## Skip bisection step if successful reduction. |
8305 | 398 if (itype == 5 && (b - a) <= mba) |
399 itype = 2; | |
400 endif | |
401 if (itype == 2) | |
402 mba = mu * (b - a); | |
403 endif | |
404 endwhile | |
405 | |
10337
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
406 ## Check solution for a singularity by examining slope |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
407 if (info == 1) |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
408 if ((b - a) != 0 |
21088
323e92c4589f
fzero.m: Correctly choose tolerance (eps) based on class of fun and X0 (bug #46658).
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
409 && abs ((fb - fa)/(b - a) / slope0) > max (1e6, 0.5/(macheps+tolx))) |
10337
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
410 info = -5; |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
411 endif |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
412 endif |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
413 |
27501
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
414 if (displev != 0) |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
415 switch (info) |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
416 case 1 |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
417 if (displev != 3) |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
418 disp ("\nAlgorithm converged.\n"); |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
419 endif |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
420 case -1 |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
421 disp ("\nAlgorithm has been terminated by user.\n"); |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
422 case -5 |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
423 disp ("\nAlgorithm seemingly converged to a singular point.\n"); |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
424 otherwise |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
425 disp ( ... |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
426 "\nMaximum number of iterations or function evaluations reached.\n"); |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
427 endswitch |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
428 endif |
9d3c895cbe38
fzero.m: Implement "Display" option (bug #56954).
Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
parents:
27069
diff
changeset
|
429 |
8305 | 430 output.iterations = niter; |
10296 | 431 output.funcCount = nfev; |
25747 | 432 output.algorithm = "bisection, interpolation"; |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
433 output.bracketx = [a, b]; |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10470
diff
changeset
|
434 output.brackety = [fa, fb]; |
8305 | 435 |
436 endfunction | |
437 | |
21759
b002b4331a12
maint: Use two newlines after endfunction and start of BIST tests.
Rik <rik@octave.org>
parents:
21751
diff
changeset
|
438 ## A helper function that evaluates a function and checks for bad results. |
8305 | 439 function fx = guarded_eval (fun, x) |
440 fx = fun (x); | |
441 fx = fx(1); | |
442 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
|
443 error ("Octave:fzero:notreal", "fzero: non-real value encountered"); |
8305 | 444 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
|
445 error ("Octave:fzero:isnan", "fzero: NaN value encountered"); |
8305 | 446 endif |
447 endfunction | |
448 | |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
449 |
10296 | 450 %!shared opt0 |
451 %! 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
|
452 %!assert (fzero (@cos, [0, 3], opt0), pi/2, 10*eps) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
453 %!assert (fzero (@(x) x^(1/3) - 1e-8, [0,1], opt0), 1e-24, 1e-22*eps) |
25745
092bb49b6962
Fix initial bracketing for fzero (bug #54445).
Marco Caliari <marco.caliari@univr.it>
parents:
25436
diff
changeset
|
454 %!assert <*54445> (fzero (@ (x) x, 0), 0) |
092bb49b6962
Fix initial bracketing for fzero (bug #54445).
Marco Caliari <marco.caliari@univr.it>
parents:
25436
diff
changeset
|
455 %!assert <*54445> (fzero (@ (x) x + 1, 0), -1) |