## Simulation and Prediction at the Command Line

### 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 States 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. `sim`, `forecast`, and `predict` commands provide defaults for handling initial conditions.

Simulation: Default initial conditions are zero for all model types except `idnlgrey` model, in which case the default initial conditions are the internal model initial states (model property `x0`). You can specify other initial conditions using the `InitialCondition` simulation option (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.

If you use `sim`, the simulated and the measured responses might differ when the initial conditions of the estimated model and the system that measured the validation data set differ—especially at the beginning of the response. To minimize this difference, estimate the initial state values from the data using `findstates` and specify these initial states using the `InitialCondition` simulation option (see `simOptions`). For example, to 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) ```

Prediction: Default initial conditions depend on the type of model. You can specify other initial conditions using the `InitialCondition` option (see `predictOptions`). For example, to 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,X0est] = predict(m,z,1,opt);```

This command returns the estimated initial states as the output argument `X0est`. For information about other ways to specify initials 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

```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\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.

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