From 75563505172dce2cc78116f0884819ca32620dbe Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Thu, 7 Mar 2013 23:18:47 -0500 Subject: [PATCH] Add the Rosenbrock functions and their tests. --- optimization/test_functions/rosenbrock.m | 11 +++++++++++ optimization/test_functions/rosenbrock1.m | 12 ++++++++++++ .../test_functions/rosenbrock_gradient.m | 9 +++++++++ .../test_functions/rosenbrock_gradient1.m | 12 ++++++++++++ optimization/test_functions/rosenbrock_hessian.m | 11 +++++++++++ .../test_functions/rosenbrock_hessian1.m | 12 ++++++++++++ tests/rosenbrock1_tests.m | 16 ++++++++++++++++ tests/rosenbrock_gradient_tests.m | 5 +++++ tests/rosenbrock_tests.m | 5 +++++ 9 files changed, 93 insertions(+) create mode 100644 optimization/test_functions/rosenbrock.m create mode 100644 optimization/test_functions/rosenbrock1.m create mode 100644 optimization/test_functions/rosenbrock_gradient.m create mode 100644 optimization/test_functions/rosenbrock_gradient1.m create mode 100644 optimization/test_functions/rosenbrock_hessian.m create mode 100644 optimization/test_functions/rosenbrock_hessian1.m create mode 100644 tests/rosenbrock1_tests.m create mode 100644 tests/rosenbrock_gradient_tests.m create mode 100644 tests/rosenbrock_tests.m diff --git a/optimization/test_functions/rosenbrock.m b/optimization/test_functions/rosenbrock.m new file mode 100644 index 0000000..8073a0a --- /dev/null +++ b/optimization/test_functions/rosenbrock.m @@ -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 index 0000000..5e4df05 --- /dev/null +++ b/optimization/test_functions/rosenbrock1.m @@ -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 index 0000000..50c4546 --- /dev/null +++ b/optimization/test_functions/rosenbrock_gradient.m @@ -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 index 0000000..e8a83b6 --- /dev/null +++ b/optimization/test_functions/rosenbrock_gradient1.m @@ -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 index 0000000..cd2a2b5 --- /dev/null +++ b/optimization/test_functions/rosenbrock_hessian.m @@ -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 index 0000000..63a105f --- /dev/null +++ b/optimization/test_functions/rosenbrock_hessian1.m @@ -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 index 0000000..4931403 --- /dev/null +++ b/tests/rosenbrock1_tests.m @@ -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 index 0000000..c45b8e7 --- /dev/null +++ b/tests/rosenbrock_gradient_tests.m @@ -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 index 0000000..66af77a --- /dev/null +++ b/tests/rosenbrock_tests.m @@ -0,0 +1,5 @@ +## Test the optimal point. + +unit_test_equals("rosenbrock(1,1) == 0", ... + 0, ... + rosenbrock(1,1)); -- 2.43.2