我在尝试使用 MATLAB 代码实现与 Simulink 相同的响应时遇到麻烦。
这是我的 MATLAB 代码:
该系统仅包含 pid 和状态空间模型
M = 1.5;
m = 0.5;
l = 10;
g = 9.8;
%% state space
A = [0 1 0 0; 0 0 m*g/M 0; 0 0 0 1; 0 0 g/(M*l)*(M+m) 0];
B = [0; 1/M; 0; 1/(M*l)];
C = [0 0 1 0];
D = 0;
sys_ss = ss(A, B, C, D);
% pid
Kp = 99;
Ki = 49;
Kd = 29;
Gc = pid(Kp, Ki, Kd);
% feedback closed-up system
sys_fb = feedback(Gc*sys_ss, 1);
% simulation time and pid reference input value '0'
t = 0:0.01:10;
r = zeros(size(t));
% x0 = [2; 10; -5; 0]
[y, t] = lsim(sys_fb, r, t, [2; 10; -5; 0]);
% plot
figure();
plot(t, y);
title('PID Controller + State Space Model Closed-Loop System Output');
xlabel('Time (s)');
ylabel('Output');
我收到一条错误,其中指出:
Cannot simulate response with initial condition for models with singular E matrix. Use the "isproper" command to check if the model is proper and to extract an equivalent explicit representation.
如何解决这个问题?
一种解决方案是使用规则
G(s) = C * (sI-A)^-1 *B + D
将系统转换为传递函数表示
sys_tf = C*(inv(s*eye(4)-A))*B+D;
完整代码为:
M = 1.5;
m = 0.5;
l = 10;
g = 9.8;
%% state space
A = [0 1 0 0; 0 0 m*g/M 0; 0 0 0 1; 0 0 g/(M*l)*(M+m) 0];
B = [0; 1/M; 0; 1/(M*l)];
C = [0 0 1 0];
D = 0;
% sys_ss = ss(A, B, C, D);
s = tf('s');
sys_tf = C*(inv(s*eye(4)-A))*B+D;
% pid
Kp = 99;
Ki = 49;
Kd = 29;
Gc = pid(Kp, Ki, Kd);
% feedback closed-up system
sys_fb = feedback(Gc*sys_tf, 1);
% simulation time and pid reference input value '0'
t = 0:0.01:10;
r = ones(size(t));
[y, t] = lsim(sys_fb, r, t);
% plot
figure();
plot(t, y);
title('PID Controller + State Space Model Closed-Loop System Output');
xlabel('Time (s)');
ylabel('Output');
注意:在您的系统中,没有
x1
的影响,没有状态变量或输出依赖于它