Advertisement
Guest User

Onde é erro

a guest
Apr 3rd, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.56 KB | None | 0 0
  1. import os
  2.  
  3. #criar uma função que apresente um menu
  4.  
  5. def menu():
  6.     os.system('clear')
  7.     print ('MENU')
  8.     print ('1. Ler Aquivo e Resolver Sistema')
  9.     print ('2. Sair do Programa')
  10.     op = input()
  11.     os.system('clear')
  12.     return op
  13.  
  14. #função que mostre na tela uma matriz
  15.  
  16. def mostraMatriz(mat):
  17.     for i in range(len(mat[i])):
  18.         for j in range(len(mat[i])):
  19.             if mat[i][j] >= 0:
  20.                 print ('%3.5f') % (mat[i][j]),
  21.  
  22.            else:
  23.                    
  24.                 print ('3.5f') % (mat[i][j]),
  25.                 print '\n'
  26.  
  27. #função para ler a matriz
  28.  
  29. def le_matriz(arq):
  30.     return [[float(x) for x in line.split()] for in arq]
  31.  
  32. #avaliando se a matriz pode ser resolvida
  33.  
  34. def avaliaMatriz(mat):
  35.     j = 0
  36.     av = 1
  37.     for i in range(len(mat)):
  38.  
  39.         while j < i:
  40.             if mat[i][j] != 0.0:
  41.                 av = 0
  42.                 j = j + 1
  43.             if av == 1:
  44.                 if mat´[i][j] == 0.0:
  45.                     av = -1
  46. return av
  47.  
  48. #função que resolve quando a matriz é triangular
  49.  
  50. def resolverTri(mat):
  51.     n = len(mat):
  52.         n = len(mat) - 1
  53.         res = []
  54.  
  55.         for i in range (len(mat)):
  56.             j = len(mat) - 1
  57.             x = []
  58.             r = 0.0
  59.             while (mat[n][j] != 0):
  60.                 x.append(mat[n][j])
  61.                 j = j - 1
  62.                 if j < 0:
  63.                     break
  64.                 if len(x) == 1:
  65.                     res.append(mat[n][-1]/ x[0])
  66.                         else:
  67.                             i = 0
  68.                             for i in range(len(res)):
  69.                                 r = x[i] * res[i] + r
  70.                             res.append((mat[-n][-1] - r) / x[-1])
  71.                         n = n - 1
  72. return res
  73.  
  74. #função que acha o maior número entre os valores da coluna passada
  75.  
  76. def achaMaior(mat, i, j):
  77.     maior = 0
  78.     maiorIndice = i
  79.     while i < len(mat):
  80.         if abs (mat[i][j]) > maior:
  81.             maior = mat[i][j]
  82.             maiorIndice = i
  83.         i += 1
  84.  
  85. return maiorIndice
  86.  
  87. #função que resolve a matriz pelo método de Gauss
  88.  
  89. def resolveGauss(mat):
  90.     for j in range(len(mat[0]) - 1):
  91.         i = j
  92.         maior = achaMaior(mat, i, j)
  93.         aux = mat[i]
  94.         mat[i] = mat[maior]
  95.         mat[maior] = aux
  96.         j1 = j
  97.         div = mat[i][j]
  98.         while j1 < len(mat[i]):
  99.             mat[i][j1] = mat[i][j1] / div
  100.             j1 += 1
  101.         i += 1
  102.         while i < len(mat):
  103.             mul = mat[i][j]
  104.             for j1 in range (len(mat[0])):
  105.                 mat[i][j1] -= mat[j][j1] * mul
  106.             i += 1
  107.  
  108.             print ('\nMatriz apos pivotacao\n')
  109.             mostraMat(mat)
  110.             r = resolveTri(mat)
  111. return r
  112.  
  113. #resolvendo o sistema
  114.  
  115. def main():
  116.     """Le earquivos contendo matrizes de sistemas lineares e os resolve"""
  117.  
  118.     while True:
  119.         if menu():
  120.             nome_arq = raw_input(
  121.         'Entre com o nome do arquivo de dados\n=>')
  122.             arq = open(nome_arq, "r")
  123.             mat = le_matriz(arq.readlines())
  124.  
  125.             print 'Matriz extendida do sistema\n'
  126.             mostraMat(mat)
  127.  
  128.             aval = avaliaMat(mat)
  129.             if aval == 1:
  130.                 print ('Sistema Indeterminado')
  131.                 raw_input()
  132.                 continue
  133.  
  134.             else:
  135.  
  136.                 r= resolveGauss(mat)
  137.             for i in range(len(r)):
  138.                 print ('x%d=%5.5f') % (i + 1, r[-(i + 1)])
  139.  
  140.                 else:
  141.                     exit()
  142.                 raw_input()
  143. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement