]> gitweb.michael.orlitzky.com - octave.git/commitdiff
Add trigonometric functions and their tests.
authorMichael Orlitzky <michael@orlitzky.com>
Thu, 14 Mar 2013 04:27:32 +0000 (00:27 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Thu, 14 Mar 2013 04:27:32 +0000 (00:27 -0400)
optimization/test_functions/trigonometric1.m [new file with mode: 0644]
optimization/test_functions/trigonometric_gradient1.m [new file with mode: 0644]
optimization/test_functions/trigonometric_hessian1.m [new file with mode: 0644]
tests/trigonometric1_tests.m [new file with mode: 0644]
tests/trigonometric_gradient1_tests.m [new file with mode: 0644]

diff --git a/optimization/test_functions/trigonometric1.m b/optimization/test_functions/trigonometric1.m
new file mode 100644 (file)
index 0000000..f7fd625
--- /dev/null
@@ -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 (file)
index 0000000..134ce86
--- /dev/null
@@ -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 (file)
index 0000000..f3e40a4
--- /dev/null
@@ -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 (file)
index 0000000..9e2c5e8
--- /dev/null
@@ -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 (file)
index 0000000..36231af
--- /dev/null
@@ -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);