Simulate response of identified models to arbitrary inputs




y = sim(sys,udata) returns the simulated response of an identified model using the input data, udata. By default, zero initial conditions are used for all model types except idnlgrey, in which case the initial conditions stored internally in the model are used.


y = sim(sys,udata,opt) additionally uses the option set, opt, to configure the simulation option, including the specification of initial conditions.


[y,y_sd] = sim(___) also returns the estimated standard deviation, y_sd, of the simulated response.


[y,y_sd,x] = sim(___) also returns the state trajectory, x, for state-space models. If sys is not a state-space model, this syntax generates a warning.


sim(___) plots the simulated response of the identified model.


collapse all

Simulate a Nonlinear Grey-Box Model Using Input Data

Load the data.

load twotankdata

The data is from a two-tank system with one input (voltage), and one output (height of liquid in second tank). The structure of the model is specified by the twotanks_c C MEX file.

Create a nonlinear grey-box model.

FileName = 'twotanks_c';
Order = [1 1 2];
Parameters = {0.5; 0.0035; 0.019; 9.81; 0.25; 0.016};
InitialStates = [0; 0.1];
Ts = 0;

nlgr = idnlgrey(FileName,Order,Parameters,InitialStates,Ts,'Name','Two tanks');

Create an iddata object containing the input signal u.

z = iddata([],u,0.2,'Name','Two tanks');

Simulate the model using the input data.

y = sim(nlgr,z);

Add Noise to Simulated Model Response

Load the data, and obtain the identified model.

load iddata2 z2
sys = n4sid(z2,3);

sys is a third-order state-sapce model estimated using a subspace method.

Create a simulation option set to add noise to the simulated model response.

opt1 = simOptions('AddNoise',true);

Simulate the model.

y = sim(sys,z2,opt1);

Default Gaussian white noise is filtered by the noise transfer function of the model and added to the simulated model response.

You can also add your own noise signal, e, using the NoiseData option.

e = randn(length(z2.u),1);
opt2 = simOptions('AddNoise',true,'NoiseData',e);

Simulate the model.

y = sim(sys,z2,opt2);

Simulate Model Using Initial Conditions Obtained During Estimation

Load data.

load iddata1 z1

Specify the estimation option to estimate the initial state.

estimOpt = ssestOptions('InitialState','estimate');

Estimate a state-space model, and return the value of the estimated initial state.

[sys,x0] = ssest(z1,2,estimOpt);

Specify initial conditions for simulation

simOpt = simOptions('InitialCondition',x0);

Simulate the model, and obtain the model response and standard deviation.

[y,y_sd] = sim(sys,z1,simOpt);

Estimate Standard Deviation and State Trajectory for State-Space Models

Load estimation data, and estimate a state-space model.

load iddata1 z1
sys = ssest(z1,2);

Return the standard deviation and state trajectory.

[y,y_sd,x] = sim(sys,z1);

Plot Simulated Model Response

Obtain the identified model.

load iddata2 z2
sys = tfest(z2,3);

sys is an idtf model that encapsulates the third-order transfer function estimated for the measured data z2.

Simulate the model.


Simulate Nonlinear ARX Model

Simulate a single-input single-output nonlinear ARX model around a known equilibrium point, with an input level of 1 and output level of 10.

Load the sample data.

load iddata2

Estimate a nonlinear ARX model from the data.

M = nlarx(z2,[2 2 1],'treepartition');

Estimate current states of model based on past data.

x0 = data2state(M,struct('Input',1,'Output',10));

Simulate the model using the initial states returned by data2state.

opt = simOptions('InitialCondition',x0);

Continue from End of Previous Simulation

Continue the simulation of a nonlinear ARX model from the end of a previous simulation run.

Estimate a nonlinear ARX model from data.

load iddata2
M = nlarx(z2,[2 2 1],'treepartition');

Simulate the model using the first half of the input data z2. Start the simulation from zero initial states.

u1 = z2(1:200,[]);
opt1 = simOptions('InitialCondition','zero');
ys1 = sim(M,u1,opt1);

Start another simulation using the second half of the input data z2. Use the same states of the model from the end of the first simulation.

u2 = z2(201:end,[]);

To set the initial states for the second simulation correctly, package input u1 and output ys1 from the first simulation into one iddata object. Pass this data as initial conditions for the next simulation.

firstSimData = [ys1,u1];
opt2 = simOptions('InitialCondition',firstSimData);
ys2 = sim(M,u2,opt2);

Verify the two simulations by comparing to a complete simulation using all the input data z2. First, extract the whole set of input data.

uTotal = z2(:,[]);
opt3 = simOptions('InitialCondition','zero');
ysTotal = sim(M,uTotal,opt3);

Plot the three responses ys1, ys2 and ysTotal. ys1 should be equal to first half of ysTotal. ys2 should be equal to the second half of ysTotal.


The plot shows that the three responses ys1, ys2, and ysTotal overlap as expected.

Match Model Response to Output Data

Estimate initial states of model M such that, the response best matches the output in data set z2.

Load the sample data.

load iddata2;

Estimate a nonlinear ARX model from the data.

M = nlarx(z2,[4 3 2],wavenet('NumberOfUnits',20));

Estimate the initial states of M to best fit z2.y in the simulated response.

x0 = findstates(M,z2,Inf);

Simulate the model.

opt = simOptions('InitialCondition',x0);
ysim = sim(M,z2.u,opt);

Compare the simulated model output ysim with the output signal in z2.

time = z2.SamplingInstants;

Simulate Model Near Steady State with Known Input and Unknown Output

Start simulation of a model near steady state, where the input is known to be 1, but the output is unknown.

Load the sample data.

load iddata2

Estimate a nonlinear ARX model from the data.

M = nlarx(z2,[4 3 2],'wavenet');

Determine equilibrium state values for input 1 and unknown target output.

x0 = findop(M,'steady',1, NaN);

Simulate the model using initial states x0.

opt = simOptions('InitialCondition',x0);

Simulate Hammerstein-Wiener Model at Steady-State Operating Point

Load the sample data.

load iddata2

Create a Hammerstein-Wiener model.

M = nlhw(z2,[4 3 2],[],'pwlinear');

Compute steady-state operating point values corresponding to an input level of 1 and an unknown output level.

x0 = findop(M,'steady',1,NaN);

Simulate the model using the estimated initial states.

opt = simOptions('InitialCondition',x0);

Simulate Time-Series Model

Load time-series data, and estimate an AR model using the least-squares approach.

load iddata9 z9
sys = ar(z9,6,'ls');

For time-series data,, specify the desired simulation length, N = 200 using an N-by-|0| input data set.

data = iddata([],zeros(200,0),z9.Ts);

Set the initial conditions to use the initial samples of the time series as historical output samples.

IC = struct('Input',[],'Output',z9.y(1:6));
opt = simOptions('InitialCondition',IC);

Simulate the model.


Related Examples

Input Arguments

collapse all

sys — Identified modellinear identified model | nonlinear identified model

Identified model, specified as one of the following:

 Model TypeModel Object
Linear Identified ModelPolynomial modelidpoly
Process modelidproc
State-space modelidss
Transfer function modelidtf
Linear grey-box modelidgrey
Nonlinear Identified ModelNonlinear ARX modelidnlarx
Nonlinear Hammerstein-Wiener modelidnlhw
Nonlinear grey-box modelidnlgrey

udata — Simulation input dataiddata object | matrix

Simulation input data, specified as an iddata object or a matrix. sim uses the input channels from this object as the simulation inputs. For time-domain simulation of discrete-time systems, you can also specify udata as a matrix with columns that correspond to each input channel.

If sys is a linear model, you can use either time-domain or frequency-domain data. If sys is a nonlinear model, you can only use time-domain data.

If sys is a time-series model, that is a model with no inputs, specify udata as an Ns-by-0 signal, where Ns is the wanted number of simulation output samples. For example, to simulate 100 output samples, specify udata as follows.

udata = iddata([],zeros(100,0),Ts);

If you do not have data from an experiment, use idinput to generate signals with various characteristics.

opt — Simulation optionssimOptions option set

Simulation options, specified as a simOptions option set for setting the following options:

  • Initial conditions

  • Input/output offsets

  • Additive noise

Output Arguments

collapse all

y — Simulated responseiddata object

Simulated response for sys, returned as an iddata object.

If udata represents time-domain data, then y is the simulated response for the time vector corresponding to udata.

If udata represents frequency-domain data, U(ω), then y contains the Fourier transform of the corresponding sampled time-domain output signal. This signal is the product of the frequency response of sys, G(ω), and U(ω).

For multi-experiment data, y is a corresponding multi-experiment iddata object.

y_sd — Estimated standard deviationiddata object | []

Estimated standard deviation of the simulated response for linear models or nonlinear grey-box models, returned as an iddata object.

y_sd is derived using first order sensitivity considerations (Gauss approximation formula).

For nonlinear models, y_sd is [].

x — Estimated state trajectory for state-space modelsmatrix | []

Estimated state trajectory for state-space models, returned as an Ns-by-Nx matrix, where Ns is the number of samples and Nx is the number of states.

x is only relevant if sys is a state-space model (idss, idgrey, or idnlgrey). If sys is not a state-space model, x is returned as [].


  • Use simsd for a Monte-Carlo method of computing the standard deviation of the response.

  • sim extends lsim to facilitate additional features relevant to identified models:

    • Simulation of nonlinear models

    • Simulation with additive noise

    • Incorporation of signal offsets

    • Computation of response standard deviation (linear models only)

    • Frequency-domain simulation (linear models only)

    • Simulations using different intersample behavior for different inputs

    To obtain the simulated response without any of the preceding operations, use lsim.

More About

expand all


  • When the initial conditions of the estimated model and the system that measured the validation data set are different, the simulated and measured responses may also differ, especially at the beginning of the response. To minimize this difference, estimate the initial state values using findstates and use the estimated values to set the InitialCondition option using simOptions.

    % Estimate the initial states
    X0est = findstates(m,z);
    % Specify the initial conditions
    opt = simOptions('InitialCondition',X0est);
    % Simulate the response


Simulation means computing the model response using input data and initial conditions. sim simulates the following system:


  • u(t) is the simulation input data, udata.

  • y(t) is the simulated output response.

  • G is the transfer function from the input to the output and is defined in sys. The simulation initial conditions, as specified using simOptions, set the initial state of G.

  • e(t) is an optional noise signal. Add noise to your simulation by creating a simOptions option set, and setting the AddNoise option to true. Additionally, you can change the default noise signal by specifying the NoiseData option.

  • H is the noise transfer function and is defined in sys.

  • δu is an optional input offset subtracted from the input signal, u(t), before the input is used to simulate the model. Specify an input offset by setting the InputOffset option using simOptions.

  • δy is an optional output offset added to the output response, y(t), after simulation. Specify an output offset by setting the OutputOffset option using simOptions.

For more information on specifying simulation initial conditions, input and output offsets, and noise signal data, see simOptions. For multiexperiment data, you can specify these options separately for each experiment.

Introduced before R2006a

Was this topic helpful?