Firstly, select an initial guess (k = 0) .
Secondly, compute a new (k + 1 = 1).
Thirdly, test for convergence. . Notice that all the xi must pass the test.
If all the xi do not pass the test, then repeat until they do.
The Gauss-Seidel Method hopes to speed up the convergence by using newly computed values of xi at once, as soon as each is available. Thus, in computing xnew(12), for instance, the values of xnew(1), xnew(2), . . ., xnew(11) are used on the right hand side of the formula. We still need to keep separate sets of xnew and xold in order to perform the convergence tests.
% Script to implement Gauss-Seidel
b=[4 1 2;1 3 1;1 2 5];
c=[16;10;12];
xold=[1;1;1];
xnew=xold;
np=size(b);
n=np(1);
flag=1;
while flag > 0
for k=1:n
sum=0;
for l=1:n
if k~=l
sum=sum+b(k,l)*xnew(l);
end
end
xnew(k)=(c(k)-sum)/b(k,k);
end
for k=1:n
if abs((xnew(k)-xold(k))/xold(k)) > 0.0005
xold=xnew;
break
else
flag=0;
end
end
end
xnew
Share with your friends: |