我尝试使用Python来模拟LTI系统。结构为xDot = Ax。我将系统动力学定义为一个函数,然后使用solve_ivp
调用它。调用函数本身可以工作,但是模拟系统会导致以下错误
ValueError: matmul: Input operand 1 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)
对于直线与矩阵乘法在系统动力学中的定义。下面是密码。
import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt
# System Parameters
l = 1 # Pendulum length
g = 9.81 # gravity
# Initial Conditions
x0 = np.array([np.pi/2, 0])
# Simulation parameters
tStart = 0
tEnd = 4
t_span = [tStart, tEnd]
t = np.linspace(tStart,tEnd,10) # Simulation time
# System matrices
A = np.array([[0, 1],[-g/l, 0]])
def dynamics(x, t):
xdot = -A@x
return xdot
y = integrate.solve_ivp(dynamics, t_span, x0)
我需要如何调整系统定义才能使其工作?
发布于 2021-12-02 14:16:49
根据文档,动力学的签名应该是dynamics(t, x)
,标量t
作为第一个参数。这就是scipy.integrate.solve_ivp
如何调用给定的动态。
在您的示例中,错误是由矩阵A
与标量t
相乘造成的,而错误消息Input operand 1 does not have enough dimensions
表示矩阵乘法出错。
因此,解决方案是将参数切换到dynamics(t, x)
。在dynamics
中,只要矩阵A
不依赖于时间,就可以忽略参数t
。
https://stackoverflow.com/questions/70199622
复制相似问题