X-Git-Url: http://gitweb.michael.orlitzky.com/?p=octave.git;a=blobdiff_plain;f=tests%2Fconjugate_gradient_method_tests.m;h=e04b2e2a0e9a28afab3a0f083872303775471100;hp=56987a50ac0615b1bcaf61458872cb72d67c0355;hb=e1b71b4ca7cfa08ac76744a17a3778d4ccfaa7e2;hpb=336f1181f7f065a07ee58bd772a875f7f4b39247 diff --git a/tests/conjugate_gradient_method_tests.m b/tests/conjugate_gradient_method_tests.m index 56987a5..e04b2e2 100644 --- a/tests/conjugate_gradient_method_tests.m +++ b/tests/conjugate_gradient_method_tests.m @@ -8,9 +8,29 @@ x0 = [1;1;1]; ## Solved over the rationals. expected = [2/73; 11/73; 26/73]; -actual = conjugate_gradient_method(A, b, x0, 1e-6); +actual = conjugate_gradient_method(A, b, x0, 1e-6, 1000); diff = norm(actual - expected); unit_test_equals("CGM works on an example", ... true, ... norm(diff) < 1e-6); + + +# Let's test Octave's pcg() against our method on some easy matrices. +max_iterations = 100000; +tolerance = 1e-11; + +for n = [ 5, 10, 25, 50, 100 ] + A = random_positive_definite_matrix(5, 1000); + + # Assumed by Octave's implementation when you don't supply a + # preconditioner. + x0 = zeros(5, 1); + b = unifrnd(-1000, 1000, 5, 1); + [o_x, o_flag, o_relres, o_iter] = pcg(A, b, tolerance, max_iterations); + [x, k] = conjugate_gradient_method(A, b, x0, tolerance, max_iterations); + + diff = norm(o_x - x); + msg = sprintf("Our CGM agrees with Octave's, n=%d.", n); + unit_test_equals(msg, true, norm(diff) < 1e-10); +end