2016-07-23 41 views
0

我試圖用odeint模擬狀態空間形式的動態系統。 我的A矩陣是12 * 12,B矩陣是12 * 4(行*列),所以我的初始狀態向量是12 * 1,正如它所暗示的。Python odeint-初始條件y0必須是一維

我的代碼如下

import numpy as np 
from scipy.integrate import odeint 

tmp = np.loadtxt("system.txt", skiprows =2) 

A=np.matrix(tmp)[0:12,0:12] 
B=np.matrix(tmp)[0:12,12:] 

control = np.matrix([[0.0],[0.0],[-0.6310],[0.0]]) 

def aircraft(state, t): 
    return A*state + B*control 

state0 = np.array([[6.809827628],[0.439572153],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0]]) 
t = np.arange(0.0, 10.0, 0.1) 

state = odeint(aircraft, state0, t) 

我收到此錯誤

Traceback (most recent call last): 
    File "sim.py", line 17, in <module> 
    state = odeint(aircraft, state0, t) 
    File "/home/aluminium/anaconda2/lib/python2.7/site-packages/scipy/integrate/odepack.py", line 215, in odeint 
    ixpr, mxstep, mxhnil, mxordn, mxords) 
ValueError: Initial condition y0 must be one-dimensional. 

我能想到的定義狀態向量的唯一方法是爲列向量。 你能告訴我如何定義一個初始狀態向量來克服這個問題嗎?

非常感謝。

回答

0

我已經得到了答案,並想到在這裏分享它,希望未來有同樣問題的人能夠受益。

odeint似乎期望1d數組作爲狀態變量來調用函數,並從函數中返回1d數組。

所以我重新塑造了相應的變量。這裏是代碼。

def aircraft(state, t): 
    xdot= A*state.reshape(12,1) + B*control 
    return np.squeeze(np.asarray(xdot)) 

state0 = np.array([6.809827628,0.439572153,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]) 

乾杯

相關問題