lsim
Simulating linear systems

The lsim command is quite similar to the step command (actually, the step command is just a special case of lsim). Given a system described either by state-space equations or a transfer function, the lsim command can run a simulation of the system using arbitrary inputs and initial conditions.

The command lsim(sys,U,T,X0) plots the time response of a linear time invariant system. This system can be continuous or discrete.

For a continuous time system, the differential equation is integrated from time T(0) to T(length(T)), starting at the initial condition X0 and using the input U. The input vector must have the same number of entries as the time vector. If the initial conditions are all zero, X0 can be omitted.

For a discrete time system, U should be sampled at the same rate as the system (T is then redundant and can be omitted or set to the empty matrix, i.e. [ ]).

Suppose we have a continuous time system described by the following A,B,C,D matrices:

A = [-20 -40 -60
1    0    0
0    1    0];
B = [1
0
0];
C = [0   0   1];
D = 0;

As you can see from the dimensions of the matrices, this system has three states, one input, and one output. Let's say we want to plot the response of the system with a non-zero initial condition and no input. This can be done in the following manner.
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);     %construct a system model
lsim(sys, U, T, X0)    % simulate and plot the response (the output)

When the lsim command is invoked with left-hand arguments,

[Y, Tsim, X] = lsim(sys,U,T);

no plot is drawn on the screen. MATLAB returns the output and state time history in the matrices Y and X respectively. In general Tsim is the same as T. However, in some case, T is resampled when intersample oscillations may occur. Then, Tsim is the time actually used in simulation. The matrix Y has as many columns as there are outputs and length(Tsim) rows. X has as many columns as there are states and length(Tsim) rows. The plot command can then be used to plot the response. Note that X can be omitted.

Now let's plot the step response for the system given above; the input U is 1 for all positive time, and the initial condition is zero.

T = 0:0.01:10;                 % simulation time = 10 seconds
U = ones(size(T));             % u = 1, a step input
sys = ss(A,B,C,D);             %construct a system model
[Y, Tsim, X] = lsim(sys,U,T);  % simulate
plot(Tsim,Y)                   % plot the output vs. time

The plot above shows the step response of the system. We can also plot the response of the system to any other input that we choose. For example, if we had a sinusoidal input, say u(t) = 0.1 sin(5t+1):

T = 0:0.01:10;                  % simulation time = 10 seconds
U = 0.1*sin(5*T+1);             % input as a function of time
sys = ss(A,B,C,D);              %construct a system model
[Y, Tsim, X] = lsim(sys,U,T);   % simulate
plot(Tsim,Y)                    % plot the output vs. time

Remember that the steady-state response of a linear system to a sinusoidal input will always be a sinusoid of the same frequency but with a different magnitude and phase.

The lsim command also works with transfer function model (continuous or discrete). Run the following commands:

T = 0:0.01:10;
U = 0.1*sin(5*T+1);
num=1;
den=[1 20 40 60];
sysTF = tf(num,den);
lsim(sysTF,U,T)

You should get the same picture as above.

Because any transfer function can be represented using many different sets of state-space matrices, you can only simulate a system in transfer function form with zero initial conditions (and cannot obtain the state trajectories); see the conversion page for more details.