I have implemented the gauss-seidel method in matlab, with the code at the bottom and I am appling it to a simple problem Ax=b
$\begin {bmatrix} 1 & 2\\ 3 & 4\end{bmatrix}x=\begin {bmatrix} 3 \\ 7\end{bmatrix}$ whose solution is $\begin {bmatrix} 1 \\ 1\end{bmatrix}$
When I call Gauss_Seidel_mat(A,b,[0;0],10^-3,2000)
matlab yields a completely incorrect result and an astronomical value for one of the variables
ans =
1.0e+307 *
8.5597
-Inf
I know the convergence of the method is only garanteed for strictly diagonally dominant or for symmetric and positive-definite matrices, so is that the reason of this result? This means that every time I want to use the G-S method I should implement a code that checks if the matrix is either strictly diagonally dominant or symmetric and positive-definite, right? If is not , I shouldn't use it?
What is the meaning of the * in the matlab output for x and why is it outputting 3 values instead of 2? The solution should have only two components I noticed that the 3 values and the * first appear if I call the function with a kmax >= 17
>> Gauss_Seidel_mat(A,b,[0;0],10^-3,16)
ans =
876.7878
-655.8408
>> Gauss_Seidel_mat(A,b,[0;0],10^-3,17)
ans =
1.0e+03 *
1.3147
-0.9843
Code:
function [x,k,ier] = Gauss_Seidel_mat(A,b,x,tol,kmax)
% Gauss-Seidel
D = tril(A);
C = A-D;
for k = 1:kmax
y = x;
x = D\(b-C*y);
if norm(x-y,inf) <= tol*norm(x,inf)
ier = 0;
return
end
end
ier = 1;
As you mentioned, there are sufficient conditions for the convergence of the Gauss-Seidel method, like the matrix being strictly diagonally dominant or symmetric positive definite. More generally, the method converges for all initial approximations iif $\rho((L+D)^{-1}U)<1$.
However, if the method converges, it converges to the solution of the linear system. For this reason, there is no need to implement a check for the sufficient conditions, if you observe convergence, it is fine.