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.

CommandDescriptionExample
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 five-step-ahead predicted output of the model mod against the validation data data, use the following command:

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 model using input data data, use the following command:

sim(model,data)
predict

Predict and plot the model output only.

To perform one-step-ahead prediction of the response for the model model and input data data, use the following command:

predict(model,data,1)

Use the following syntax to compute k-step-ahead prediction of the output signal using model m:

yhat = predict(m,[y u],k)

predict computes the prediction results only over the time range of data. It does not forecast results beyond the available data range.

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, model is a time series model, past_data is a record of the observed values of the time series, and K is the forecasting horizon.

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 follow-on simulation and comparison with the same data set. These initial conditions can be in the form of an initial state vector (state-space 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 (state-space 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 1-step-ahead 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 Continuous-Time State-Space Model

This example shows how to simulate a continuous-time state-space model using a random binary input u and a sample time of 0.1 s.

Consider the following state-space model:

$\begin{array}{l}\underset{}{\overset{˙}{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 continuous-time state-space 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

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\left(t\right)-1.5y\left(t-1\right)+0.7y\left(t-2\right)=\\ u\left(t-1\right)+0.5u\left(t-2\right)+e\left(t\right)-e\left(t-1\right)+0.2e\left(t-1\right)\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.

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 noise-free response to the input u , which is equivalent to setting e to zero. 