-
Notifications
You must be signed in to change notification settings - Fork 0
/
gauss_seidel.m
30 lines (20 loc) · 1 KB
/
gauss_seidel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function [x, residual, numItr] = gauss_seidel(A, b, x0,ConvCrit)
% Additional inputs:
rlx=0.1; % Relaxation factor
n=length(x0);
x = x0; xold = x0;
numItr=0;
residual=1;
while residual>= ConvCrit % Continue until the residual is smaler than the convergence criteria
for i=1:n % Loop over gridpoints
I_lhs=[1:i-1]; % left-hand-side values from the diagonal in each row in A matrix
I_rhs=[i+1:n]; % right-hand-side values from the diagonal in each row in A matrix
% Compute solution (x) for each grid point (including relaxation):
x(i) =(1-rlx)*xold(i)+ rlx*(-A(i,I_lhs)*x(I_lhs)/A(i,i) - A(i,I_rhs)*xold(I_rhs)/A(i,i) + b(i)/A(i,i));
end
r= b-A*x; % Equation residual
residual=max(abs(r)); % Maximum value as residual criteria
numItr=numItr+1; % Number of solver iterations
xold=x; % Replace old solution with new
end
end