From: Michael Orlitzky Date: Fri, 8 Mar 2013 15:01:16 +0000 (-0500) Subject: Add extended_rosenbrock1() and its tests. X-Git-Url: http://gitweb.michael.orlitzky.com/?p=octave.git;a=commitdiff_plain;h=f81dbdae572c276be06e7cb9bbe2d6c5293e60ec Add extended_rosenbrock1() and its tests. --- diff --git a/optimization/test_functions/extended_rosenbrock1.m b/optimization/test_functions/extended_rosenbrock1.m new file mode 100644 index 0000000..1f54520 --- /dev/null +++ b/optimization/test_functions/extended_rosenbrock1.m @@ -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 index 0000000..2f0eb8f --- /dev/null +++ b/tests/extended_rosenbrock1_tests.m @@ -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));