# Método de Gauss-Seidel

Sass<-function(A){ # Teste de convergêncial de Sassenfeld
n=length(A[1,])
b=matrix(1,n,1)
for ( i in 1:n){
b[i]=sum(abs(b[-c(i)])*abs(A[i,-c(i)]))/abs(A[i,i])
}
return(b)
}

GSeidel<-function(A,b,erro){  # Gauss-Seidel
m=length(b)
Erro=erro+1

c=0*v
B=0*A

for ( i in 1:m){
c[i]=b[i]/A[i,i]
B[i,]=A[i,]/A[i,i]
B[i,i]=0
}

x=0*b
cont=1
while (Erro>=erro){
y=x
mm=1:m
for (i in mm){
p=0
for ( j in mm[-c(i)]){
p=p+B[i,j]*x[j]
}
x[i]=c[i]-p
}
cont=cont+1
Erro=sum(abs(x-y))

if (cont>=10^6){Erro=0}}
x
}

# ------------------------------------------Um exemplo 

c=c(8, 1, 1, 0, 2, 20, 9, 2, 1, 1, 2, 1, 11,2, 0, 3, 2, 1, -8, 1, 2, 1, 2, 3, 10)
A=matrix(c,5,5,byrow=TRUE); A   # Entrada da matriz A
v=c(1,2,3,4,5); v               # Entrada do vetor v

b=Sass(A);max(b)                # Teste de convergência teórica
x=GSeidel(A,v,10^(-16))         # Gauss-Seidel
x                               # Solução aproximada     
A%*%x-v                         # Teste de erro na aproximação

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: