3

I'm trying to find the outbreak time (when the number of zombies >= number of humans) as a function of the zombification rate gamma, using the following code:

arrOutbreakT = []
arrGamma = np.linspace(10**(-4), 1, 2001)

for i in range (0, len(arrGamma)):
    gamma = arrGamma[i]
    NstepC = (gamma*10000).astype(int)
    tlistC = np.linspace(iniTime, finalTime, NstepC + 1)
    solC = solve_ivp(gradF, t_span2, ini2, t_eval = tlistC)
    
    k = 0
    klist = np.zeros(NstepC+1)
    while solC.y[1, k] < (solC.y[0, k] / 4):
        k = k + 1
        
    arrOutbreakT.append(tlistC[k])

where

alpha = 0.0 # human natural birth rate
beta = 1.0*(10**(-4)) # human natural death rate
gamma = 9.5*(10**(-3)) # zombification rate
delta = 1.0*(10**(-4)) # zombie destruction rate
epsilon = 1.0*(10**(-4))
iniTime = 0.0
h = 0.01 # step size
Nstep = 500
finalTime = iniTime + Nstep*h # = 5.0 days

H0 = 1000 # original no. of humans
Z0 = 5 # original no. of zombies
D0 = 0 # original no. of dead

ini2 = [H0, Z0, D0]
t_span2 = [time[0], finalTime]

def gradF (time, HZD):
    return [(alpha - beta - gamma*HZD[1])*HZD[0], 
            gamma*HZD[0]*HZD[1] - delta*HZD[0]*HZD[1] + epsilon/HZD[1], 
            beta*HZD[0] + delta*HZD[0]*HZD[1] - epsilon*HZD[2]]

I get this output:

IndexError                                Traceback (most recent call last)
    321 k = 0
    322 klist = np.zeros(NstepC+1)
--> 323 while solC.y[1, k] < (solC.y[0, k] / 4):
    324     k = k + 1
    326 arrOutbreakT.append(tlistC[k])

IndexError: index 2 is out of bounds for axis 1 with size 2

Can someone help?

1
  • where is 'time' defined for 'time[0]'? Commented Nov 12 at 16:48

2 Answers 2

3

You’re running past the end of your solution array — your while loop keeps incrementing k until it exceeds the last index of solC.y.

Use a bounded loop condition instead, e.g.:

k = 0
while k < solC.y.shape[1] and solC.y[1, k] < solC.y[0, k] / 4:
    k += 1

if k == solC.y.shape[1]:
    # outbreak never reached; handle accordingly
    arrOutbreakT.append(np.nan)
else:
    arrOutbreakT.append(tlistC[k])

The IndexError happens because your condition is never met before k exceeds solC.y.shape[1]-1.
Always check k < solC.y.shape[1] in loops like this.

Sign up to request clarification or add additional context in comments.

Comments

0

gradF function outputs an array of 3 elements. solC as you see is an array of 3 elements (see below). After the 3rd loop, and solC.y[1, k] < (solC.y[0, k] / 4) is still True, it goes into k=3 for the 4th loop, but there is no 4th element.

array([[1000.        ,  997.00447058],
      [   5.        ,    5.0001    ],
      [   0.        ,    2.99478029]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.