问题
This is my first question on here, so please go easy on me. I'm wondering if there is a way to integrate an ODE system only until a local max of a specified variable is found. Here is some more detail:
Let's call our ODE system dX/dt = F(X) where X(t) = [x1(t), x2(t), ... , xn(t)]
. Assume the solution to this system is attracted to a stable limit cycle C everywhere but at one unstable fixed point p. Choose some initial conditions X0 not p, and not in C. We wish to follow the trajectory of the solution to:
dX/dt = F(X), X(0) = X0 (*)
just until x1(t) reaches its first local max.
Progress: Using scipy.integrate.odeint I am able to find the solution of the limit cycle C and its period T, but starting at an arbitrary point the only way I've been able to solve this problem is by solving (*) for a 'long time' (maybe 4*T) and, assuming this is a long enough time, determine the first local max of x1 after the fact. My larger project requires this to be done many times, so I'm trying to reduce computation time wherever I can. It seems like there has to be a faster way to do this without writing my own ode solver.
It's possible (and likely) that I'm making this too complicated? If you think of a different way to do this I would appreciate any suggestions.
回答1:
I haven't tested this but the following code should work or at least help:
from scipy.integrate import ode
y0, t0 = [1.0j, 2.0], 0
def f(t, y, arg1):
return [1j*arg1*y[0] + y[1], -arg1*y[1]**2]
r = ode(f).set_integrator('zvode', method='bdf')
r.set_initial_value(y0, t0).set_f_params(2.0)
t1 = 10
dt = 1
older = 0
previous = 0
current = 0
while r.successful() and not (older < previous and prevous > current):
older = previous
previous = current
current = r.integrate(r.t+dt)
print("Maximum occurs at {}".format(r.t - dt))
I would also further look into python ode
EDIT:
even better would be to use the solout
which can be found here
来源:https://stackoverflow.com/questions/30597976/can-i-integrate-with-scipys-odeint-until-a-local-max-is-found