Simulation and Prediction at the Command Line
Simulation and Prediction Commands
Note
If you estimated a linear model from detrended data and want to simulate or predict the
output at the original operation conditions, use retrend
to add trend data back into the simulated or predicted output.
Command  Description  Example 

compare 
Determine how closely the simulated model response matches the measured output signal. Plots simulated or predicted output of one or more models on top of the measured output. You should use an independent validation data set as input to the model. 
To plot fivestepahead predicted output of the model compare(data,mod,5) Note Omitting the third argument assumes an infinite horizon and results in the comparison of the simulated response to the input data. 
sim 
Simulate and plot the model output only. 
To simulate the response of the model sim(model,data) 
predict 
Predict and plot the model output only. 
To perform onestepahead prediction of the response for the model
predict(model,data,1) Use the following syntax to compute yhat = predict(m,[y u],k)

forecast 
Forecast a time series into the future. 
To forecast the value of a time series in an arbitrary number of steps into the future, use the following command: forecast(model,past_data,K) Here, 
Initial Conditions in Simulation and Prediction
The process of computing simulated and predicted responses over a time range starts by
using the initial conditions to compute the first few output values. The
sim
, forecast
, and predict
commands provide options and default settings for handling initial conditions.
Simulation: Default initial conditions are zero for all
model types except the idnlgrey
model, whose default initial conditions are
the internal model initial states (model property x0
). You can specify other
initial conditions using the InitialCondition
simulation option. For more
information on simulation options, see simOptions
.
Use the compare
command to validate models by simulation because its
algorithm estimates the initial states of a model to optimize the model fit to a given data set.
You can also use compare
to return the estimated initial conditions for
followon simulation and comparison with the same data set. These initial conditions can be in
the form of an initial state vector (statespace models) or an initialCondition
object
(transfer function or polynomial models.)
If you are using sim
to validate the quality of the identified model,
you need to use the input signal from the validation data set and also
account for initial condition effects. The simulated and the measured responses
differ in the first few samples if the validation data set output contains initial condition
effects that are not captured when you simulate the model. To minimize this difference, estimate
the initial state values or the initialCondition
model from the data using
either findstates
(statespace models) or compare
(all
LTI models) and specify these initial states using the InitialCondition
simulation option (see simOptions
). For example, compute the initial states
that optimize the fit of the model m
to the output data in
z
:
% Estimate the initial states X0est = findstates(m,z); % Simulate the response using estimated initial states opt = simOptions('InitialCondition',X0est); sim(m,z.InputData,opt)
For an example of obtaining and using initialCondition
models, see Apply Initial Conditions when Simulating Identified Linear Models.
Prediction: Default initial conditions depend on the type
of model. You can specify other initial conditions using the InitialCondition
option (see predictOptions
). For example, compute the initial
states that optimize the 1stepahead predicted response of the model m
to
the output data z
:
opt = predictOptions('InitialCondition','estimate'); [Yp,IC] = predict(m,z,1,opt);
This command returns the estimated initial conditions as the output argument
IC
. For information about other ways to specify initial states, see the
predictOptions
reference page.
Simulate a ContinuousTime StateSpace Model
This example shows how to simulate a continuoustime statespace model using a random binary input u
and a sample time of 0.1 s
.
Consider the following statespace model:
$$\begin{array}{l}\underset{}{\overset{\dot{}}{x}}=\left[\begin{array}{cc}1& 1\\ 0.5& 0\end{array}\right]x+\left[\begin{array}{c}1\\ 0.5\end{array}\right]u+\left[\begin{array}{c}0.5\\ 0.5\end{array}\right]e\\ y=\left[\begin{array}{cc}1& 0\end{array}\right]x+e\end{array}$$
where e is Gaussian white noise with variance 7.
Create a continuoustime statespace model.
A = [1 1; 0.5 0]; B = [1;0.5]; C = [1 0]; D = 0; K = [0.5;0.5]; % Ts = 0 indicates continuous time model_ss = idss(A,B,C,D,K,'Ts',0,'NoiseVariance',7);
Create a random binary input.
u = idinput(400,'rbs',[0 0.3]);
Create an iddata
object with empty output to represent just the input signal.
data = iddata([],u); data.ts = 0.1;
Simulate the output using the model
opt = simOptions('AddNoise',true);
y = sim(model_ss,data,opt);
Simulate Model Output with Noise
This example shows how you can create input data and a model, and then use the data and the model to simulate output data.
In this example, you create the following ARMAX model with Gaussian noise e:
$$\begin{array}{l}y(t)1.5y(t1)+0.7y(t2)=\\ u(t1)+0.5u(t2)+e(t)e(t1)+0.2e(t1)\end{array}$$
Then, you simulate output data with random binary input u
.
Create an ARMAX model.
m_armax = idpoly([1 1.5 0.7],[0 1 0.5],[1 1 0.2]);
Create a random binary input.
u = idinput(400,'rbs',[0 0.3]);
Simulate the output data.
opt = simOptions('AddNoise',true);
y = sim(m_armax,u,opt);
The 'AddNoise'
option specifies to include in the simulation the Gaussian noise e
present in the model. Set this option to false
(default behavior) to simulate the noisefree response to the input u
, which is equivalent to setting e
to zero.
See Also
sim
 predict
 forecast
 compare
Related Examples
 Compare Simulated Output with Measured Validation Data
 Forecast Multivariate Time Series
 Simulation and Prediction in the App