Gauss-Seidel
% Parametros: A, b, X inicial, convergencia, tolerancia
function x = gauss_seidel(A, b, xi, conv, tol)
tic
ox = xi;
tx = ones(1,length(xi));
k=0;
while max(abs(A*tx' - b)) > tol
sprintf('Iteracao %d... variacao = %0.5g',k, abs(max(ox - tx) / max(tx)))
sprintf('Maoir erro = %15.5f', max(abs(A*tx' - b)))
ox=tx;
for i=1:length(b)
soma1=0;
for j=1:i-1
soma1=soma1 + A(i,j)*tx(j);
end
soma2=0;
for j=i+1:length(b)
soma2=A(i,j)*ox(j);
end
tx(i) = (b(i) - soma1 - soma2) / A(i,i);
end
k=k+1;
if abs(max(ox - tx) / max(tx)) > conv
disp('Algoritmo nao converge!');
break
end
if max(abs(A*tx' - b)) > tol
if abs(max(ox - tx) / max(tx)) == 0
disp('Algoritmo travou!');
break
end
end
end
if max(abs(A*tx' - b)) < tol
sprintf('Resolvido na iteracao %d', k)
end
toc
erro = (A* tx' - b)'
x = tx;
x_inversao = (inv(A)*b)'