comparison scripts/optimization/fzero.m @ 10470:9500a66118dc

improve fzero
author Jaroslav Hajek <highegg@gmail.com>
date Sat, 27 Mar 2010 10:07:10 +0100
parents bc475cd49147
children a8ce6bdecce5
comparison
equal deleted inserted replaced
10469:ef9dee167f75 10470:9500a66118dc
19 ## Author: Jaroslav Hajek <highegg@gmail.com> 19 ## Author: Jaroslav Hajek <highegg@gmail.com>
20 20
21 ## -*- texinfo -*- 21 ## -*- texinfo -*-
22 ## @deftypefn {Function File} {[@var{x}, @var{fval}, @var{info}, @var{output}] =} fzero (@var{fun}, @var{x0}, @var{options}) 22 ## @deftypefn {Function File} {[@var{x}, @var{fval}, @var{info}, @var{output}] =} fzero (@var{fun}, @var{x0}, @var{options})
23 ## Find a zero point of a univariate function. @var{fun} should be a function 23 ## Find a zero point of a univariate function. @var{fun} should be a function
24 ## handle or name. @var{x0} specifies a starting point. @var{options} is a 24 ## handle or name. @var{x0} should be a two-element vector specifying the initial
25 ## structure specifying additional options. Currently, @code{fzero} 25 ## bracketing. It should hold
26 ## @example
27 ## sign (@var{fun}(@var{x0}(1))) * sign (@var{fun}(@var{x0}(2))) <= 0
28 ## @end example
29 ## If only a single scalar is given as @var{x0}, several nearby and distant
30 ## values are probed in an attempt to obtain a valid bracketing. If this
31 ## is not successful, the function fails.
32 ## @var{options} is a structure specifying additional options.
33 ## Currently, @code{fzero}
26 ## recognizes these options: @code{"FunValCheck"}, @code{"OutputFcn"}, 34 ## recognizes these options: @code{"FunValCheck"}, @code{"OutputFcn"},
27 ## @code{"TolX"}, @code{"MaxIter"}, @code{"MaxFunEvals"}. 35 ## @code{"TolX"}, @code{"MaxIter"}, @code{"MaxFunEvals"}.
28 ## For description of these options, see @ref{doc-optimset,,optimset}. 36 ## For description of these options, see @ref{doc-optimset,,optimset}.
29 ## 37 ##
30 ## On exit, the function returns @var{x}, the approximate zero point 38 ## On exit, the function returns @var{x}, the approximate zero point
134 if (! (sign (fa) * sign (fb) <= 0)) 142 if (! (sign (fa) * sign (fb) <= 0))
135 error ("fzero:bracket", "fzero: not a valid initial bracketing"); 143 error ("fzero:bracket", "fzero: not a valid initial bracketing");
136 endif 144 endif
137 145
138 slope0 = (fb - fa) / (b - a); 146 slope0 = (fb - fa) / (b - a);
147
148 if (fa == 0)
149 b = a;
150 fb = fa;
151 elseif (fb == 0)
152 a = b;
153 fa = fb;
154 endif
139 155
140 itype = 1; 156 itype = 1;
141 157
142 if (abs (fa) < abs (fb)) 158 if (abs (fa) < abs (fb))
143 u = a; fu = fa; 159 u = a; fu = fa;