changeset 14386:59aab666f2bf

Extend "FunValCheck" option to optimization routines to detect Inf values. * fminunc.m, fsolve.m: Extend "FunValCheck" option to optimization routines to detect Inf values.
author Rik <octave@nomad.inbox5.com>
date Sat, 18 Feb 2012 12:43:46 -0800
parents 310039bc3dd3
children 966ec84e7858
files scripts/optimization/fminunc.m scripts/optimization/fsolve.m
diffstat 2 files changed, 11 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/optimization/fminunc.m	Sat Feb 18 11:59:21 2012 -0800
+++ b/scripts/optimization/fminunc.m	Sat Feb 18 12:43:46 2012 -0800
@@ -354,9 +354,12 @@
     error ("fminunc:notreal", "fminunc: non-real value encountered");
   elseif (any (isnan (fx(:))))
     error ("fminunc:isnan", "fminunc: NaN value encountered");
+  elseif (any (isinf (fx(:))))
+    error ("fminunc:isinf", "fminunc: Inf value encountered");
   endif
 endfunction
 
+
 %!function f = __rosenb (x)
 %!  n = length (x);
 %!  f = sumsq (1 - x(1:n-1)) + 100 * sumsq (x(2:n) - x(1:n-1).^2);
@@ -373,6 +376,12 @@
 %! assert (info > 0);
 %! assert (x, ones (1, 4), tol);
 %! assert (fval, 0, tol);
+%% Test FunValCheck works correctly
+%!assert (fminunc (@(x) x^2, 1, optimset ("FunValCheck", "on")), 0, eps)
+%!error <non-real value> fminunc (@(x) x + i, 1, optimset ("FunValCheck", "on"))
+%!error <NaN value> fminunc (@(x) x + NaN, 1, optimset ("FunValCheck", "on"))
+%!error <Inf value> fminunc (@(x) x + Inf, 1, optimset ("FunValCheck", "on"))
+
 
 ## Solve the double dogleg trust-region minimization problem:
 ## Minimize 1/2*norm(r*x)^2  subject to the constraint norm(d.*x) <= delta,
--- a/scripts/optimization/fsolve.m	Sat Feb 18 11:59:21 2012 -0800
+++ b/scripts/optimization/fsolve.m	Sat Feb 18 12:43:46 2012 -0800
@@ -450,6 +450,8 @@
     error ("fsolve:notnum", "fsolve: non-numeric value encountered");
   elseif (any (isnan (fx(:))))
     error ("fsolve:isnan", "fsolve: NaN value encountered");
+  elseif (any (isinf (fx(:))))
+    error ("fsolve:isinf", "fsolve: Inf value encountered");
   endif
 endfunction