How do I make my matrix scheduling algorithm work in python?

Asked

Viewed 80 times

-2

I need to make a program in python that reads a matrix A nxn and a vector B and then solve the system Ax=B, the function q I am using for the first part is

    global n
n= int(input('Qual será a dimensão da matriz A nxn: '))
print(f' "A" será uma matriz [{n}x{n}]')
global matrizA
matrizA = []
global i
global j
for i in range(0,n):
    matrizA.append(list())
for i in range(0,n):
    for j in range(0,n):
        matrizA[i].append(list())
for i in range(0,n):
    for j in range(0,n):
        matrizA[i][j].append(float(input(f'Elemento [{i+1}x{j+1}]: ')))
print()
print('='*24)
print('Matriz A'.center(24))
print('='*24)
print()
for i in range(0,n):
    print(f'|{matrizA[i]}|')
print()        
ask= input('Os valores estão corretos? ')
if ask==('nao'):
    print('Refaça a matriz A: ')
    LerMatriz()
              
elif ask==('não'):
    print('Refaça a matriz A: ')
print('='*24)
print('='*24)
print()
print(f'A matriz B será [{n}x1]')
h=1
global matrizB
matrizB = []
for i in range(0,n):
    matrizB.append(list())
for i in range(0,n):
    for j in range(0,h):
        matrizB[i].append(list())
for i in range(0,n):
    for j in range(0,h):
        matrizB[i][j].append(float(input(f'Elemento [{i+n}]: ')))
print()
print('='*24)
print('Matriz B'.center(24))
print('='*24)
print()
for i in range(0,n):
    print(f'|{matrizB[i]}|')
            
ask= input('Os valores estão corretos? ')
if ask==('nao'):
    print('Refaça a matriz B: ')
    LerMatriz()
                  
elif ask==('não'):
    print('Refaça a matriz B: ')  

Already the algorithm for elimination is like this:

def pivoteamento():

for k in range(1, n-1):
    #pivoteamento
    pivo=matrizA[k][k]
    lpivo=k
    for i in range(k+1,n):
        if matrizA[i][k]>pivo:
            pivo= matrizA[i][k]
            lpivo=i
    if pivo==0:
        print('A matriz A é singular.')
        quit()
    elif lpivo !=k:
        for j in range(1,n):
            troca=matrizA[k][j]
            matrizA[k][j]=matrizA[lpivo][j]
            matrizA[lpivo][j]=troca
        troca=matrizB[k]
        matrizB[k]=matrizB[lpivo]
        matrizB[lpivo]=troca

    #eliminação
    for i in range(k+1,n):
        m=matrizA[i][k]/matrizA[k][k]
        matrizA[i][k]=0
        for j in range(k+1,n):
            matrizA[i][j]=matrizA[i][j]-m*matrizA[k][j]
        matrizB=matrizB[i]-m*matrizB[k]

And then I created a menu printing everything, I don’t know how to print a well formatted Ax=B system and make the algorithm work, can anyone help me? Detail: I can’t import anything from the library according to the teacher, only Math.

1 answer

0

Voce wants help with factoring with partial pivoting?

whatever you need, search and try to implement some solution and bring the error, then we can help find alternatives that work but here is no place to solve a whole code or project

  • sorry... I created an algorithm to scale any AX=B system and run the solution set, but it does not want to run lists and returns me this: Typeerror: float() argument must be a string or a number, not 'list'

  • got it, you can’t float in the list directly, try using the map to call the float for each thing in the list, I’ll try to exemplify: matrizA[i][j]. append(map(float(input(f'Element [{i+1}x{j+1}]: '))))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.