# How to use lsim in matlab for a closed loop system

I have designed a system using a minimum realization.

I have the A, B, C matrices and I have assigned closed loop poles for the system.

I used the place command to find my K matrix.

I am wondering if it is possible to use the lsim matlab command to simulate my closed loop system (A-B*K)?

$$x' = Ax+Bu$$ $$u=-Kx$$ $$x' = (A-BK)x$$ $$y=Cx$$

All examples I have found for lsim use the system transfer function or the open loop system.

I have seen examples such as this:

t = 0:0.01:10;         % simulation time = 10 seconds
u = zeros(size(T));    % no input
x0 = [0.1 0.1 0.1];    % initial conditions of the three states
sys= ss(A,B,C,D);
lsim(sys,u,t,x0)


But I am not sure how to use this with the closed loop (A-B*K) system.

Any help would be appreciated! Thank you.

Yes, you can using the feedback command. However, if your open-loop system is defined by your state-space system sys= ss(A,B,C,D), then your closed-loop system would be defined as follows (and not by A-B*K as you suggest):

CL_loop = feedback(sys,K); % K is your controller/compensator, this needs to be an LTI object
lsim(CL_loop,u,t,x0); % you might have additional states due to the feedback loop, and so would need additional IC


Note that you may also need to change your input vector, as it's now representing your desired trajectory.

Update after comment & question edit

I think it's just a matter of creating a new state-space representation with the appropriate matrices, e.g.:

CL_loop = sys(A-B*K,zeros(size(B)),C,zeros(size(D))); % implement x'=(A-B*K)*x & y = C*x
lsim(CL_loop,u,t,x0);

• Thank you for reading! This seems to still not be exactly what I am looking for, as the feedback that I would like is u=-Kx. I think what you provided would give u =-Ky. May 1 '19 at 1:01
• I have updated my answer based on the extra information you provided in the question, I think it's just a matter of constructing a state-space system with the right matrices. May 1 '19 at 8:28