Plot simulated time response of dynamic system to arbitrary inputs with additional plot customization options
lsimplot lets you plot simulated time response of dynamic
system to arbitrary inputs with a broader range of plot customization options than
lsim. You can use
lsimplot to obtain the plot handle
and use it to customize the plot, such as modify the axes labels, limits and units. You can
lsimplot to draw a simulated time response plot on an existing set
of axes represented by an axes handle. To customize an existing simulated time response plot
using the plot handle:
Obtain the plot handle
getoptionsto obtain the option set
Update the plot using
setoptionsto modify the required options
the Linear Simulation Tool for the dynamic system model
h = lsimplot(
sys, where you can interactively specify the driving input(s), the time
vector, and initial state. It also returns the plot handle
h. You can
use this handle
h to customize the plot with the
For more information about using the Linear Simulation Tool for linear analysis, see Working with the Linear Simulation Tool.
plots the simulated time response of the model
h = lsimplot(
sys to the input signal
u and the corresponding time vector
t. For MIMO
u is a matrix with as many columns as the number of inputs and
ith row specifies the input value at time
For SISO systems, the input
u can be specified either as a row or
plots the simulated response with the options set specified in
h = lsimplot(___,
plotoptions. You can use these options to customize the plot
appearance using the command line. Settings you specify in
overrides the preference settings in the MATLAB® session in which you run
lsimplot. Therefore, this syntax
is useful when you want to write a script to generate multiple plots that look the same
regardless of the local preferences.
Customized Plot of Simulated Response to Arbitrary Input Signal
For this example, change time units to minutes and turn the grid on for the simulated response plot. Consider the following transfer function.
sys = tf(3,[1 2 3]);
To compute the response of this system to an arbitrary input signal, provide
lsimplot with a vector of the times
t at which you want to compute the response and a vector
u containing the corresponding signal values. For instance, plot the system response to a ramping step signal that starts at 0 at time
t = 0, ramps from 0 at
t = 1 to 1 at
t = 2, and then holds steady at 1. Define
t and compute the values of
t = 0:0.04:8; u = max(0,min(t-1,1));
lsimplot plot the system response to the signal with a plot handle
h = lsimplot(sys,u,t);
The plot shows the applied input
(u,t) in gray and the system response in blue.
Use the plot handle to change the time units to minutes and to turn the grid on. To do so, edit properties of the plot handle,
The plot automatically updates when you call
Alternatively, you can also use the
timeoptions command to specify the required plot options. First, create an options set based on the toolbox preferences.
plotoptions = timeoptions('cstprefs');
Change properties of the options set by setting the time units to minutes and enabling the grid.
plotoptions.TimeUnits = 'minutes'; plotoptions.Grid = 'on'; lsimplot(sys,u,t,plotoptions);
Customized Plot Response of Multiple Systems to Same Input
lsimplot allows you to plot the simulated responses of multiple dynamic systems on the same axis. For instance, compare the closed-loop response of a system with a PI controller and a PID controller. Then, customize the plot by enabling normalization and turning the grid on.
First, create a transfer function of the system and tune the controllers.
H = tf(4,[1 10 25]); C1 = pidtune(H,'PI'); C2 = pidtune(H,'PID');
Form the closed-loop systems.
sys1 = feedback(H*C1,1); sys2 = feedback(H*C2,1);
Plot the responses of both systems to a square wave with a period of 4 s.
[u,t] = gensig("square",4,12); h1 = lsimplot(sys1,sys2,u,t); legend("PI","PID")
setoptions to enable normalization and to turn on the grid.
The plot automatically updates when you call
lsimplot chooses distinct colors for each system that you plot. You can specify colors and line styles using the
LineSpec input argument.
h2 = lsimplot(sys1,"r--",sys2,"b",u,t); legend("PI","PID") setoptions(h2,'Normalize','on','Grid','on')
r--" specifies a dashed red line for the response with the PI controller. The second
b" specifies a solid blue line for the response with the PID controller. The legend reflects the specified colors and line styles.
Custom Plot of System Evolution from Initial Condition
lsimplot simulates the model assuming all states are zero at the start of the simulation. When simulating the response of a state-space model, use the optional
x0 input argument to specify nonzero initial state values. Consider the following two-state SISO state-space model.
A = [-1.5 -3; 3 -1]; B = [1.3; 0]; C = [1.15 2.3]; D = 0; sys = ss(A,B,C,D);
Suppose that you want to allow the system to evolve from a known set of initial states with no input for 2 s, and then apply a unit step change. Specify the vector
x0 of initial state values, and create the input vector.
x0 = [-0.2 0.3]; t = 0:0.05:8; u = zeros(length(t),1); u(t>=2) = 1;
First, create a default options set using
plotoptions = timeoptions;
Next change the required properties of the options set
plotoptions and plot the simulated response with the zero order hold option.
plotoptions.Title.FontSize = 15; plotoptions.Title.Color = [0 0 1]; plotoptions.Grid = 'on'; h = lsimplot(sys,u,t,x0,plotoptions,'zoh'); hold on title('Simulated Time Response with Initial Conditions')
The first half of the plot shows the free evolution of the system from the initial state values
[-0.2 0.3]. At
t = 2 there is a step change to the input, and the plot shows the system response to this new signal beginning from the state values at that time. Because
plotoptions begins with a fixed set of options, the plot result is independent of the toolbox preferences of the MATLAB session.
sys — Dynamic system
dynamic system model | model array
Dynamic system, specified as a SISO or MIMO dynamic system model or array of dynamic system models. Dynamic systems that you can use include:
For tunable control design blocks, the function evaluates the model at its current value to plot the simulated response.
For uncertain control design blocks, the function plots the nominal value and random samples of the model.
Identified LTI models, such as
idtf(System Identification Toolbox),
idss(System Identification Toolbox), or
idproc(System Identification Toolbox) models. For identified models, you can also use the
sim(System Identification Toolbox) command, which can compute the standard deviation of the simulated response and state trajectories.
simcan also simulate all types of models with nonzero initial conditions, and can simulate nonlinear identified models.(Using identified models requires System Identification Toolbox™ software.)
lsimplot does not support frequency-response data models such
sys is an array of models, the function plots the responses
of all models in the array on the same axes.
LineSpec — Line style, marker, and color
character vector | string
Line style, marker, and color, specified as a character vector or string containing symbols. The symbols can appear in any order. You do not need to specify all three characteristics (line style, marker, and color). For example, if you omit the line style and specify the marker, then the plot shows only the marker and no line.
'--or' is a red dashed line with circle markers
u — Input signal for simulation
vector | array
Input signal for simulation, specified as a vector for single-input systems, and an array for multi-input systems.
For single-input systems,
uis a vector of the same length as
For multi-input systems,
uis an array with as many rows as there are time samples (
length(t)) and as many columns as there are inputs to
sys. In other words, each row
u(i,:)represents the values applied at the inputs of
t(i). Each column
u(:,j)is the signal applied to the
jth input of
t — Time samples
Time samples at which to compute the response, specified as a vector of the form
lsimplot command interprets
t as having the units specified in the
TimeUnit property of the model
sys. The time
vector must be real, finite, and must contain monotonically increasing and evenly spaced
For continuous-time systems, the
lsimplot command uses the time
dT to discretize the model. If
dT is too
large relative to the system dynamics (undersampling),
issues a warning recommending a faster sampling time.
For discrete-time systems, the time step
dT must equal the sample
sys. Alternatively, you can omit
set it to
. In that case,
t to a vector of the same length as
begins at 0 with a time step equal to
method — Discretization method
Discretization method for sampling continuous-time models, specified as one of the following.
'zoh'— Zero-order hold
'foh'— First-order hold
sys is a continuous-time model,
lsimplot computes the time response by discretizing the model
using a sample time equal to the time step
dT = t(2)-t(1) of
t. If you do not specify a discretization method, then
lsimplot selects the method automatically based on the smoothness
of the signal
u. For more information about these two
discretization methods, see Continuous-Discrete Conversion Methods.
x0 — Initial state values
vector of zeros (default) | vector
Initial state values for simulating a state-space model, specified as a vector
having one entry for each state in
sys. If you omit this argument,
lsim sets all states to zero at
AX — Target axes
Target axes, specified as an
Axes object. If you do not specify
the axes and if the current axes are Cartesian axes, then
plots on the current axes. Use
AX to plot into specific axes when
creating a step plot.
plotoptions — Step plot options set
Step plot options set, specified as a
TimePlotOptions object. You
can use this option set to customize the step plot appearance. Use
timeoptions to create the option set. Settings you
plotoptions overrides the preference settings in the
MATLAB session in which you run
plotoptions is useful when you want to write a script to generate
multiple plots that look the same regardless of the local preferences.
For the list of available options, see