How to fix 'RuntimeWarning: divide by zero encountered in double_scalars'

34,356

Those are not actual errors, but warnings. They are there because you are trying to divide something by zero.

Namely, you are setting M[0] = 0 and then dividing by M[0] (at first iteration, where m = 0) and same for P[0].

The question is, what do you want the first values to be?

Maybe a solution would be initialize the zero values as you wish (before the loop) and start the loop with m=1 (so using for m in range(1,n+1):). Is that what you needed?

However, the warning may still be there if at different iterations P[m] or M[m] will be zero.

T_max = 0.5 * v_PM(Me) * RTOD 
DT = (90 - T_max) - np.fix(90 - T_max) 
n = int(T_max * 2)

P = np.zeros((n+1))    
T = np.zeros((n+1))
M = np.zeros((n+1))
RR = np.zeros((n+1))
LR = np.zeros((n+1))
SL = np.zeros((n+1))

# initialize your values
M[0] = 0 # actually not needed
P[0] = 0 # actually not needed
RR[0] = 0 # actually not needed
LR[0] = 0 # actually not needed
SL[0] = 0 # actually not needed
T[m] = (DT) * DTOR

for m in range(1,n+1):
    T[m] = (DT + m) * DTOR
    #Mach from T[i] using T[i] = v_PM (FALSE POSITION)
    func = lambda x: T[m] - v_PM(x) 
    M[m] = brentq(func, 1, Me+1) 
    P[m] = TR * np.tan(T[m]) #X-AXIS POINTS

    #RR SLOPES
    RR[m] = -TR / P[m]

    #LR slopes
    LR[m] = (np.tan(T[m] + np.arcsin(1 / M[m]))) 
    SL[m] = -RR[m] 

Share:
34,356
macaroni
Author by

macaroni

Updated on July 09, 2022

Comments

  • macaroni
    macaroni almost 2 years

    After doing for loop in my code, there is an error in one line:

    RuntimeWarning: divide by zero encountered in double_scalars

    and in another line two errors:

    RuntimeWarning: divide by zero encountered in double_scalars

    RuntimeWarning: invalid value encountered in arcsin

    I found it might be problem with float values, but im not sure about it and im typing it here for looking yours help. :)

    Here is the code:

    T_max = 0.5 * v_PM(Me) * RTOD 
    DT = (90 - T_max) - np.fix(90 - T_max) 
    n = int(T_max * 2)
    
    P = np.zeros((n+1))    
    T = np.zeros((n+1))
    M = np.zeros((n+1))
    RR = np.zeros((n+1))
    LR = np.zeros((n+1))
    SL = np.zeros((n+1))
    
    for m in range(n+1):
        T[m] = (DT + m) * DTOR
        func = lambda x: T[m] - v_PM(x) 
        M[m] = brentq(func, 1, Me+1) 
        M[0] = 0
        P[m] = TR * np.tan(T[m]) #X-AXIS POINTS
        P[0] = 0
        RR[m] = -TR / P[m]
        RR[0] = 0
        LR[m] = (np.tan(T[m] + np.arcsin(1 / M[m]))) 
        LR[0] = 0
        SL[m] = -RR[m] 
        SL[0] = 0 
    

    I defined earlier (before loop) v_PM and it's equal

    v_PM = lambda x : (A * np.arctan(np.sqrt(B * (x ** 2 - 1))) - np.arctan(np.sqrt(x ** 2 - 1)))

    As i mentioned, error RuntimeWarning: divide by zero encountered in double_scalars refer to RR[m] = -TR / P[m]

    and two errors RuntimeWarning: divide by zero encountered in double_scalars and

    RuntimeWarning: invalid value encountered in arcsin refers to:

    LR[m] = (np.tan(T[m] + np.arcsin(1 / M[m])))

    I put there also P[0] = 0 and M[0] = 0 etc. because i want to have first element as 0.

    I dont know if i can forget about this error and move on with my code or if it is some serious problem. Thanks for your help.