2014-07-07 15 views
0

我想驗證我的RK4代碼並且有一個狀態空間模型來解決同一個系統。我有一個具有初始條件的14狀態系統,但條件隨時間變化(每次迭代)。我試圖制定A,B,C,D矩陣,並使用syslsim以便在整個時間範圍內編譯我所有狀態的結果。我試圖做類似這樣的:用變量矩陣求解狀態空間響應

for t=1:1:5401 

    y1b=whatever 
    . 
    . 
    y14b = whatever 

    y_0 = vector of ICs 

    A = (will change with time) 
    B = (1,14) with mostly zeros and 3 ones 
    C = ones(14,1) 
    D = 0 

    Q = eye(14) 
    R = eye(1) 

    k = lqr(A,B,C,D) 

    A_bar = A - B*k 

    sys = ss(A_bar,B,C,D) 

    u = zeros(14,1) 

    sto(t,14) = lsim(sys,u,t,y_0) 

    then solve for new y1b-y14b from outside function 

end 

換句話說,我想使用sto(t,14)每次迭代存儲lsim,並與所有我的狀態爲從1到每個時間段的矩陣結束5401我不斷收到此錯誤信息:

Error using DynamicSystem/lsim (line 85) 
In time response commands, the time vector must be real, finite, and must contain 
monotonically increasing and evenly spaced time samples. 

Error using DynamicSystem/lsim (line 85) 
When simulating the response to a specific input signal, the input data U must be a 
matrix with as many rows as samples in the time vector T, and as many columns as 
input channels. 

任何有用的輸入是極大的讚賞。謝謝

回答

1

對於lsim工作,t必須至少包含2點。

另外,翻轉了BC的尺寸。您有1個輸入和1個輸出,從而u應當由1

最後是長度lsim噸的,它看起來像你試圖把所有的聲母條件在一次lsimy_0,你只想部分相關到這個迭代。

s = [t-1 t]; 
u = [0; 0]; 
if t==1 
    y0 = y_0; 
else 
    y0 = sto(t-1,1:14); 
end 
y = lsim(sys, u, s, y0); 
sto(t,1:14) = y(end,:); 

我不確定我是否正確理解了您的問題,但我希望它有幫助。

+0

謝謝你的幫助。我試着按照你所說的去做,但它告訴我最初的條件必須與14的狀態相同。我試着用14步將s從t-1拉伸到t,並且使它的大小爲14告訴我有一個下標不匹配。有任何想法嗎? – user3814258

+0

'y_0'的大小是多少?這聽起來像是'14×1',在這種情況下,我不明白你想要做什麼。 – Simon

+0

這是y_0的正確大小,因爲它是14個狀態的初始條件。我試圖求解x_dot = Ax + Bu和y = Cx + Du,其中狀態嵌入A矩陣的公式中,但也隨時間變化。這意味着我必須爲每個時間步驟循環lsim並且每次都解析A矩陣。最後,我想要一個從1-5401開始的每個時間的所有狀態值的矩陣,我只關心整個數字時間 – user3814258