-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgaussseidel.py
More file actions
37 lines (29 loc) · 831 Bytes
/
gaussseidel.py
File metadata and controls
37 lines (29 loc) · 831 Bytes
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
31
32
33
34
35
36
37
''' iteracao de gauss-seidel '''
from pylab import *
# dou a matriz quadrada de coeficientes e vetor independente
A=matrix('4 2 0.6; 2 5.5 3; 1 0 2.5')
b=matrix('1. ; 3.; 1.')
def iterGS(x, A, b):
xold=x.copy()
L=list(x)
for i in range(len(A)):
soma = 0.
for j in range(len(A)):
if (j != i): soma += -A[i,j]*L[j]
L[i] = (b[i] + soma)/A[i,i]
return L
def GaussSeidel(x0,A,b,eps,n):
IterGauss=0
dx=10.
while ((IterGauss<=n)&(dx>eps)):
xold = x0.copy()
L=iterGS(x0, A, b)
for k in range(len(L)) : L[k]=float(L[k])
x0=matrix(L).T
IterGauss += 1
dx = norm(x0-xold)
return x0 , IterGauss, dx
########## Teste com um exemplo ##############
x0=matrix(' 0; 0; 0 ')
sol = GaussSeidel(x0,A,b,0.0000001, 100)
print sol