annotate scripts/optimization/fzero.m @ 10687:a8ce6bdecce5

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