# Gauss Siedel for solving a linear system of equations

4 views (last 30 days)
F.O on 8 Mar 2018
Edited: F.O on 8 Mar 2018
Hi ,
I want to fix this code for Gauss Siedel method for solving a linear system of equations Here we are using some values at index k+1 while computing other values at index k. Gauss siedel formula: https://wikimedia.org/api/rest_v1/media/math/render/svg/57760458e6cf8203f399cb09445a7989c0139cdc i wonder if what did is correct i.e by putting xit(i,iter)=xit(i) in the for loop.
The second thing is how to modify this to SOR method which is https://wikimedia.org/api/rest_v1/media/math/render/svg/57760458e6cf8203f399cb09445a7989c0139cdc x(at index k+1)= (1+w)X(at index k) +w*x(index k+1) but i dont know how to implement this thanks for the help
if true
% code
clear all
close all
A=[4 -1 1;4 -8 1;-2 1 5]
b=[7;-21;15]
xsol=A\b
x0=b./diag(A)
%G_S iteration
tol=10^-6
err=2*tol
maxItr=100;
n=length(b);
maxItr=100;
xit(:,1)=x0;
iter=1
while ((err>tol)&&(iter<maxItr))
iter=iter+1
for i=1:n
xit(i,iter)=xit(i)
xit(i,iter)= (b(i)-A(i,1:i-1)*xit(1:i-1,iter)-A(i,i+1:n)*xit(i+1:n,iter-1))/A(i,i)
end
err=norm(xit(:,iter)-xit(:,iter-1),inf)
xout=xit'
disp(['iter',num2str(iter), ';Error=',num2str(max(err)),' ;solution=',num2str(xout(end,1:end))])
end