X-Git-Url: http://gitweb.michael.orlitzky.com/?p=octave.git;a=blobdiff_plain;f=tests%2Fsteepest_descent_tests.m;h=d6de1ccaf4b013539d45833a70c3e293bb0ad6b6;hp=035c82d20bfafbc56021f1b2a11b7ffc1839dee2;hb=2607d0ce1289b34ecce0a82b11e9935e239fe708;hpb=f7b0583a5615b9314c818e4037af554cfb679d8c diff --git a/tests/steepest_descent_tests.m b/tests/steepest_descent_tests.m index 035c82d..d6de1cc 100644 --- a/tests/steepest_descent_tests.m +++ b/tests/steepest_descent_tests.m @@ -6,7 +6,7 @@ ## stopping condition, so we should too. ## max_iterations = 100000; -tolerance = 1e-10; +tolerance = 1e-8; ## First a simple example. Q = [5,1,2; ... @@ -23,7 +23,7 @@ q = @(x) (1/2)*x'*Q*x - b'*x; g = @(x) Q*x - b; % The gradient of q at x. % The step size algorithm to use in the steepest descent method. -step_size = @(x) step_length_positive_definite(g(x), Q); +step_size = @(x) step_length_positive_definite(g(x), Q, -g(x)); sd = steepest_descent(g, x0, step_size, tolerance, max_iterations); diff = norm(cgm - sd, 'inf'); @@ -46,7 +46,7 @@ for n = [ 5, 10, 25, 50, 100 ] g = @(x) Q*x - b; % The gradient of q at x. % The step size algorithm to use in the steepest descent method. - step_size = @(x) step_length_positive_definite(g(x), Q); + step_size = @(x) step_length_positive_definite(g(x), Q, -g(x)); ## pcg() stops when the /relative/ norm falls below tolerance. To ## eliminate the relativity, we divide the tolerance by the @@ -57,10 +57,13 @@ for n = [ 5, 10, 25, 50, 100 ] max_iterations, ... C, ... C'); - x_sd = steepest_descent(g, x0, step_size, tolerance, max_iterations); + [x_sd, k] = steepest_descent(g, x0, step_size, tolerance, max_iterations); - ## Note: pcg() uses the 2-norm. - diff = abs(norm(g(x_pcg)) - norm(g(x_sd), 'inf')); + diff = norm(x_pcg - x_sd, 'inf'); msg = sprintf("Our steepest descent agrees with Octave's pcg, n=%d.", n); - unit_test_equals(msg, true, diff <= tolerance); + + ## There's no good way to choose the tolerance here, since each + ## individual algorithm terminates based on the (2,infinity)-norm of + ## the gradient. + unit_test_equals(msg, true, diff <= sqrt(tolerance)); end