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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
6 ##
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7 ## This file is part of Octave.
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
13 ##
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 ## Octave is distributed in the hope that it will be useful, but
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
18 ##
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 ## You should have received a copy of the GNU General Public License
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
22 ##
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
23 ## Author: Jaroslav Hajek <highegg@gmail.com>
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
9500a66118dc improve fzero
Jaroslav Hajek <highegg@gmail.com>
parents: 10394
diff changeset
39 ## @example
9500a66118dc improve fzero
Jaroslav Hajek <highegg@gmail.com>
parents: 10394
diff changeset
40 ## sign (@var{fun}(@var{x0}(1))) * sign (@var{fun}(@var{x0}(2))) <= 0
9500a66118dc improve fzero
Jaroslav Hajek <highegg@gmail.com>
parents: 10394
diff changeset
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
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
51 ##
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
52 ## @qcode{"MaxFunEvals"} proscribes the maximum number of function evaluations
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
53 ## before the search is halted. The default value is @code{Inf}.
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
54 ## The value must be a positive integer.
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
55 ##
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
56 ## @qcode{"MaxIter"} proscribes the maximum number of algorithm iterations
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
57 ## before the search is halted. The default value is @code{Inf}.
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
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
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
60 ## @qcode{"TolX"} specifies the termination tolerance for the solution @var{x}.
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
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
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
63 ## For a description of the other options, see @ref{XREFoptimset,,optimset}.
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
64 ## To initialize an options structure with default values for @code{fzero} use
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
65 ## @code{options = optimset ("fzero")}.
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
66 ##
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
67 ## On exit, the function returns @var{x}, the approximate zero point, and
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
68 ## @var{fval}, the function evaluated at @var{x}.
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
69 ##
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
70 ## The third output @var{info} reports whether the algorithm succeeded and
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
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
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
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
d02e4b1df697 doc: Spellcheck documentation.
Rik <rik@octave.org>
parents: 26043
diff changeset
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
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
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
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
136 if (nargin < 2 || nargin > 3)
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
137 print_usage ();
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
142 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
157 maxfev = optimget (options, "MaxFunEvals", Inf);
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
158 maxiter = optimget (options, "MaxIter", Inf);
8305
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
161
25747
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
162 mu = 0.5;
8305
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
163
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
164 if (funvalchk)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
165 ## Replace fun with a guarded version.
8305
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
166 fun = @(x) guarded_eval (fun, x);
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
167 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
168
25747
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
172
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
173 x = fval = a = fa = b = fb = NaN;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
174
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
178 nfev = 1;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
179 if (length (x0) > 1)
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
210 if (sign (fa) * sign (fb) <= 0)
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
211 break;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
212 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
213 endfor
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
214 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
215
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
224 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
225
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
228 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
9500a66118dc improve fzero
Jaroslav Hajek <highegg@gmail.com>
parents: 10394
diff changeset
238 if (fa == 0)
9500a66118dc improve fzero
Jaroslav Hajek <highegg@gmail.com>
parents: 10394
diff changeset
239 b = a;
9500a66118dc improve fzero
Jaroslav Hajek <highegg@gmail.com>
parents: 10394
diff changeset
240 fb = fa;
9500a66118dc improve fzero
Jaroslav Hajek <highegg@gmail.com>
parents: 10394
diff changeset
241 elseif (fb == 0)
9500a66118dc improve fzero
Jaroslav Hajek <highegg@gmail.com>
parents: 10394
diff changeset
242 a = b;
9500a66118dc improve fzero
Jaroslav Hajek <highegg@gmail.com>
parents: 10394
diff changeset
243 fa = fb;
9500a66118dc improve fzero
Jaroslav Hajek <highegg@gmail.com>
parents: 10394
diff changeset
244 endif
9500a66118dc improve fzero
Jaroslav Hajek <highegg@gmail.com>
parents: 10394
diff changeset
245
8305
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
246 itype = 1;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
247
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
248 if (abs (fa) < abs (fb))
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
249 u = a; fu = fa;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
250 else
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
251 u = b; fu = fb;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
263 d = e = u;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
264 fd = fe = fu;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
265 mba = mu*(b - a);
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
266 while (niter < maxiter && nfev < maxfev)
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
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
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
329 ## Bisection step.
8305
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
332 endswitch
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
333
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
336 if ((b - a) <= 2*delta)
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
337 c = (a + b)/2;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
338 else
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
339 c = max (a + delta, min (b - delta, c));
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
340 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
341
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
342 ## Calculate new point.
8305
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
349
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
350 ## Modification 2: skip inverse cubic interpolation if
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
351 ## nonmonotonicity is detected.
8305
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
354 ## Disable inverse cubic.
8305
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
355 fe = fc;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
356 else
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
357 e = d; fe = fd;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
358 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
359
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
360 ## Bracketing.
8305
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
361 if (sign (fa) * sign (fc) < 0)
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
362 d = b; fd = fb;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
363 b = c; fb = fc;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
364 elseif (sign (fb) * sign (fc) < 0)
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
365 d = a; fd = fa;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
366 a = c; fa = fc;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
367 elseif (fc == 0)
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
368 a = b = c; fa = fb = fc;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
369 info = 1;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
370 break;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
371 else
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
374 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
375
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
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
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents: 9464
diff changeset
378 optv.funccount = nfev;
8305
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
379 optv.fval = fval;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
380 optv.iteration = niter;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
381 if (outfcn (x, optv, "iter"))
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
382 info = -1;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
383 break;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
384 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
385 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
386
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
387 if (abs (fa) < abs (fb))
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
388 u = a; fu = fa;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
389 else
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
390 u = b; fu = fb;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
393 info = 1;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
394 break;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
395 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
396
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8468
diff changeset
397 ## Skip bisection step if successful reduction.
8305
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
398 if (itype == 5 && (b - a) <= mba)
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
399 itype = 2;
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
400 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
401 if (itype == 2)
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
402 mba = mu * (b - a);
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
403 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
404 endwhile
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
430 output.iterations = niter;
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents: 9464
diff changeset
431 output.funcCount = nfev;
25747
3c45cfac7692 fzero.m: Overhaul function.
Rik <rik@octave.org>
parents: 25745
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
435
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
436 endfunction
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
439 function fx = guarded_eval (fun, x)
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
440 fx = fun (x);
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
441 fx = fx(1);
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
446 endif
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
447 endfunction
368b504777a8 implement fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
448
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17281
diff changeset
449
10296
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents: 9464
diff changeset
450 %!shared opt0
035ac548a67e implement fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents: 9464
diff changeset
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)