Problem with exponent operator in function

Asked

Viewed 56 times

-4

destaquei o problema e descrevi na equação: 
M = 1000/(10 - Vinc(10)**2/299792458**2)**(1/2)
////////////////////////////////////////////////////////////////////////////////

import matplotlib.pyplot as plt
import numpy as np
import time

#Função de Progreção aritmética com rasão = 10, Retornando em Vf (Velocidade pós aceleração).  
def Vinc(V):
    X = 2
    while(X <= 1):    
        X += 1
        V += 10
        return(V)


leitura =[]
fig, ax = plt.subplots()
contador = 0
eixo_x = 50
while True:           
    ax.clear()
    ax.set_xlim([0,eixo_x])   #faixa do eixo horizontal
    ax.set_ylim([0,1023]) # faixa do eixo vertical

    #O problema está aqui, Vinc(10) não pode ter expoente pois a função não aceita esse operador.
    M = 1000/(10 - Vinc(10)**2/299792458**2)**(1/2) 

    print(Vinc(10),'______',M)
    leitura.append(M)  #teste com numeros aleatorios
    #leitura.append(dados)     
    ax.plot(leitura)
    plt.pause(.000001)     
    contador = contador + 1
    if (contador > eixo_x):
        leitura.pop(0)

**

If anyone can help me, I’d appreciate it! Thank you very much, sincerely: letalboy ;) **#

  • I think your Vinc job is wrong. As you part of X = 2 and increment this X within the loop it will logically never meet the condition (X <= 1). It will probably go into an infinite loop until an overflow occurs. But there’s another problem: you loop but you put Return inside that loop, which doesn’t make sense. Another thing: I believe 299792458**2 will cause overflow. Take a look at the numerical limits you can represent with each type of data in your language.

  • Thank you, I’ll study about it ;)

1 answer

1

Hello, my first question is:

The logic of his Vinc function is right?,

    def Vinc(V):
        X = 2     //você definiu X =  2,
        while(X <= 1):   //você testou se X é <= 1, o que é false pois X = 2
                         // logo você não entra no loop   
            X += 1       // Incrementa x de maneira que irar gerar um loop infinito
            V += 10
            return(V)   //retorno dentro do loop, ou seja, loop só executaria uma vez.

As I think it should be:

def Vinc(V):
    X = 2
    while(X >= 1):    
        X = X - 1
        V += 10
    return(V)

Now answering your question,

One way to solve it is to break your line of code in two, so:

temp =  Vinc(10) 

M = 1000/(10 - temp**2/299792458**2)**(1/2)
  • Thank you, I think it works now! ;)

Browser other questions tagged

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