Main Content

lsim

Plot simulated time response of dynamic system to arbitrary inputs; simulated response data

Description

Response Plots

example

lsim(sys,u,t)plots the simulated time response of thedynamic system modelsysto the input history (t,u). The vectortspecifies the time samples for the simulation. For single-input systems, the input signaluis a vector of the same length ast。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 tosys

example

lsim(sys,u,t,x0)further specifies a vectorx0of initial state values, whensysis a state-space model.

lsim(sys,u,t,x0,method)specifies howlsiminterpolates the input values between samples, whensysis a continuous-time model.

example

lsim(sys1,sys2,...,sysN,u,t,___)simulates the responses of several dynamic system models to the same input history and plots these responses on a single figure. All systems must have the same number of inputs and outputs. You can also use thex0andmethodinput arguments when computing the responses of multiple models.

example

lsim(sys1,LineSpec1,...,sysN,LineSpecN,___)specifies a color, line style, and marker for each system in the plot. When you need additional plot customization options, uselsimplotinstead.

Response Data

example

y= lsim(sys,u,t)returns the system responsey, sampled at the same timestas the input. For single-output systems,yis a vector of the same length ast。对于多输出系统,yis an array having as many rows as there are time samples (length(t)) and as many columns as there are outputs insys。This syntax does not generate a plot.

y= lsim(sys,u,t,x0)further specifies a vectorx0of initial state values, whensysis a state-space model.

y= lsim(sys,u,t,x0,method)specifies howlsiminterpolates the input values between samples, whensysis a continuous-time model.

example

[y,tOut,x] = lsim(___)returns the state trajectoriesx, whensysis a state-space model.xis an array with as many rows as there are time samples and as many columns as there are states insys。This syntax also returns the time samples used for the simulation intOut

Linear Simulation Tool

lsim(sys)opens the Linear Simulation Tool. For more information about using this tool for linear analysis, seeWorking with the Linear Simulation Tool(Control System Toolbox)

Examples

collapse all

Consider the following transfer function.

sys = tf(3,[1 2 3])
sys = 3 ------------- s^2 + 2 s + 3 Continuous-time transfer function.

To compute the response of this system to an arbitrary input signal, providelsimwith a vector of the timestat which you want to compute the response and a vectorucontaining the corresponding signal values. For instance, plot the system response to a ramping step signal that starts at 0 at timet = 0, ramps from 0 att = 1to 1 att = 2, and then holds steady at 1. Definetand compute the values ofu

t = 0:0.04:8;% 201 pointsu = max(0,min(t-1,1));

Uselsimwithout an output argument to plot the system response to the signal.

lsim(sys,u,t) gridon

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Driving inputs, sys.

The plot shows the applied input(u,t)in gray and the system response in blue.

Uselsimwith an output argument to obtain the simulated response data.

y = lsim(sys,u,t); size(y)
ans =1×2201 1

The vectorycontains the simulated response at the corresponding times int

Usegensig(Control System Toolbox)to create periodic input signals such as sine waves and square waves for use withlsim。Simulate the response to a square wave of the following SISO state-space model.

A = [-3 -1.5; 5 0]; B = [1; 0]; C = [0.5 1.5]; D = 0; sys = ss(A,B,C,D);

For this example, create a square wave with a period of 10 s and a duration of 20 s.

[u,t] = gensig("square",10,20);

gensigreturns the vectortof time steps and the vectorucontaining the corresponding values of the input signal. (If you do not specify a sample time fort,ngensiggenerates 64 samples per period.) Use these withlsimand plot the system response.

lsim(sys,u,t) gridon

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Driving inputs, sys.

The plot shows the applied square wave in gray and the system response in blue. Calllsimwith an output argument to obtain the response values at each point int

[y,~] = lsim(sys,u,t);

When you simulate the response of a discrete-time system, the time vectortmust be of the formTi:dT:Tf, wheredTis the sample time of the model. Simulate the response of the following discrete-time transfer function to a ramp step input.

sys = tf([0.06 0.05],[1 -1.56 0.67],0.05);

This transfer function has a sample time of 0.05 s. Use the same sample time to generate the time vectortand a ramped step signalu

t = 0:0.05:4; u = max(0,min(t-1,1));

Plot the system response.

lsim(sys,u,t)

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Driving inputs, sys.

To simulate the response of a discrete-time system to a periodic input signal, use the same sample time withgensigto generate the input. For instance, simulate the system response to a sine wave with period of 1 s and a duration of 4 s.

[u,t] = gensig("sine",1,4,0.05);

Plot the system response.

lsim(sys,u,t)

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Driving inputs, sys.

lsimallows 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. 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); lsim(sys1,sys2,u,t) gridonlegend("PI","PID")

Figure contains an axes object. The axes object contains 3 objects of type line. These objects represent Driving inputs, PI, PID.

By default,lsimchooses distinct colors for each system that you plot. You can specify colors and line styles using theLineSpecinput argument.

lsim(sys1,"r--",sys2,"b",u,t) gridonlegend("PI","PID")

Figure contains an axes object. The axes object contains 3 objects of type line. These objects represent Driving inputs, PI, PID.

The firstLineSpec"r--"specifies a dashed red line for the response with the PI controller. The secondLineSpec"b”specifies a solid blue line for the response with the PID controller. The legend reflects the specified colors and line styles. For more plot customization options, uselsimplot

In a MIMO system, at each time stept,inputu(t)is a vector whose length is the number of inputs. To uselsim, you specifyuas a matrix with dimensionsNt-by-Nu, whereNuis the number of system inputs andNtis the length oft。In other words, each column ofuis the input signal applied to the corresponding system input. For instance, to simulate a system with four inputs for 201 time steps, provideuas a matrix of four columns and 201 rows, where each rowu(i,:)is the vector of input values at theith time step; each columnu(:,j)is the signal applied at thejth input.

Similarly, the outputy(t)computed bylsim是一个矩阵的列表示信号在哪里each system output. When you uselsimto plot the simulated response,lsimprovides separate axes for each output, representing the system response in each output channel to the inputu(t)applied at all inputs.

Consider the two-input, three-output state-space model with the following state-space matrices.

A = [-1.5 -0.2 1.0; -0.2 -1.7 0.6; 1.0 0.6 -1.4]; B = [ 1.5 0.6; -1.8 1.0; 0 0 ]; C = [ 0 -0.5 -0.1; 0.35 -0.1 -0.15 0.65 0 0.6]; D = [ 0.5 0; 0.05 0.75 0 0]; sys = ss(A,B,C,D);

Plot the response ofsysto a square wave of period 4 s, applied to the first inputsysand a pulse applied to the second input every 3 s. To do so, create column vectors representing the square wave and the pulsed signal usinggensig。Then stack the columns into an input matrix. To ensure the two signals have the same number of samples, specify the same end time and sample time.

Tf = 10; Ts = 0.1; [uSq,t] = gensig("square",4,Tf,Ts); [uP,~] = gensig("pulse",3,Tf,Ts); u = [uSq uP]; lsim(sys,u,t)

Figure contains 3 axes objects. Axes object 1 contains 3 objects of type line. These objects represent Driving inputs, sys. Axes object 2 contains 3 objects of type line. These objects represent Driving inputs, sys. Axes object 3 contains 3 objects of type line. These objects represent Driving inputs, sys.

Each axis shows the response of one of the three system outputs to the signalsuapplied at all inputs. Each plot also shows all input signals in gray.

By default,lsimsimulates the model assuming all states are zero at the start of the simulation. When simulating the response of a state-space model, use the optionalx0input 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 vectorx0of 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; lsim(sys,u,t,x0) gridon

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Driving inputs, sys.

The first half of the plot shows the free evolution of the system from the initial state values(-0.2 - 0.3)。Att = 2there 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.

When you uselsimwith output arguments, it returns the simulated response data in an array. For a SISO system, the response data is returned as a column vector of the same length ast。For instance, extract the response of a SISO system to a square wave. Create the square wave usinggensig

sys = tf([2 5 1],[1 2 3]); [u,t] = gensig("square",4,10,0.05); [y,t] = lsim(sys,u,t); size(y)
ans =1×2201 1

The vectorycontains the simulated response at each time step int。(lsimreturns the time vectortas a convenience.)

For a MIMO system, the response data is returned in an array of dimensionsN-by-Ny-by-Nu, whereNyandNuare the number of outputs and inputs of the dynamic system. For instance, consider the following state-space model, representing a three-state system with two inputs and three outputs.

A = [-1.5 -0.2 1.0; -0.2 -1.7 0.6; 1.0 0.6 -1.4]; B = [ 1.5 0.6; -1.8 1.0; 0 0 ]; C = [ 0 -0.1 -0.2; 0.7 -0.2 -0.3 -0.65 0 -0.6]; D = [ 0.1 0; 0.1 1.5 0 0]; sys = ss(A,B,C,D);

Extract the responses of the three output channels to the square wave applied at both inputs.

uM = [u u]; [y,t] = lsim(sys,uM,t); size(y)
ans =1×2201 3

y(:,j)is a column vector containing response at thejth output to the square wave applied to both inputs. That is,y(i,:)is a vector of three values, the output values at theith time step.

Becausesysis a state-space model, you can extract the time evolution of the state values in response to the input signal.

[y,t,x] = lsim(sys,uM,t); size(x)
ans =1×2201 3

Each row ofxcontains the state values[x1,x2,x3]at the corresponding time int。In other words,x(i,:)is the state vector at theith time step. Plot the state values.

plot(t,x)

Figure contains an axes object. The axes object contains 3 objects of type line.

The example Plot Response of Multiple Systems to Same Input shows how to plot responses of several individual systems on a single axis. When you have multiple dynamic systems arranged in a model array,lsimplots all their responses at once.

Create a model array. For this example, use a one-dimensional array of second-order transfer functions having different natural frequencies. First, preallocate memory for the model array. The following command creates a 1-by-5 row of zero-gain SISO transfer functions. The first two dimensions represent the model outputs and inputs. The remaining dimensions are the array dimensions. (For more information about model arrays and how to create them, seeModel Arrays(Control System Toolbox)。)

sys = tf(zeros(1,1,1,5));

Populate the array.

w0 = 1.5:1:5.5;% natural frequencieszeta = 0.5;% damping constantfori = 1:length(w0) sys(:,:,1,i) = tf(w0(i)^2,[1 2*zeta*w0(i) w0(i)^2]);end

Plot the responses of all models in the array to a square wave input.

[u,t] = gensig("square",5,15); lsim(sys,u,t)

Figure contains an axes object. The axes object contains 6 objects of type line. These objects represent Driving inputs, sys.

lsimuses the same line style for the responses of all entries in the array. One way to distinguish among entries is to use theSamplingGridproperty of dynamic system models to associate each entry in the array with the correspondingw0value.

sys.SamplingGrid = struct('frequency',w0);

Now, when you plot the responses in a MATLAB figure window, you can click a trace to see which frequency value it corresponds to.

Load estimation data to estimate a model.

load(fullfile(matlabroot,'toolbox','ident','iddemos','data','dcmotordata')); z = iddata(y,u,0.1,'Name','DC-motor');

zis aniddataobject that stores the one-input two-output estimation data with a sample time of 0.1 s.

Estimate a state-space model of order 4 using estimation dataz

[sys,x0] = n4sid(z,4);

sysis the estimated model andx0is the estimated initial states.

Simulate the response ofsys使用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);

Here,yis the system response,tis the time vector used for simulation, andxis the state trajectory.

Compare the simulated responseyto the measured responsez.OutputDatafor both outputs.

subplot(211), plot(t,z.OutputData(:,1),'k',t,y(:,1),'r') legend('Measured','Simulated') subplot(212), plot(t,z.OutputData(:,2),'k',t,y(:,2),'r') legend('Measured','Simulated')

Figure contains 2 axes objects. Axes object 1 contains 2 objects of type line. These objects represent Measured, Simulated. Axes object 2 contains 2 objects of type line. These objects represent Measured, Simulated.

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

s y s ( s ) = ω 2 s 2 + 2 s + ω 2 , ω = 62.83

Simulate the response of this model to a square wave with period 1 s, using a sample time of 0.1 s.

w2 = 62.83^2; sys = tf(w2,[1 2 w2]); tau = 1; Tf = 5; Ts = 0.1; [u,t] = gensig("square",tau,Tf,Ts); lsim(sys,u,t)

lsim warning message: The input signal is undersampled.

lsimsimulates the model using the specified input signal, but it issues a warning that the input signal is undersampled.lsimrecommends a sample time that generates at least 64 samples per period of the inputu。To see why this recommendation matters, simulatesysagain using a sample time smaller than the recommended maximum.

figure Ts2 = 0.01; [u2,t2] = gensig("square",tau,Tf,Ts2); lsim(sys,u2,t2)

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

Input Arguments

collapse all

Dynamic system, specified as a SISO or MIMO dynamic system model or array of dynamic system models. Dynamic systems whose responses you can simulate include:

  • Continuous-time or discrete-time numeric LTI models, such astf,zpk, orssmodels.

  • Generalized or uncertain LTI models such asgenssorussmodels. (Using uncertain models requires Robust Control Toolbox™ software.)

    • For tunable control design blocks, the function evaluates the model at its current value for both plotting and returning response data.

    • For uncertain control design blocks, the function plots the nominal value and random samples of the model. When you use output arguments, the function returns response data for the nominal model only.

  • Sparse state-space models such assparssandmechssmodels.

  • Identified LTI models, such asidtf,idss, oridprocmodels. For identified models, you can also use thesimcommand, 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.

lsimdoes not support frequency-response data models such asfrd,genfrd, oridfrdmodels.

Ifsysis an array of models, the function plots the responses of all models in the array on the same axes. SeeResponse of Systems in Model Array(Control System Toolbox)

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 ast

  • 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 tosys。In other words, each rowu(i,:)represents the values applied at the inputs ofsysat timet(i)。Each columnu(:,j)is the signal applied to thejth input ofsys

Time samples at which to compute the response, specified as a vector of the form0:dT:Tf。Thelsim命令解释tas having the units specified in theTimeUnitproperty of the modelsys

For continuous-timesys,lsimcommand uses the time stepdTto discretize the model. IfdTis too large relative to the system dynamics (undersampling),lsimissues a warning recommending a faster sampling time. For further discussion of the impact of sampling time on simulation, seeEffect of Sample Time on Simulation(Control System Toolbox)

For discrete-timesys,time stepdTmust equal the sample time ofsys。Alternatively, you can omittor set it to[]。In that case,lsimsetstto a vector of the same length asuthat begins at 0 with a time step equal tosys.Ts

Initial state values for simulating a state-space model, specified as a vector having one entry for each state insys。If you omit this argument, thenlsimsets all states to zero att = 0

Discretization method for sampling continuous-time models, specified as one of the following.

  • 'zoh'— Zero-order hold

  • 'foh'— First-order hold

Whensysis a continuous-time model,lsimcomputes the time response by discretizing the model using a sample time equal to the time stepdT = t(2)-t(1)oft。If you do not specify a discretization method, thenlsimselects the method automatically based on the smoothness of the signalu。For more information about these two discretization methods, seeContinuous-Discrete Conversion Methods(Control System Toolbox)

线型、标志和颜色,指定为一个生态学者ng or vector of one, two, or three characters. The characters 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. For more information about configuring this argument, see theLineSpecinput argument of theplotfunction.

Example:'r--'specifies a red dashed line

Example:'*b'specifies blue asterisk markers

Example:'y'specifies a yellow line

Output Arguments

collapse all

Simulated response data, returned as an array.

  • For single-input systems,yis a column vector of the same length ast

  • 对于多输出系统,yis an array with as many rows as there are time samples (length(t)) and as many columns as there are outputs insys。Thus, thejth column ofy, ory(:,j), contains the response at thejth output touapplied at all inputs.

Time vector used for simulation, returned as a column vector. When you specify an input time vectortof the form0:dT:Tf,ntOut = t。Iftis nearly equisampled,lsimadjusts the sample times for simulation and returns the result intOut。For discrete-timesys, you can omittor set it to[]。In that case,lsimsetstto a vector of the same length asuthat begins at 0 with a time step equal tosys.Ts, and returns the result intOut

State trajectories, returned as an array. Whensysis a state-space model,xcontains the evolution of the states ofsysin response to the input.xis an array with as many rows as there are time samples(length(t))and as many columns as there are states in sys.

Tips

  • When you need additional plot customization options, uselsimplotinstead.

Algorithms

For a discrete-time transfer function,

s y s ( z 1 ) = a 0 + a 1 z 1 + + a n z n 1 + b 1 z 1 + + b n z n ,

lsimfilters the input based on the recursion associated with this transfer function:

y [ k ] = a 0 u [ k ] + + a n u [ k n ] b 1 y [ k 1 ] b n [ k n ]

For discrete-timezpkmodels,lsimfilters the input through a series of first-order or second-order sections. This approach avoids forming the numerator and denominator polynomials, which can cause numerical instability for higher-order models.

For discrete-time state-space models,lsimpropagates the discrete-time state-space equations,

x [ n + 1 ] = A x [ n ] + B u [ n ] , y [ n ] = C x [ n ] + D u [ n ]

For continuous-time systems,lsimfirst discretizes the system usingc2d, and then propagates the resulting discrete-time state-space equations. Unless you specify otherwise with themethodinput argument,lsimuses the first-order-hold discretization method when the input signal is smooth, and zero-order hold when the input signal is discontinuous, such as for pulses or square waves. The sample time for discretization is the spacingdTbetween the time samples you supply int

See Also

Functions

Apps

Introduced in R2012a