0

I have 2 questions:

a) I have initial conditions stated, but would like to for example loop over those initial conditions, increasing S0 by an increment of 5 each time and R0 by an increment of 10 x times (or until i say enough is enough).

b) For each new set of initial conditions in the loop I would like to write the results of the solved ODEs to a txt file rather than plotting them straight after.

How can this be done? Is this the most efficient way to save data from a large set of initial conditions?

Any help would be great as I have not found anything similar online

# 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, 5., 1000)         # time grid

# solve the DEs
soln = odeint(f, y0, t)
S = soln[:, 0]
Z = soln[:, 1]
R = soln[:, 2]

# plot results
plt.figure()
plt.plot(t, S, label='Living')
plt.plot(t, Z, label='Zombies')
plt.xlabel('Days from outbreak')
plt.ylabel('Population')
plt.title('Zombie Apocalypse - No Init. Dead Pop.; No New Births.')
plt.legend(loc=0)
jipes
  • 11
  • 3
  • a) https://wiki.python.org/moin/ForLoop b) https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files – Georgy Mar 11 '18 at 12:30
  • I'm not really sure how to implement a for loop this way, how can I implement it so that the loop continues until I say stop? I can write files, I just don't know how I would go about writing a file for each loop – jipes Mar 11 '18 at 12:37
  • [How to kill a while loop with a keystroke?](https://stackoverflow.com/questions/13180941/how-to-kill-a-while-loop-with-a-keystroke) – Georgy Mar 11 '18 at 12:41
  • Okay I'll try out the while loop, and is there a command line or something that I can use that writes a file for each loop? – jipes Mar 11 '18 at 12:42
  • It is not necessary to use while loop. You still can use for loop as well and wrap it with that try-except construct. – Georgy Mar 11 '18 at 12:44
  • I sent you the link about reading and writing files. Try it out :) There are many other places to read about it as well. – Georgy Mar 11 '18 at 12:46
  • 1
    Oh cool thanks, I'll try to work on it now and get somewhere with the links you gave – jipes Mar 11 '18 at 13:12

0 Answers0