Simulate time response of dynamic system to arbitrary inputs
y = lsim(___)
[y,t,x] = lsim(___)
a plot of the time response of the dynamic
sys to the input
u. The vector
the time samples for the simulation (in system time units, specified
sys), and consists of regularly spaced time
t = 0:dt:Tfinal
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
u is a t-by-1 vector. If
three inputs, then
u is a t-by-3 array. Each row
the input value(s) at the time sample
u also appears on the plot.
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
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
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.
specifies an initial condition
x0 for the system
states. This syntax applies only when
sys is a
x0 is a vector whose entries
are the initial values of the corresponding states of
lsim(sys,u,t,x0,method) explicitly specifies
how the input values should be interpolated between samples, when
a continuous-time system. The string
take one of the following values:
'zoh' — Use zero-order hold
'foh' — Use linear interpolation
If you do not specify a method,
the interpolation method automatically based on the smoothness of
lsim(sys1,...,sysn,u,t) simulates the responses
of several dynamic system models to the same input history
plots these responses on a single figure. You can also use the
arguments when computing the responses of multiple models.
the line style, marker, and color of each of the system responses
in the plot. (You can also use the
arguments with this syntax.) Each
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
a yellow dotted line and the response of
a green dashed line:
For more information about configuring the
see Specify Line Style, Color, and Markers in the MATLAB® documentation.
y = lsim(___) returns the system
y, sampled at the same times as the input
t). The output
y is an array
having as many rows as time samples (
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
[y,t,x] = lsim(___) also returns
the time vector
t used for simulation and the state
x (for state-space models only).
x has as many rows as time samples (
and as many columns as system states. You can use this syntax with
any of the input arguments described in previous syntaxes except the
lsim(sys) opens the Linear
Simulation Tool GUI. For more information about working with this
GUI, see Working with the Linear
Simulate and plot the response of the following system to a square wave with period of four seconds:
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
The plot displays both the applied signal and the response.
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
Discrete-time systems are simulated with
filter (transfer function and zero-pole-gain).
Continuous-time systems are discretized with
'foh' is used for smooth input signals and
discontinuous signals such as pulses or square waves). The sample
time is set to the spacing
dt between the user-supplied
The choice of sample time can drastically affect simulation results. To illustrate why, consider the second-order model
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.