How to fix ‘RuntimeWarning: divide by zero encountered in double_scalars’

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] 

Leave a Comment