我嘗試使用 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)
我需要如何調整系統定義才能使其正常作業?
uj5u.com熱心網友回復:
根據檔案,動態的簽名應該dynamics(t, x)以標量t作為第一個引數。這就是scipy.integrate.solve_ivp呼叫給定動態的方式。
在您的情況下,錯誤是由矩陣A乘以標量引起的t,錯誤訊息Input operand 1 does not have enough dimensions表明矩陣乘法出錯。
因此,解決方案是將引數切換為dynamics(t, x)。只要您的矩陣不依賴于時間,dynamics您就可以在內部忽略該引數。tA
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/372118.html
