]> gitweb.michael.orlitzky.com - octave.git/commitdiff
Add the Rosenbrock functions and their tests.
authorMichael Orlitzky <michael@orlitzky.com>
Fri, 8 Mar 2013 04:18:47 +0000 (23:18 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Fri, 8 Mar 2013 04:18:47 +0000 (23:18 -0500)
optimization/test_functions/rosenbrock.m [new file with mode: 0644]
optimization/test_functions/rosenbrock1.m [new file with mode: 0644]
optimization/test_functions/rosenbrock_gradient.m [new file with mode: 0644]
optimization/test_functions/rosenbrock_gradient1.m [new file with mode: 0644]
optimization/test_functions/rosenbrock_hessian.m [new file with mode: 0644]
optimization/test_functions/rosenbrock_hessian1.m [new file with mode: 0644]
tests/rosenbrock1_tests.m [new file with mode: 0644]
tests/rosenbrock_gradient_tests.m [new file with mode: 0644]
tests/rosenbrock_tests.m [new file with mode: 0644]

diff --git a/optimization/test_functions/rosenbrock.m b/optimization/test_functions/rosenbrock.m
new file mode 100644 (file)
index 0000000..8073a0a
--- /dev/null
@@ -0,0 +1,11 @@
+function f = rosenbrock(x1, x2)
+  ##
+  ## The Rosenbrock function. See Dennis & Schnabel, Appendix B, problem #1.
+  ## (The "regular" Rosenbrock function is simply the Extended Rosenbrock
+  ## with m=1).
+  ##
+  ## This function has a minimum at x=(1,1) with f(x) == 0. The
+  ## suggested starting point is x0=(-1.2, 1).
+  ##
+  f = 100*(x2 - x1^2)^2 + (1 - x1)^2;
+end
diff --git a/optimization/test_functions/rosenbrock1.m b/optimization/test_functions/rosenbrock1.m
new file mode 100644 (file)
index 0000000..5e4df05
--- /dev/null
@@ -0,0 +1,12 @@
+function f = rosenbrock1(x)
+  ##
+  ## A version of the Rosenbrock function which takes a column
+  ## 2-vector instead of two distinct arguments. See rosenbrock.m for
+  ## more information.
+  ##
+  if (length(x) == 2)
+    f = rosenbrock(x(1), x(2));
+  else
+    f = NA;
+  end
+end
diff --git a/optimization/test_functions/rosenbrock_gradient.m b/optimization/test_functions/rosenbrock_gradient.m
new file mode 100644 (file)
index 0000000..50c4546
--- /dev/null
@@ -0,0 +1,9 @@
+function g = rosenbrock_gradient(x1, x2)
+  ##
+  ## The gradient of the Rosenbrock function. See rosenbrock.m for more
+  ## information.
+  ##
+  f_x1 = -400*x1*(x2 - x1^2) + 2*x1 - 2;
+  f_x2 = 200*(x2 - x1^2);
+  g = [f_x1; f_x2];
+end
diff --git a/optimization/test_functions/rosenbrock_gradient1.m b/optimization/test_functions/rosenbrock_gradient1.m
new file mode 100644 (file)
index 0000000..e8a83b6
--- /dev/null
@@ -0,0 +1,12 @@
+function g = rosenbrock_gradient1(x)
+  ##
+  ## A version of the rosenbrock_gradient() function which takes a
+  ## column 2-vector instead of two distinct arguments. See
+  ## rosenbrock_gradient.m for more information.
+  ##
+  if (length(x) == 2)
+    g = rosenbrock_gradient(x(1), x(2));
+  else
+    g = NA;
+  end
+end
diff --git a/optimization/test_functions/rosenbrock_hessian.m b/optimization/test_functions/rosenbrock_hessian.m
new file mode 100644 (file)
index 0000000..cd2a2b5
--- /dev/null
@@ -0,0 +1,11 @@
+function H = rosenbrock_hessian(x1, x2)
+  ##
+  ## The Hessian of the Rosenbrock function. See rosenbrock.m for more
+  ## information.
+  ##
+  H = zeros(2,2);
+  H(1,1) = 1200*x1^2 - 400*x2 + 2;
+  H(1,2) = -400*x1;
+  H(2,1) = -400*x1;
+  H(2,2) = 200;
+end
diff --git a/optimization/test_functions/rosenbrock_hessian1.m b/optimization/test_functions/rosenbrock_hessian1.m
new file mode 100644 (file)
index 0000000..63a105f
--- /dev/null
@@ -0,0 +1,12 @@
+function H = rosenbrock_hessian1(x)
+  ##
+  ## A version of the rosenbrock_hessian() function which takes a column
+  ## 2-vector instead of two distinct arguments. See rosenbrock_hessian.m
+  ## for more information.
+  ##
+  if (length(x) == 2)
+    H = rosenbrock_hessian(x(1), x(2));
+  else
+    H = NA;
+  end
+end
diff --git a/tests/rosenbrock1_tests.m b/tests/rosenbrock1_tests.m
new file mode 100644 (file)
index 0000000..4931403
--- /dev/null
@@ -0,0 +1,16 @@
+## Test the optimal point.
+
+unit_test_equals("rosenbrock1([1;1]) == 0", ...
+                0, ...
+                rosenbrock1([1;1]));
+
+## It should fail with the wrong number of coordinates.
+f = rosenbrock1([1]);
+unit_test_equals("rosenbrock1 fails with too few coordinates", ...
+                true, ...
+                isna(f));
+
+f = rosenbrock1([1;2;3]);
+unit_test_equals("rosenbrock1 fails with too many coordinates", ...
+                true, ...
+                isna(f));
diff --git a/tests/rosenbrock_gradient_tests.m b/tests/rosenbrock_gradient_tests.m
new file mode 100644 (file)
index 0000000..c45b8e7
--- /dev/null
@@ -0,0 +1,5 @@
+## The gradient should be zero at the optimal point.
+
+unit_test_equals("rosenbrock_gradient(1,1) == 0", ...
+                0, ...
+                rosenbrock_gradient(1,1));
diff --git a/tests/rosenbrock_tests.m b/tests/rosenbrock_tests.m
new file mode 100644 (file)
index 0000000..66af77a
--- /dev/null
@@ -0,0 +1,5 @@
+## Test the optimal point.
+
+unit_test_equals("rosenbrock(1,1) == 0", ...
+                0, ...
+                rosenbrock(1,1));