|
| 1 | +% +linearSolvers/gaussSeidel.m |
| 2 | +function [x, iterations, final_error] = gaussSeidel(A, b, x0, tol, max_iter) |
| 3 | +% Solves Ax = b using the Gauss-Seidel iterative method (matrix form). |
| 4 | + |
| 5 | + n = size(A, 1); |
| 6 | + if nargin < 3 || isempty(x0) |
| 7 | + x = zeros(n, 1); % Initial guess |
| 8 | + else |
| 9 | + x = x0(:); % Ensure column vector |
| 10 | + end |
| 11 | + if nargin < 4 |
| 12 | + tol = 1e-6; % Default tolerance |
| 13 | + end |
| 14 | + if nargin < 5 |
| 15 | + max_iter = 1000; % Default max iterations |
| 16 | + end |
| 17 | + |
| 18 | + % Check for zero diagonal elements |
| 19 | + if any(abs(diag(A)) < eps) |
| 20 | + error('Matrix has zero on the diagonal. Gauss-Seidel requires non-zero diagonal elements for this implementation.'); |
| 21 | + end |
| 22 | + |
| 23 | + D = diag(diag(A)); |
| 24 | + L = tril(A, -1); % Strictly lower triangle |
| 25 | + U = triu(A, 1); % Strictly upper triangle |
| 26 | + |
| 27 | + DL_inv = inv(D + L); % Precompute inverse (can be slow for large matrices) |
| 28 | + % More efficient: use forward substitution in loop |
| 29 | + % But for clarity/simplicity here, precompute. |
| 30 | + |
| 31 | + x_prev = x; |
| 32 | + iterations = 0; |
| 33 | + final_error = inf; |
| 34 | + |
| 35 | + for k = 1:max_iter |
| 36 | + iterations = k; |
| 37 | + % Matrix form: x(k+1) = (D+L)^-1 * (b - U*x(k)) |
| 38 | + x = DL_inv * (b(:) - U * x_prev); |
| 39 | + |
| 40 | + % Check for convergence |
| 41 | + final_error = norm(x - x_prev, inf); |
| 42 | + if final_error < tol |
| 43 | + break; |
| 44 | + end |
| 45 | + x_prev = x; |
| 46 | + end |
| 47 | + |
| 48 | + if iterations == max_iter && final_error >= tol |
| 49 | + warning('Gauss-Seidel method did not converge within %d iterations. Error = %e', max_iter, final_error); |
| 50 | + end |
| 51 | +end |
0 commit comments