1 A = [5,1,2; ...
2 1,6,3; ...
3 2,3,7];
5 M = eye(3);
6 b = [1;2;3];
7 x0 = [1;1;1];
9 cgm = conjugate_gradient_method(A, b, x0, 1e-6, 1000);
10 pcgm = preconditioned_conjugate_gradient_method(A, M, b, x0, 1e-6, 1000);
11 diff = norm(cgm - pcgm);
13 unit_test_equals("PCGM agrees with CGM when M == I", ...
14 true, ...
15 norm(diff) < 1e-6);
17 pcgm_simple = simple_preconditioned_cgm(A, M, b, x0, 1e-6, 1000);
18 diff = norm(pcgm_simple - pcgm);
20 unit_test_equals("PCGM agrees with SimplePCGM when M == I", ...
21 true, ...
22 norm(diff) < 1e-6);
24 ## Needs to be symmetric!
25 M = [0.97466, 0.24345, 0.54850; ...
26 0.24345, 0.73251, 0.76639; ...
27 0.54850, 0.76639, 1.47581];
29 pcgm = preconditioned_conjugate_gradient_method(A, M, b, x0, 1e-6, 1000);
30 diff = norm(cgm - pcgm);
32 unit_test_equals("PCGM agrees with CGM when M != I", ...
33 true, ...
34 norm(diff) < 1e-6);
37 pcgm_simple = simple_preconditioned_cgm(A, M, b, x0, 1e-6, 1000);
38 diff = norm(pcgm_simple - pcgm);
40 unit_test_equals("PCGM agrees with Simple PCGM when M != I", ...
41 true, ...
42 norm(diff) < 1e-6);
45 # Test again Octave's pcg() function.
46 max_iterations = 100000;
47 tolerance = 1e-11;
48 C = random_positive_definite_matrix(5, 1000);
49 M = C*C';
51 for n = [ 5, 10, 25, 50, 100 ]
52 A = random_positive_definite_matrix(5, 1000);
54 # Assumed by Octave's implementation when you don't supply a
55 # preconditioner.
56 x0 = zeros(5, 1);
57 b = unifrnd(-1000, 1000, 5, 1);
58 [o_x, o_flag, o_relres, o_iter] = pcg(A, b, tolerance, max_iterations, C, C');