首页 > 解决方案 > 使用时间数组输入求解 ode - PYTHON

问题描述

我正在尝试解决这个问题,搜索堆栈溢出并找不到答案。

我想整合一组状态方程,其中我想作为参数传递的输入是与 t 长度相同的数组。

时不变参数的示例:

# state function
def state(x, t, u_in):
    u     = x[0]
    v     = x[1]
    w     = x[2]
    phi   = x[3]
    theta = x[4]
    psi   = x[5]
    h     = x[6]

    ax   = u_in[0]
    ay   = u_in[1]
    az   = u_in[2]
    p    = u_in[3]
    q    = u_in[4]
    r    = u_in[5]
    pdot = u_in[6]
    qdot = u_in[7]
    rdot = u_in[8]

    xdot = np.zeros(len(x))
    xdot[0] = ax - g * np.sin(theta) + r * v - q * w
    xdot[1] = ay + g * np.sin(phi) * np.cos(theta) + p * w - r * u
    xdot[2] = az + g * np.cos(phi) * np.cos(theta) + q * u - p * v
    xdot[3] = p + (q * np.sin(phi) + r * np.cos(phi)) * np.tan(theta)
    xdot[4] = q * np.cos(phi) - r * np.sin(phi)
    xdot[5] = (q * np.sin(phi) + r * np.cos(phi)) / np.cos(theta)
    xdot[6] = u * np.sin(theta) - v * np.sin(phi) * np.cos(theta) - w * np.cos(phi) * np.cos(theta)
    return xdot


# initial condition
x0 = np.zeros(7)

# set problem
n = 101
t = np.linspace(0, 10, num=n)
uinp = np.zeros(9)
uinp[0] = 0
uinp[1] = 0
uinp[2] = -g
uinp[3] = 0
uinp[4] = 0
uinp[5] = 0
uinp[6] = 0
uinp[7] = 0
uinp[8] = 0


# solve ODE
x = odeint(state, x0, t, args=(uinp,))

这很好用,因为我的输入是时间不变的。

我想做但不起作用的是将我的 uinp 设置为 np.zeros_like(t) 并解决相同的 ODE。

IE

uinp = np.zeros((n, 9))
uinp[:, 0] = 0
uinp[:, 1] = 0
uinp[:, 2] = -g
uinp[:, 3] = 0
uinp[:, 4] = 0
uinp[:, 5] = 0
uinp[:, 6] = 0
uinp[:, 7] = 0
uinp[:, 8] = 0

我得到这个错误:

ValueError: setting an array element with a sequence.
enter code here

为每个时钟输入使用循环不是一种选择,因为它会产生过多的开销,并且运行此模拟需要很长时间。

感谢您的帮助和见解

标签: pythonarraysinputodeodeint

解决方案


你不能让你的状态函数依赖于时间吗?如果我理解正确,那么这部分:

ax   = u_in[0]
ay   = u_in[1]
az   = u_in[2]
p    = u_in[3]
q    = u_in[4]
r    = u_in[5]
pdot = u_in[6]
qdot = u_in[7]
rdot = u_in[8]

应该取决于时间t

ax   = u_in[t, 0]
ay   = u_in[t, 1]
az   = u_in[t, 2]
p    = u_in[t, 3]
q    = u_in[t, 4]
r    = u_in[t, 5]
pdot = u_in[t, 6]
qdot = u_in[t, 7]
rdot = u_in[t, 8]

t您可能需要找到一种将输入映射float到 u_in 中相应列的方法


推荐阅读