Documentation |
Simulate time response of dynamic system to arbitrary inputs
lsim
lsim(sys,u,t)
lsim(sys,u,t,x0)
lsim(sys,u,t,x0,'zoh')
lsim(sys,u,t,x0,'foh')
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 time 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 matrix u must have as many rows as time samples (length(t)) and as many columns as system inputs. Each row u(i,:) specifies the input value(s) at the time sample t(i).
The LTI model sys can be continuous or discrete, SISO or MIMO. In discrete time, u must be sampled at the same rate as the system (t is then redundant and can be omitted or set to the 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 to state-space models.
lsim(sys,u,t,x0,'zoh') or lsim(sys,u,t,x0,'foh') explicitly specifies how the input values should be interpolated between samples (zero-order hold or linear interpolation). By default, lsim selects the interpolation method automatically based on the smoothness of the signal U.
Finally,
lsim(sys1,sys2,...,sysN,u,t)
simulates the responses of several LTI models to the same input history t,u and plots these responses on a single figure. As with bode or plot, you can specify a particular color, linestyle, and/or marker for each system, for example,
lsim(sys1,'y:',sys2,'g--',u,t,x0)
The multisystem behavior is similar to that of bode or step.
When invoked with left-hand arguments,
[y,t] = lsim(sys,u,t) [y,t,x] = lsim(sys,u,t) % for state-space models only [y,t,x] = lsim(sys,u,t,x0) % with initial state
return the output response y, the time vector t used for simulation, and the state trajectories x (for state-space models only). No plot is drawn on the screen. The matrix y has as many rows as time samples (length(t)) and as many columns as system outputs. The same holds for x with "outputs" replaced by states.
lsim(sys) opens the Linear Simulation Tool GUI. For more information about working with this GUI, see Working with the Linear Simulation Tool.
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 lsim.
lsim(H,u,t)
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 response z.OutputData.
plot(t,z.OutputData,'k',t,y,'r') legend('Measured','Simulated')