- % This creates a diagonal matrix from the diagonal entries of A.
- M = diag(diag(A));
- N = M - A;
-
- % Avoid recomputing this each iteration.
- b_norm = norm(b);
-
- k = 0;
- xk = x0;
- rk = b - A*xk;
-
- while (norm(rk) > tolerance*b_norm && k < max_iterations)
- x_next = M \ (N*xk + b);
- r_next = b - A*x_next;
-
- k = k + 1;
- xk = x_next;
- rk = r_next;
- if (save_residual_norms)
- residual_norms = [ residual_norms; norm(rk) ];
- end
+ M_of_A = @(A) diag(diag(A));
+ if (nargout > 2)
+ [x, iterations, residual_norms] = ...
+ classical_iteration(A, b, x0, M_of_A, tolerance, max_iterations);
+ else
+ [x, iterations] = ...
+ classical_iteration(A, b, x0, M_of_A, tolerance, max_iterations);