given the following system: $$\frac{dP}{dt} = \alpha P(1-\frac{P}{K}) - \beta P I$$ $$\frac{dI}{dt} = \beta P I - \rho I$$

how do I solve the system numerically. as when I attempt to solve this is the error I get.

/home/gideon/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:19: RuntimeWarning: >divide by zero encountered in double_scalars

/home/gideon/anaconda3/lib/python3.7/site-packages/scipy/integrate/odepack.py:247: >ODEintWarning: Illegal input detected (internal error). Run with full_output = 1 to get >quantitative information. warnings.warn(warning_msg, ODEintWarning)

here is my code

   x[0]  : P(t). population not infected
   x[1]  : I(t). population Infected
   k     : population carrying capacity
   alpha : growth rate
   rho   : death rate
   beta  : infection rate
   t     : time

t = np.linspace(0,20,50)
fig,ax = plt.subplots(1,figsize = (10,4))
plt.suptitle('Infection Model')

def update_plot(k,alpha,rho,beta):

   xprime = lambda x,k,alpha,rho,beta,t: np.array([alpha*x[0]*(1-(x[0]/k))-beta*x[0]*x[1],
   x0 = np.array([(4/5)*k,k/5])
   x = odeint(xprime,x0,t,args=(k,alpha,rho,beta,))
   y1 = x[:,0]
   y2 = x[:,1]
   ax.plot(t,y1,'b',label = 'P(t)')
   ax.plot(t,y2,'g--',label = 'I(t)')
   plt.legend(loc = 'best')

k = widgets.FloatSlider(min=1,max = 10 , value =1, description = 'K :')
alpha = widgets.FloatSlider(min=1,max = 10 , value =1, description = r'$\alpha$ :')
rho = widgets.FloatSlider(min=1,max = 10 , value =1, description = r'$\rho$ :')
beta = widgets.FloatSlider(min=1,max = 10 , value =1, description = r'$\beta $ :')


I'm trying to plot the system for varying parameters but the graphs I am getting are incorrect. i think it is due to the nan values I'm getting in the solution array $x$


1 Answer 1

xprime = lambda x,k,alpha,rho,beta,t:

is in the wrong argument order, first are the fixed arguments, then the parameters.

xprime = lambda x,t,k,alpha,rho,beta:

I'm not sure where the division-by-zero error originates, it could be a consequence of the solution diverging, doing strange things in the step-size controller.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.