changeset 10470:9500a66118dc

improve fzero
author Jaroslav Hajek <highegg@gmail.com>
date Sat, 27 Mar 2010 10:07:10 +0100
parents ef9dee167f75
children bcabc1c4f20c
files scripts/ChangeLog scripts/optimization/fzero.m
diffstat 2 files changed, 23 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Fri Mar 26 11:57:03 2010 +0100
+++ b/scripts/ChangeLog	Sat Mar 27 10:07:10 2010 +0100
@@ -1,3 +1,8 @@
+2010-03-27  Jaroslav Hajek  <highegg@gmail.com>
+
+	* optimization/fzero.m: Handle the breakdown if initial bracketing
+	contains an exact root. Improve docstring.
+
 2010-03-26  Jaroslav Hajek  <highegg@gmail.com>
 
 	* miscellaneous/module.mk: Add unimplemented.m here.
--- a/scripts/optimization/fzero.m	Fri Mar 26 11:57:03 2010 +0100
+++ b/scripts/optimization/fzero.m	Sat Mar 27 10:07:10 2010 +0100
@@ -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))