changeset 19477:0f79fa9b3a8c

fmindbnd.m: Check input range is low to high (bug #43219). * fminbnd.m: Validate that lower bound of input range is smaller than upper bound. Add BIST tests for new input validation.
author Massimiliano Fasi <massimiliano.fasi@gmail.com>
date Fri, 19 Sep 2014 18:24:05 +0200
parents 8ee14c64ab5f
children 3f29b433bd5d
files scripts/optimization/fminbnd.m
diffstat 1 files changed, 8 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/optimization/fminbnd.m	Tue Dec 30 08:31:51 2014 -0800
+++ b/scripts/optimization/fminbnd.m	Fri Sep 19 18:24:05 2014 +0200
@@ -70,6 +70,11 @@
   if (nargin < 2 || nargin > 4)
     print_usage ();
   endif
+  
+  if (xmin > xmax)
+    error ("Octave:invalid-input-arg",
+           "fminbnd: the lower bound cannot be greater than the upper one");
+  endif
 
   if (ischar (fun))
     fun = str2func (fun, "global");
@@ -284,4 +289,7 @@
 %!assert (fminbnd (@(x) abs (x-1e7), 0, 1e10, opt0), 1e7, 10e7*sqrt (eps))
 %!assert (fminbnd (@(x) x^2 + sin (2*pi*x), 0.4, 1, opt0), fzero (@(x) 2*x + 2*pi*cos (2*pi*x), [0.4, 1], opt0), sqrt (eps))
 %!assert (fminbnd (@(x) x > 0.3, 0, 1) < 0.3) 
+%!assert (fminbnd (@(x) sin (x), 0, 0), 0, eps) 
 
+%!error <lower bound cannot be greater> fminbnd (@(x) sin (x), 0, -pi)
+