-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetodoGauss.py
More file actions
88 lines (73 loc) · 2.18 KB
/
MetodoGauss.py
File metadata and controls
88 lines (73 loc) · 2.18 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
Implementação do método de Gauss
Para resolução de sistemas de equações lineares
"""
def matriz (n):
""" Função que faz a criação da Matriz """
x=[0]*n
for i in range (0, n):
x[i] = [0]*n
for i in range(0, n):
for j in range (0, n):
x[i][j] = 0.0
return x
def vetor (n):
""" Função que cria um vetor de tamanho N """
x=[0]*n
for i in range(0, n):
x[i]=0.0
return x
def substituicaoret (n, mat, c):
""" Implementação do Método das Substituições Retroativas """
x=vetor(n)
x[n-1] = c[n-1]/mat[n-1][n-1]
for i in range(n-2, -1, -1):
soma=0
for j in range(i+1, n):
soma=soma+mat[i][j] * x[j]
x[i] = ((c[i] - soma)/mat[i][i])
return x
def gauss(n, mat, c):
""" Implementação do Método de Gauss """
for k in range (0, n-1):
for i in range(k+1, n):
m=(-1)*mat[i][k]/mat[k][k]
for j in range (k, n):
mat[i][j] = mat[i][j] + (m*mat[k][j])
c[i] = c[i] + m*c[k]
return substituicaoret(n, mat, c)
def main():
n= int(input("Numero de termos: "))
mat = matriz(n)
c = vetor(n)
print("Insira os termos da matriz:")
for i in range (0, n):
for j in range (0, n):
mat[i][j] = float(input())
print("Insira os termos do vetor independente: ")
for i in range (0, n):
c[i] = float(input())
x = gauss(n, mat, c)
print("[", end=" ")
for i in range (0, n):
print(x[i], end = " ")
print("]")
return
main()
# MATRIZ UTILIZADA PARA TESTE:
# | 1 -3 2 | |X1| | 11 |
# | -2 8 -1 | |X2| = | -15 |
# | 4 -6 5 | |X3| | 29 |
# Resultado esperado: [2, -1, 3]
# | 2 1 -1 | |X1| | 3 |
# | 1 3 2 | |X2| = | 4 |
# | 3 -2 1 | |X3| | -5 |
# Resultado esperado: [0, 2, -1]
# | 1 2 -3 | |X1| | 10 |
# | 2 1 1 | |X2| = | 3 |
# | -3 2 1 | |X3| | -6 |
# Resultado esperado: [2, 1, -2]
# | 1 1 1 | |X1| | 6 |
# | 1 2 2 | |X2| = | 9 |
# | 2 1 3 | |X3| | 11 |
# Resultado esperado: [3, 2, 1]