I’m trying to solve multiple ordinary differential equations in Python using odeint
. A simple version of this would look like this:
def lotka_volterra(xy, t):
x, y = xy
dxdt = x - x * y
dydt = x * y - y
return [dxdt, dydt]
This can then be solved as follows:
x0 = 40 #Initial prey population
y0 = 9 #Initial predator population
initial_conditions = [x0, y0]
t = np.linspace(0, 300, 4000)
solution = odeint(lotka_volterra, initial_conditions, t)
However, I have a more complicated system where I need to generate variables within the function and then write the DEs.
I have come up with the following code:
def setofequations(variables, t):
species = []
ucarrier = []
R = []
O = []
for i in range(1, nspecies + 1):
name = 'N_' + str(i)
species.append('N_' + str(i))
for i in range(1, nreactions+1):
R.append('R'+str(i))
O.append('O'+str(i))
ucarrier.append('ucarrier_' + str(i))
Here, every element of every list species
, ucarrier
, R
, I
is a variable that has its own ODE that needs to be solved.
I want to be able to make each string in those lists a variable that can then be used in equations. Every solution I can find such as globals()
, locals()
doesn’t seem to work for my specific requirement – they all have you outright assign some value.
Any help is appreciated, thanks!