# odeint: Variable timestep for mathematical modelling

205
March 28, 2018, at 07:34 AM

I am using the Sci Py zombie code as an example.

The problem with ODEint is I am integrating over long times (in this case from 0 to 500, but for other projects up to thousands of years) and it does this integration in one go. I would like to implement a long output timescale, but a small integration timescale and change the number of samples generated to 1, but continuously update the timescale so that I can write a new point to a file per loop. i.e I do not want it to integrate from 0 to 500 in one go, but rather loop over a changing timescale (t_new = t_old + t_delta). Can this be implemented?

``````# zombie apocalypse modeling
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
plt.ion()
plt.rcParams['figure.figsize'] = 10, 8
P = 0      # birth rate
d = 0.0001  # natural death percent (per day)
B = 0.0095  # transmission percent  (per day)
G = 0.0001  # resurect percent (per day)
A = 0.0001  # destroy percent  (per day)
# solve the system dy/dt = f(y, t)
def f(y, t):
Si = y[0]
Zi = y[1]
Ri = y[2]
# the model equations (see Munz et al. 2009)
f0 = P - B*Si*Zi - d*Si
f1 = B*Si*Zi + G*Ri - A*Si*Zi
f2 = d*Si + A*Si*Zi - G*Ri
return [f0, f1, f2]
# initial conditions
S0 = 500.              # initial population
Z0 = 0                 # initial zombie population
R0 = 0                 # initial death population
y0 = [S0, Z0, R0]     # initial condition vector
t  = np.linspace(0, 50, 1000)         # time grid
# solve the DEs
soln = odeint(f, y0, t)
S = soln[:, 0]
Z = soln[:, 1]
R = soln[:, 2]
``````
POPULAR ONLINE

READ ALSO

### Numpy shuffle multidimensional array by row only, keep column order unchanged

How can I shuffle a multidimensional array by row only in Python (so do not shuffle the columns)

457

204

### Dict with same keys names

I need a dictionary that has two keys with the same name, but different valuesOne way I tried to do this is by creating a class where I would put the each key name of my dictionary, so that they would be different objects:

198

### App initializes twice. Is this typical behavior? [duplicate]

This question already has an answer here:

156