Add extended_rosenbrock1() and its tests.
authorMichael Orlitzky <michael@orlitzky.com>
Fri, 8 Mar 2013 15:01:16 +0000 (10:01 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Fri, 8 Mar 2013 15:01:16 +0000 (10:01 -0500)
optimization/test_functions/extended_rosenbrock1.m [new file with mode: 0644]
tests/extended_rosenbrock1_tests.m [new file with mode: 0644]

diff --git a/optimization/test_functions/extended_rosenbrock1.m b/optimization/test_functions/extended_rosenbrock1.m
new file mode 100644 (file)
index 0000000..1f54520
--- /dev/null
@@ -0,0 +1,30 @@
+function f = extended_rosenbrock1(x)
+  ##
+  ## The extended Rosenbrock function. See Dennis & Schnabel, Appendix
+  ## B, problem #1.
+  ##
+  ## This function has a minimum at x=(1,1,...,1) with f(x) == 0. The
+  ## suggested starting point is x0=(-1.2, 1,-1.2, 1,...,-1.2, 1).
+  ## Since the number of arguments is variable, we take a vector
+  ## instead of its individual components.
+  ##
+  n = length(x);
+
+  if (odd(n))
+    ## 'm' below must be an integer.
+    f = NA;
+    return;
+  end
+
+  m = n / 2;
+  f = 0;
+
+  % The extended Rosenbrock is simply a sum of Rosenbrock
+  % applications.
+  for k = [ 1 : m ]
+    y1 = x(2*k - 1);
+    y2 = x(2*k);
+    f_k = rosenbrock(y1, y2);
+    f = f + f_k;
+  end
+end
diff --git a/tests/extended_rosenbrock1_tests.m b/tests/extended_rosenbrock1_tests.m
new file mode 100644 (file)
index 0000000..2f0eb8f
--- /dev/null
@@ -0,0 +1,14 @@
+## Test the optimal point.
+
+for m = [ 1 : 10 ]
+  x = repmat([1;1], m, 1);
+
+  msg = sprintf("extended_rosenbrock1([1;1;...]) == 0 (m = %d)", m);
+  unit_test_equals(msg, 0, extended_rosenbrock1(x));
+end
+
+## It should fail with the wrong number of coordinates.
+f = extended_rosenbrock1([1]);
+unit_test_equals("extended_rosenbrock1 fails when length(x) is odd", ...
+                true, ...
+                isna(f));