2017-01-14 44 views
0

我想繪製步進響應。我知道我可以使用狀態空間方程的階躍函數,但我試圖使用繪圖函數得到相同的結果。這裏是我的代碼示例:繪製沒有使用步進功能的步進響應

for i=1:201 
    u(i) = 1; 
    x1(i+1) = (-(b/J)*x1(i) + (K/J)*x2(i)); 
    x2(i+1) = (-(K/L)*x1(i) - (R/L)*x2(i) + (1/L)*u(i)); 
    y(i) = x1(i); 
end 

,這是狀態空間方程:

A = [-b/J K/J 
    -K/L -R/L]; 
B = [0 
    1/L]; 
C = [1 0]; 
D = 0; 

如果我這樣做:

t = 0:1:200; 
plot(t, y) 

它不工作,我想有相同的結果,如下面的步驟功能:

sys = ss(A,B,C,D); 
step(sys) 

你可以找到我的狀態空間方程here

回答

1

不匹配的原因是sys是一個連續時間模型,而y的計算將其視爲一個離散時間系統。

以下是估計離散時間域中的連續時間系統的階躍響應的方式:

% Given from the problem statement 
A = [-b/J K/J 
    -K/L -R/L]; 
B = [0 
    1/L]; 
C = [1 0]; 
D = 0; 

% this is your continuous-time model 
sys = ss(A,B,C,D); 

% define the sample rate of the equivalent discrete-time model 
Ts = 1/10; 
% this needs to be something smaller than the time-constants in your model, 
% so that you have enough resolution to represent the continuous-time 
% signal. 

% convert the system to the equivalent discrete-time model 
sysd = c2d(sys,Ts); 

% define how long a step response you'd like to compute 
T = 7; 
% this should be long enough to cover the length of the step response 


t = 0:Ts:T; % time-grid for the plot 
nSmp = length(t); % total number of samples to be computed 

% initializations 
y = NaN(1, nSmp); % output vector 
u = ones(1, nSmp); % unit step input 
X = [0; 0]; % state vector, initialized to 0 

% compute the samples of the step-response 
% (i prefer to use vectorized form to keep the code concise) 
for i=1:nSmp 
    y(i) = sysd.C * X + sysd.D * u(i); 
    X = sysd.A * X + sysd.B * u(i); 
end 

% plot continous-time step response 
figure; 
step(sys); 

% plot simulated discrete-time step response 
figure; 
plot(t, y, 'r') 
xlabel('Time (s)'); 
ylabel('Amplitude'); 
title('Simulated Step Response'); 
+0

非常感謝你,這是偉大的。接下來我需要添加PID控制器。你能告訴我如何得到錯誤,intagretion錯誤和導數錯誤?我需要這3個值通過遺傳alghorithm生成PID參數 – Masaj

+0

@Masaj:恐怕我可能無法正確回答這個問題。你最好把它作爲一個新的問題發佈,這樣別人就可以。 – aksadv

+0

我已經問過新的。再次感謝您的幫助 – Masaj