2017-01-14 40 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