我有一个带有外部确定性输入(控制)和随机分量的常微分方程组。除了初始状态之外,我如何安全地(执行良好的代码样式)通过tf.contrib.integrate.odeint()
将这些额外的输入参数传递给方程函数?如果有这样的方法可以做到这一点。或者在外部范围内定义它们并从方程函数中引用它们是迄今为止唯一的方法吗?
答案 0 :(得分:0)
当我尝试使用odeint求解器模拟Hindmarsh-Rose模型时,我遇到了同样的问题。我想在等式中注入电流,不知道该怎么做。 以下是一个基本的例子:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
## Model parameters
# v' = u - a.v^3 + b.v^2 + I -z
# u' = c - d.v^2 -u
# z' = epsilon_z.(s.(v-v0)-z
#parameters for v and u terms
a = 1.0
b = 3.0
c = -3.0
d = 5.0
v0 = -1.4
s = 4.0
epsilon_z = 0.002
# init tensions
v_init = -3.0
u_init = 0.0
z_init = +0.9
#injected current that is currently fixed
Id = 5
# What I would like to do :
# def I(t):
# if 0.0 <= t < 300.0:
# return 0.0
# elif 300.0 <= t < 1700.0:
# return Iech
# return 0.0
def HR_equation(state, t):
v, u, z = tf.unstack(state)
dv = -a * v*v*v + b * v*v + u - z + I
du = -d * v*v - u + c
dz = epsilon_z * ( s*(v - v0)- z
return tf.stack([dv, du, dz])
init_state = tf.constant([v_init, u_init, z_init], dtype=tf.float64)
t = np.linspace(0, 2000, num=5000)
tensor_state, tensor_info = tf.contrib.integrate.odeint(HR_equation,
init_state, t, full_output=True)
sess = tf.Session()
state, info = sess.run([tensor_state, tensor_info])
v, u, z = state.T
plt.plot(v, u)