I've got a function and some arguments in a python script but I want to change the script so that when it is called from the command line, new arguments can be entered. I know I need to use sys module but I haven't worked much with it before and I'm having trouble. This is the original code that I want to recreate with arguments from the command line for r, a, z and e:
import scipy as sc
import scipy.integrate as integrate
def dCR_dt(pops, t=0):
R = pops[0]
C = pops[1]
dRdt = r * R - a * R * C
dCdt = -z * C + e * a * R * C
return sc.array([dRdt, dCdt])
r = 1
a = 0.1
z = 1.5
e = 0.75
t = sc.linspace(0, 15, 1000)
R0 = 10
C0 = 5
RC0 = sc.array([R0, C0])
pops, infodict = integrate.odeint(dCR_dt, RC0, t, full_output = True)
And this is what I've tried to do so far:
import scipy as sc
import scipy.integrate as integrate
import sys
def main(r, a, z, e):
def dCR_dt(pops, t=0):
R = pops[0]
C = pops[1]
dRdt = r * R - a * R * C
dCdt = -z * C + e * a * R * C
return sc.array([dRdt, dCdt])
t = sc.linspace(0, 15, 1000)
R0 = 10
C0 = 5
RC0 = sc.array([R0, C0])
pops, infodict = integrate.odeint(dCR_dt, RC0, t, full_output = True)
if (__name__ == "__main__"):
status = main(sys.argv)
sys.exit(status)
When I try and run it in command line with 4 new arguments I get the following error:
Traceback (most recent call last): File "practisesys.py", line 31, in pops, infodict = integrate.odeint(dCR_dt, RC0, t, full_output = True) NameError: name 'dCR_dt' is not defined
I'm new to coding and want to learn more about how to use the sys module to modify scipts. Any help is much appreciated! Thank you and I hope everyone's having a nice weekend :)