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. Specify
method as 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 specified as a vector of one, two, or three characters. The characters
can appear in any order. For example, the following code plots the
sys1 as a yellow dotted line and the
sys2 as a green dashed line:
For more information about configuring the
see Specify Line Style, Color, and Markers (MATLAB) 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.
Load estimation data to estimate a model.
load(fullfile(matlabroot,'toolbox','ident','iddemos','data','dcmotordata')); z = iddata(y,u,0.1,'Name','DC-motor');
z is an
iddata object that stores the 1-input 2-output estimation data with sample time 0.1 seconds.
Estimate a state-space model of order 4 using estimation data
[sys,x0] = n4sid(z,4);
sys is the estimated model and
x0 are the estimated initial states.
Simulate the response of
sys using the same input data as the one used for estimation and the initial states returned by the estimation command.
[y,t,x] = lsim(sys,z.InputData,,x0);
y is the system response,
t is the time vector used for simulation, and
x is the state trajectory.
Compare the simulated response
y to measured response
z.OutputData for both outputs.
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.