不使用阶跃函数绘制阶跃响应

Plot step response without using step function

我想绘制阶跃响应。我知道我可以将步进函数与状态 space 方程一起使用,但我尝试使用 plot 函数获得相同的结果。这是我的代码示例:

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

这是状态 space 方程:

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)

你可以找到我的状态 space 方程 here.

不匹配的原因是sys是连续时间模型,而y的计算将其视为discrete-time系统。

以下是估计 discrete-time 域中 continuous-time 系统的 step-response 的方法:

% 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');