How to calculate polynomial roots with a list?

Asked

Viewed 348 times

1

I’m having a hard time with a university job and I’ve come to ask for your help here.

I have two lists of candidate values to be roots of a polynomial.

    listaDivisoresP = []

for i in range(int(p[0])):
    if p[0]%(i+1) == 0:
        listaDivisoresP.append(i+1)

print(listaDivisoresP)

listaDivisoresN = []

for i in range (len(listaDivisoresP)):
    listaDivisoresN.append(-listaDivisoresP[i])

print(listaDivisoresN)

As the code shows, I have a list of the values of the positive candidates and another one of the values of the negative candidates. Now, I wanted to take a polynomial that has its coefficients given by a list. For example, the polynomial:

                                             5x^4+3x^3-2x^2+x-2

You have your list (p) indicated by:

                                             p[] = {5, 3, -2, 1, 2}

With this polynomial, I want to test all the values that are in the lists to find out which result value is zero. When you find a value that results in zero, I want to create a new list (listRaizesInteiras) with the values that zero the polynomial. For that, I tried the following:

listaRaizInteira = []
listaTesteRaizes = []

for i in range(len(listaDivisoresP) - 1):
    contador   = len (p) - 1
    while contador > 0:
        raiz = (listaDivisoresP[i]**(contador)) * p[contador]
        listaTesteRaizes.append(raiz)
        contador = contador - 1
    if sum(listaTesteRaizes)  + p[0] == 0:
        listaRaizInteira.append(listaDivisoresP[i])

However, I’m not getting the list I want. In this case, I only tested for the positive values (using the Software list), because with the negative values (from the Software list) it would be quite similar.

Someone can help me do what I’m trying to do?

1 answer

0

In his example the equation has no real roots.

5x^4+3x^3-2x^2+x-2 => Raízes imaginárias.

So I switched to the equation:

x^6 -3x^5 -6x^4 +10x^3 +21x^2 + 9x  => Raizes: -1, 0, 3

It follows a code that does what you want. It generates two lists of candidate numbers, a list of negative candidate numbers, and a list of positive candidate numbers. It tests all candidates in the function, if the result is zero it means that it has found a root and adds the value in a list of found roots.

# Função que representa o polinômio
# Passar lista de coeficientes e o valor de x
# Retorna o valor do polinômio em x
def funcao(coeficientes, x):
    expoente = len(coeficientes)-1
    resultado = 0
    for c in coeficientes:
        resultado = resultado + c * x ** expoente
        expoente=expoente-1
    return resultado

# LISTA COM CANDIDATOS POSITIVOS
numeros_positivos = []
for i in range(0,100,1):
    numeros_positivos.append(i)

# LISTA COM CANDIDATOS NEGATIVOS
numeros_negativos = []
for i in range(-1,-100,-1):
    numeros_negativos.append(i)

# CRIA LISTA DE COEFICIENTES DA FUNÇÃO
# Equação: 1x^6 -3x^5 -6x^4 +10x^3 +21x^2 + 9x
coeficientes = [ 1, -3, -6, 10, 21, 9, 0  ] 

# TESTA OS RESULTADOS, ARMAZENA RAIZES EM UMA LISTA
raizes = []
for x in numeros_positivos:
    if funcao(coeficientes,x) == 0:
        raizes.append(x)

for x in numeros_negativos:
    if funcao(coeficientes,x) == 0:
        raizes.append(x)

# PRINTA AS RAIZES ENCONTRADAS -1, 0, e 3
print(raizes)

I hope I’ve helped.

  • 1

    Thanks for the help!

Browser other questions tagged

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