Accelerating the pace of engineering and science

# Documentation

## Simulating and Predicting Model Output

### Why Simulate or Predict Model Output

You primarily use a model is to simulate its output, i.e., calculate the output (y(t)) for given input values. You can also predict model output, i.e., compute a qualified guess of future output values based on past observations of system's inputs and outputs. For more information, see Definition: Simulation and Prediction.

You also validate linear parametric models and nonlinear models by checking how well the simulated or predicted output of the model matches the measured output. You can use either time or frequency domain data for simulation or prediction. For frequency domain data, the simulation and prediction results are products of the Fourier transform of the input and frequency function of the model. For more information, see Simulation and Prediction in the App and Simulation and Prediction at the Command Line.

Simulation provides a better validation test for the model than prediction. However, how you validate the model output should match how you plan to use the model. For example, if you plan to use your model for control design, you can validate the model by predicting its response over a time horizon that represents the dominating time constants of the model.

### Definition: Simulation and Prediction

Simulation means computing the model response using input data and initial conditions. The time samples of the model response match the time samples of the input data used for simulation.

For a continuous-time system, simulation means solving a differential equation. For a discrete-time system, simulation means directly applying the model equations.

For example, consider a dynamic model described by a first-order difference equation that uses a sampling interval of 1 second:

y(t) + ay(t–1) = bu(t–1),

where y is the output and u is the input. For parameter values a = –0.9 and b = 1.5, the equation becomes:

y(t) – 0.9y(t–1) = 1.5u(t–1).

Suppose you want to compute the values y(1), y(2), y(3),... for given input values u(0) = 2, u(1) = 1, u(2) = 4,...Here, y(1) is the value of output at the first sampling instant. Using initial condition of y(0) = 0, the values of y(t) for times t = 1, 2 and 3 can be computed as:

y(1) = 0.9y(0) + 1.5u(0) = 0.9*0 + 1.5*2 = 3

y(2) = 0.9y(1) + 1.5u(1) = 0.9*3 + 1.5*1 = 4.2

y(3) = 0.9y(2) + 1.5u(2) = 0.9*4.2 + 1.5*4 = 9.78

...

Prediction forecasts the model response k steps ahead into the future using the current and past values of measured input and output values. k is called the prediction horizon, and corresponds to predicting output at time kTs, where Ts is the sampling interval.

For example, suppose you use sensors to measure the input signal u(t) and output signal y(t) of the physical system, described in the previous first-order equation. At the tenth sampling instant (t = 10), the output y(10) is 16 mm and the corresponding input u(10) is 12 N. Now, you want to predict the value of the output at the future time t = 11. Using the previous equation:

y(11) = 0.9y(10) + 1.5u(10)

Hence, the predicted value of future output y(11) at time t = 10 is:

y(11) = 0.9*16 + 1.5*12 = 32.4

In general, to predict the model response k steps into the future (k≥1) from the current time t, you should know the inputs up to time t+k and outputs up to time t:

yp(t+k) = f(u(t+k),u(t+k–1),...,u(t),u(t–1),...,u(0)
y(t),y(t–1),y(t–2),...,y(0))

u(0) and y(0) are the initial states. f() represents the predictor, which is a dynamic model whose form depends on the model structure. For example, the one-step-ahead predictor yp of the model y(t) + ay(t–1) = bu(t) is:

yp(t+1) = –ay(t) + bu(t+1)

The difference between prediction and simulation is that in prediction, the past values of outputs used for calculation are measured values while in simulation the outputs are themselves a result of calculation using inputs and initial conditions.

The way information in past outputs is used depends on the disturbance model H of the model. For the previous dynamic model, $H\left(z\right)=\frac{1}{1+a{z}^{-1}}$. In models of Output-Error (OE) structure (H(z) = 1), there is no information in past outputs that can be used for predicting future output values. In this case, predictions and simulations coincide. For state-space models (idss), output-error structure corresponds to models with K=0. For polynomial models (idpoly), this corresponds to models with polynomials a=c=d=1.

 Note:   Prediction with k=∞ means that no previous outputs are used in the computation and prediction returns the same result as simulation.

Both simulation and prediction require initial conditions, which correspond to the states of the model at the beginning of the simulation or prediction.

 Tip   If you do not know the initial conditions and have input and output measurements available, you can estimate the initial condition using this toolbox.

### Simulation and Prediction in the App

#### How to Plot Simulated and Predicted Model Output

To create a model output plot for parametric linear and nonlinear models in the System Identification app, select the Model output check box in the Model Views area. By default, this operation estimates the initial states from the data and plots the output of selected models for comparison.

To include or exclude a model on the plot, click the corresponding model icon in the System Identification app. Active models display a thick line inside the Model Board icon.

To learn how to interpret the model output plot, see Interpreting the Model Output Plot.

To change plot settings, see Changing Model Output Plot Settings.

For general information about creating and working with plots, see Working with Plots.

#### Interpreting the Model Output Plot

The following figure shows a sample Model Output plot, created in the System Identification app.

The model output plot shows different information depending on the domain of the input-output validation data, as follows:

• For time-domain validation data, the plot shows simulated or predicted model output.

• For frequency-domain data, the plot shows the amplitude of the model response to the frequency-domain input signal. The model response is equal to the product of the Fourier transform of the input and the model's frequency function.

• For frequency-response data, the plot shows the amplitude of the model frequency response.

For linear models, you can estimate a model using time-domain data, and then validate the model using frequency domain data. For nonlinear models, you can only use time-domain data for both estimation and validation.

The right side of the plot displays the percentage of the output that the model reproduces (Best Fit), computed using the following equation:

In this equation, y is the measured output, $\stackrel{^}{y}$ is the simulated or predicted model output, and $\overline{y}$ is the mean of y. 100% corresponds to a perfect fit, and 0% indicates that the fit is no better than guessing the output to be a constant ($\stackrel{^}{y}=\overline{y}$).

Because of the definition of Best Fit, it is possible for this value to be negative. A negative best fit is worse than 0% and can occur for the following reasons:

• The estimation algorithm failed to converge.

• The model was not estimated by minimizing $|y-\stackrel{^}{y}|$. Best Fit can be negative when you minimized 1-step-ahead prediction during the estimation, but validate using the simulated output $\stackrel{^}{y}$.

• The validation data set was not preprocessed in the same way as the estimation data set.

#### Changing Model Output Plot Settings

The following table summarizes the Model Output plot settings.

Model Output Plot Settings

ActionCommand

Display confidence intervals.

 Note:   Confidence intervals are only available for simulated model output of linear models. Confidence internal are not available for nonlinear ARX and Hammerstein-Wiener models.
• To display the dashed lines on either side of the nominal model curve, select Options > Show confidence intervals. Select this option again to hide the confidence intervals.

• To change the confidence value, select Options > Set % confidence level, and choose a value from the list.

• To enter your own confidence level, select Options > Set confidence level > Other. Enter the value as a probability (between 0 and 1) or as the number of standard deviations of a Gaussian distribution.

Change between simulated output or predicted output.

 Note:   Prediction is only available for time-domain validation data.
• Select Options > Simulated output or Options > k step ahead predicted output.

• To change the prediction horizon, select Options > Set prediction horizon, and select the number of samples.

• To enter your own prediction horizon, select Options > Set prediction horizon > Other. Enter the value in terms of the number of samples.

Display the actual output values (Signal plot), or the difference between model output and measured output (Error plot).

Select Options > Signal plot or Options > Error plot.

(Time-domain validation data only)
Set the time range for model output and the time interval for which the Best Fit value is computed.

Select Options > Customized time span for fit and enter the minimum and maximum time values. For example:

[1 20]

(Multiple-output system only)
Select a different output.

Select the output by name in the Channel menu.

#### Definition: Confidence Interval

The confidence interval corresponds to the range of output values with a specific probability of being the actual output of the system. The toolbox uses the estimated uncertainty in the model parameters to calculate confidence intervals and assumes the estimates have a Gaussian distribution.

For example, for a 95% confidence interval, the region around the nominal curve represents the range of values that have a 95% probability of being the true system response. You can specify the confidence interval as a probability (between 0 and 1) or as the number of standard deviations of a Gaussian distribution. For example, a probability of 0.99 (99%) corresponds to 2.58 standard deviations.

 Note:   The calculation of the confidence interval assumes that the model sufficiently describes the system dynamics and the model residuals pass independence tests.

In the app, you can display a confidence interval on the plot to gain insight into the quality of a linear model. To learn how to show or hide confidence interval, see Changing Model Output Plot Settings.

### Simulation and Prediction at the Command Line

#### Summary of 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 the retrend command to the simulated or predicted output.
CommandDescriptionExample
compare

Use this command for model validation to 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)


Note that predict computes the prediction results only over the time range of data. It does not perform any forecasting of 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 polynomial (idpoly), process (idproc) and transfer-function (idtf) models . For state-space (idss) and grey-box (idgrey) models, 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 the findstates command and specify these initial states as input arguments to the sim command. 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 predict reference page for the corresponding model type.

### Compare Simulated Output with Measured Data

This example shows how to validate an estimated model by comparing the simulated model output with measured data.

Create estimation and validation data.

load iddata1;
ze = z1(1:150);
zv = z1(151:300);


Estimate an ARMAX model.

m = armax(ze,[2 3 1 0]);


Compare simulated model output with measured data.

compare(zv,m);


### 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:

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.

### 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 sampling interval of 0.1 s.

Consider the following state-space model:

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);


### Predict Using Time-Series Model

This example shows how to evaluate how well a time-series model predicts the response for a given prediction horizon.

In this example, y is the original series of monthly sales figures. You use the first half of the measured data to estimate the time-series model and test the model's ability to forecast sales six months ahead using the entire data set.

Simulate figures for sales growth every month before settling down.

rng('default');
t = (0:0.2:19)';
yd = 1-exp(-0.2*t) + randn(size(t))/50;
y = iddata(yd,[],0.2);
plot(y);


Select the first half as estimation data.

y1 = y(1:48);


Estimate a second-order autoregressive model with noise integration using the data.

m = ar(y1, 2, 'yw', 'IntegrateNoise', true);


yhat = predict(m,y,6);


Forecast the response 10 steps beyond the available data's time range.

yfuture = forecast(m,y,10);


Plot the measured, predicted and forecasted outputs.

plot(y,yhat,yfuture);
legend({'measured', 'predicted', 'forecasted'},'location','southeast');