# HG changeset patch # User Jaroslav Hajek # Date 1269680830 -3600 # Node ID 9500a66118dcd4e68ed532341a224ff57959a620 # Parent ef9dee167f75d8b7b5f224ba8d209b14fa0ce195 improve fzero diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,8 @@ +2010-03-27 Jaroslav Hajek + + * optimization/fzero.m: Handle the breakdown if initial bracketing + contains an exact root. Improve docstring. + 2010-03-26 Jaroslav Hajek * miscellaneous/module.mk: Add unimplemented.m here. diff --git a/scripts/optimization/fzero.m b/scripts/optimization/fzero.m --- a/scripts/optimization/fzero.m +++ b/scripts/optimization/fzero.m @@ -21,8 +21,16 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{x}, @var{fval}, @var{info}, @var{output}] =} fzero (@var{fun}, @var{x0}, @var{options}) ## Find a zero point of a univariate function. @var{fun} should be a function -## handle or name. @var{x0} specifies a starting point. @var{options} is a -## structure specifying additional options. Currently, @code{fzero} +## handle or name. @var{x0} should be a two-element vector specifying the initial +## bracketing. It should hold +## @example +## sign (@var{fun}(@var{x0}(1))) * sign (@var{fun}(@var{x0}(2))) <= 0 +## @end example +## If only a single scalar is given as @var{x0}, several nearby and distant +## values are probed in an attempt to obtain a valid bracketing. If this +## is not successful, the function fails. +## @var{options} is a structure specifying additional options. +## Currently, @code{fzero} ## recognizes these options: @code{"FunValCheck"}, @code{"OutputFcn"}, ## @code{"TolX"}, @code{"MaxIter"}, @code{"MaxFunEvals"}. ## For description of these options, see @ref{doc-optimset,,optimset}. @@ -137,6 +145,14 @@ slope0 = (fb - fa) / (b - a); + if (fa == 0) + b = a; + fb = fa; + elseif (fb == 0) + a = b; + fa = fb; + endif + itype = 1; if (abs (fa) < abs (fb))