Python odeint with array in differential equation

淺唱寂寞╮ 提交于 2020-06-23 13:37:29

问题


I have next first order differential equation (example):

dn/dt=A*n; n(0)=28

When A is constant, it is perfectly solved with python odeint. But i have an array of different values of A from .txt file [not function,just an array of values]

A = [0.1,0.2,0.3,-0.4,0.7,...,0.0028]

And i want that in each iteration (or in each moment of time t) of solving ode A is a new value from array. I mean that: First iteration (or t=0) - A=0.1 Second iteration (or t=1) - A=0.2 and etc from array.

How can i do it with using python odeint?


回答1:


Yes, you can to that, but not directly in odeint, as that has no event mechanism, and what you propose needs an event-action mechanism.

But you can separate your problem into steps, use inside each step odeint with the now constant A parameter, and then in the end join the steps.

T = [[0]]
N = [[n0]]
for k in range(len(A)):
    t = np.linspan(k,k+1,11);
    n = odeint(lambda u,t: A[k]*u, [n0],t)
    n0 = n[-1]
    T.append(t[1:])
    N.append(n[1:])

T = np.concatenate(T)
N = np.concatenate(N)

If you are satisfied with less efficiency, both in the evaluation of the ODE and in the number of internal steps, you can also implement the parameter as a piecewise constant function.

tA = np.arange(len(A));
A_func = interp1d(tA, A, kind="zero", fill_value="extrapolate")
T = np.linspace(0,len(A)+1, 10*len(A)+11);
N = odeint(lambda u,t: A_func(t)*u, [n0], T)

The internal step size controller works on the assumption that the ODE function is well differentiable to 5th or higher order. The jumps are then seen via the implicit numerical differentiation inherent in the step error calculation as highly oscillatory events, requiring a very small step size. There is some mitigation inside the code that usually allows the solver to eventually step over such a jump, but it will require much more internal steps and thus function evaluations than the first variant above.



来源:https://stackoverflow.com/questions/60216624/python-odeint-with-array-in-differential-equation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!