Reputation: 11
I am encountering trouble while attempting to achieve the same response as Simulink using MATLAB code.
Here is my MATLAB code:
The system contains only a pid and state-space model
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');
I receive an error which states:
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.
How can I solve this issue?
Upvotes: 1
Views: 70
Reputation: 106
One solution is to transform your system to transfer function representation using the rule G(s) = C * (sI-A)^-1 *B + D
sys_tf = C*(inv(s*eye(4)-A))*B+D;
The full code is:
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');
Note: In your system there's no effect of x1
, no state variable or output depends on it
Upvotes: 0