I would like to apply odeint to an array of initial conditions and return a derivative that has the same size as those initial conditions. I could loop over each initial condition, but I think that will be very slow with higher N. The example below is from the docs for odeint. sol_1 works as expected, but sol_2 gives the error ValueError: Initial condition y0 must be one-dimensional.
Does anyone have a clever solution for how to make sol_2 run without just looping over each initial condition? Thanks.
import numpy as np
from scipy.integrate import odeint
def pend(y, t, b, c):
theta, omega = y
dydt = [omega, -b*omega - c*np.sin(theta)]
return dydt
b = 0.25
c = 5.0
t = np.linspace(0, 10, 101)
# 0D initial values, directly from docs
y0_1 = [np.pi - 0.1, 0]
sol_1 = odeint(pend, y0_1, t, args=(b, c))
# 1D initial values, directly from docs
y0_2 = [np.ones((3)) * (np.pi - 0.1), np.zeros((3))]
# Error here
sol2 = odeint(pend, y0_2, t, args=(b, c))
This is essentially the same answer written by OP, but I think it might be a little cleaner. First, I used the
flattenmethod, which isn't as verbose as thenp.ndarray.flattenfunction. Second, I flatten the results to pass to the ODE function, reshape inside the function for extracting values and using, and then flatten the result for the return. At the very end, I usenp.splitto extract each of the solutions.