From da77431bfbbb5fed8835d09aeea0de7eb7f6194a Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Thu, 14 Mar 2013 00:27:32 -0400 Subject: [PATCH] Add trigonometric functions and their tests. --- optimization/test_functions/trigonometric1.m | 18 +++++++++ .../test_functions/trigonometric_gradient1.m | 27 ++++++++++++++ .../test_functions/trigonometric_hessian1.m | 37 +++++++++++++++++++ tests/trigonometric1_tests.m | 8 ++++ tests/trigonometric_gradient1_tests.m | 8 ++++ 5 files changed, 98 insertions(+) create mode 100644 optimization/test_functions/trigonometric1.m create mode 100644 optimization/test_functions/trigonometric_gradient1.m create mode 100644 optimization/test_functions/trigonometric_hessian1.m create mode 100644 tests/trigonometric1_tests.m create mode 100644 tests/trigonometric_gradient1_tests.m diff --git a/optimization/test_functions/trigonometric1.m b/optimization/test_functions/trigonometric1.m new file mode 100644 index 0000000..f7fd625 --- /dev/null +++ b/optimization/test_functions/trigonometric1.m @@ -0,0 +1,18 @@ +function f = trigonometric1(x) + ## + ## The trigonometric function. See More, Garbow, and Hillstrom, + ## function #26. + ## + ## This function has a minimum with f(x) == 0. The suggested + ## starting point is x0=(1/n, 1/n,...). + ## + n = length(x); + f = 0; + + cos_sum = sum(cos(x)); + + for k = [ 1 : n ] + f_k = n - cos_sum + k*(1 - cos(x(k))) - sin(x(k)); + f = f + (f_k)^2; + end +end diff --git a/optimization/test_functions/trigonometric_gradient1.m b/optimization/test_functions/trigonometric_gradient1.m new file mode 100644 index 0000000..134ce86 --- /dev/null +++ b/optimization/test_functions/trigonometric_gradient1.m @@ -0,0 +1,27 @@ +function g = trigonometric_gradient1(x) + ## + ## The gradient of the trigonometric function. See trigonometric1.m + ## for more information. + ## + n = length(x); + g = zeros(n,1); + + cos_sum = sum(cos(x)); + + for k = [ 1 : n ] + f_k = n - cos_sum + k*(1 - cos(x(k))) - sin(x(k)); + + for j = [ 1 : n ] + ## Add to the jth component of g the partial of f^2 with + ## respect to x(j). The first term that we add here exists + ## regardless of j. + g(j) = g(j) + 2*f_k*sin(x(j)); + + if (j == k) + ## But this term vanishes when j != k. + g(j) = g(j) + 2*f_k*k*sin(x(k)) - 2*f_k*cos(x(k)); + end + end + end + +end diff --git a/optimization/test_functions/trigonometric_hessian1.m b/optimization/test_functions/trigonometric_hessian1.m new file mode 100644 index 0000000..f3e40a4 --- /dev/null +++ b/optimization/test_functions/trigonometric_hessian1.m @@ -0,0 +1,37 @@ +function H = trigonometric_hessian1(x) + ## + ## The Hessian of the Trigonometric function. See trigonometric1.m + ## for more information. Not my implementation. Too ugly to + ## recreate. + ## + n = length(x); + H = zeros(n,n); + + cos_sum = sum(cos(x)); + + for i = 1 : n + H(i,i) = sin ( x(i) ); + end + + s = 0; + for j = 1 : n + th = cos ( x(j) ); + t = ( n + j ) - H(j,j) - cos_sum - j * th; + s = s + t; + for k = 1 : j-1 + H(j,k) = 2 * ( sin ( x(k) ) * ( ( n + j + k ) * H(j,j) - th ) ... + - H(j,j) * cos ( x(k) ) ); + end + H(j,j) = ( j * ( j + 2 ) + n ) * H(j,j)^2 + th * ... + ( th - ( 2 * j + 2 ) * H(j,j) ) + t * ( j * th + H(j,j) ); + end + + for j = 1 : n + H(j,j) = 2 * ( H(j,j) + cos ( x(j) ) * s ); + end + + for i = 1 : n + H(i,i+1:n) = H(i+1:n,i); + end + +end diff --git a/tests/trigonometric1_tests.m b/tests/trigonometric1_tests.m new file mode 100644 index 0000000..9e2c5e8 --- /dev/null +++ b/tests/trigonometric1_tests.m @@ -0,0 +1,8 @@ +## Test the optimal point. + +x = [0.24307; + 0.61268]; + +unit_test_equals("trigonometric1([0.24307; 0.61268]) == 0", ... + true, ... + norm(trigonometric1(x)) < 1e-6); diff --git a/tests/trigonometric_gradient1_tests.m b/tests/trigonometric_gradient1_tests.m new file mode 100644 index 0000000..36231af --- /dev/null +++ b/tests/trigonometric_gradient1_tests.m @@ -0,0 +1,8 @@ +## Test the optimal point. + +x = [0.24307; + 0.61268]; + +unit_test_equals("trigonometric_gradient1([0.24307; 0.61268]) == 0", ... + true, ... + norm(trigonometric_gradient1(x)) < 1e-5); -- 2.43.2