Simulate time response of dynamic system to arbitrary inputs
lsim(sys,u,t)
lsim(sys,u,t,x0)
lsim(sys,u,t,x0,method)
lsim(sys1,...,sysn,u,t)
lsim(sys1,LineSpec1,...,sysN,LineSpecN,u,t)
y = lsim(___)
[y,t,x] = lsim(___)
lsim(sys)
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. Specify method
as 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,LineSpec1,...,sysN,LineSpecN,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
LineSpec
argument is specified as a vector of one, two, or three
characters. The characters can appear in any order. For example, the following code plots the
response of sys1
as a yellow dotted line and the response of
sys2
as a green dashed line:
lsim(sys1,'y:',sys2,'g--',u,t,x0)
For more information about configuring this argument, see the LineSpec
input argument of the plot
function.
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 LineSpec
arguments.
[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
LineSpec
arguments.
lsim(sys)
opens the Linear
Simulation Tool GUI. For more information about working with this
GUI, see Working with the Linear
Simulation Tool.
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
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.