Documentation

lsim

Simulate time response of dynamic system to arbitrary inputs

Syntax

lsim(sys,u,t)
lsim(sys,u,t,x0)
lsim(sys,u,t,x0,method)
lsim(sys1,...,sysn,u,t)
lsim(sys1,PlotStyle1,...,sysN,PlotStyleN,u,t)
y = lsim(___)
[y,t,x] = lsim(___)
lsim(sys)

Description

lsim simulates the (time) response of continuous or discrete linear systems to arbitrary inputs. When invoked without left-hand arguments, lsim plots the response on the screen.

lsim(sys,u,t) produces a plot of the time response of the dynamic system model sys to the input history, t,u. The vector t specifies the time samples for the simulation (in system time units, specified in the TimeUnit property of sys), and consists of regularly spaced time samples:

t = 0:dt:Tfinal

The input u is an array having as many rows as time samples (length(t)) and as many columns as system inputs. For instance, if sys is a SISO system, then u is a t-by-1 vector. If sys has three inputs, then u is a t-by-3 array. Each row u(i,:) specifies the input value(s) at the time sample t(i). The signal u also appears on the plot.

The model sys can be continuous or discrete, SISO or MIMO. In discrete time, u must be sampled at the same rate as the system. In this case, the input t is redundant and can be omitted or set to an empty matrix. In continuous time, the time sampling dt = t(2)-t(1) is used to discretize the continuous model. If dt is too large (undersampling), lsim issues a warning suggesting that you use a more appropriate sample time, but will use the specified sample time. See Algorithms for a discussion of sample times.

lsim(sys,u,t,x0) further specifies an initial condition x0 for the system states. This syntax applies only when sys is a state-space model. x0 is a vector whose entries are the initial values of the corresponding states of sys.

lsim(sys,u,t,x0,method) explicitly specifies how the input values should be interpolated between samples, when sys is a continuous-time system. The string method can take one of the following values:

  • 'zoh' — Use zero-order hold

  • 'foh' — Use linear interpolation (first-order hold)

If you do not specify a method, lsim selects the interpolation method automatically based on the smoothness of the signal u.

lsim(sys1,...,sysn,u,t) simulates the responses of several dynamic system models to the same input history t,u and plots these responses on a single figure. You can also use the x0 and method input arguments when computing the responses of multiple models.

lsim(sys1,PlotStyle1,...,sysN,PlotStyleN,u,t) specifies the line style, marker, and color of each of the system responses in the plot. (You can also use the x0 and method input arguments with this syntax.) Each PlotStyle entry is a one-part, two-part, or three-part string enclosed in single quotes (' '). The elements of the string can appear in any order. The string can specify only the line style, the marker, or the color. For example, the following code plots the response of sys1 as a yellow dotted line and the response of sys2 ad a green dashed line:

lsim(sys1,'y:',sys2,'g--',u,t,x0)

For more information about configuring the PlotStyle string, see Specify Line Style, Color, and Markers in the MATLAB® documentation.

y = lsim(___) returns the system response y, sampled at the same times as the input (t). The output y is an array having as many rows as time samples (length(t)) and as many columns as system outputs. No plot is drawn on the screen. You can use this syntax with any of the input arguments described in previous syntaxes except the PlotStyle strings.

[y,t,x] = lsim(___) also returns the time vector t used for simulation and the state trajectories x (for state-space models only). The output x has as many rows as time samples (length(t)) and as many columns as system states. You can use this syntax with any of the input arguments described in previous syntaxes except the PlotStyle strings.

lsim(sys) opens the Linear Simulation Tool GUI. For more information about working with this GUI, see Working with the Linear Simulation Tool.

Examples

Simulate Response to Square Wave

Simulate and plot the response of the following system to a square wave with period of four seconds:

$$H(s) = \left[ \begin{array}{c}
\displaystyle \frac{{2{s^2} + 5s + 1}}{{{s^2} + 2s + 3}}\\[3ex]
\displaystyle \frac{{s - 1}}{{{s^2} + s + 5}}
\end{array} \right].$$

Create the transfer function, and generate the square wave with gensig. Sample every 0.1 second during 10 seconds.

H = [tf([2 5 1],[1 2 3]);tf([1 -1],[1 1 5])];
[u,t] = gensig('square',4,10,0.1);

Then simulate with lsim.

lsim(H,u,t)

The plot displays both the applied signal and the response.

Simulate Response of Identified Model

Simulate the response of an identified linear model using the same input signal as the one used for estimation and the initial states returned by the estimation command.

load(fullfile(matlabroot,'toolbox','ident','iddemos','data','dcmotordata'));
z = iddata(y,u,0.1,'Name','DC-motor');
 
[sys,x0] = n4sid(z,4);
[y,t,x] = lsim(sys, z.InputData, [], x0);

Compare the simulated response y to measured response z.OutputData.

plot(t,z.OutputData,'k',t,y,'r')
legend('Measured','Simulated')

More About

collapse all

Algorithms

Discrete-time systems are simulated with ltitr (state space) or filter (transfer function and zero-pole-gain).

Continuous-time systems are discretized with c2d using either the 'zoh' or 'foh' method ('foh' is used for smooth input signals and 'zoh' for discontinuous signals such as pulses or square waves). The sample time is set to the spacing dt between the user-supplied time samples t.

The choice of sample time can drastically affect simulation results. To illustrate why, consider the second-order model

H(s)=ω2s2+2s+ω2,ω=62.83

To simulate its response to a square wave with period 1 second, you can proceed as follows:

w2 = 62.83^2;
h = tf(w2,[1 2 w2]);
t = 0:0.1:5;                % vector of time samples
u = (rem(t,1) >= 0.5);        % square wave values
lsim(h,u,t)

lsim evaluates the specified sample time, and issues a warning:

Warning: Input signal is undersampled. Sample every 0.016 sec or 
faster.

To improve on this response, discretize H(s) using the recommended sample time:

dt = 0.016;
ts = 0:dt:5;
us = (rem(ts,1) >= 0.5);
hd = c2d(h,dt);
lsim(hd,us,ts)

This response exhibits strong oscillatory behavior that is hidden in the undersampled version.

Was this topic helpful?