## How the Software Computes Nonlinear ARX Model Output

This topic describes how the software evaluates the output of nonlinearity estimators and uses this output to compute the response of a nonlinear ARX model.

### Evaluating Nonlinearities

Evaluating the predicted output of a nonlinearity for a specific
regressor value *x* requires that you first extract
the nonlinearity *F* and regressors from the model:

F = m.OutputFcn; x = getreg(m,'all',data) % computes regressors

Evaluate *F*(*x*):

y = evaluate(F,x)

where `x`

is a row vector of regressor values.

You can also evaluate predicted output values at multiple time
instants by evaluating *F* for several regressor
vectors simultaneously:

y = evaluate(F,[x1;x2;x3])

**Simulation and Prediction of Sigmoid Network**

This example shows how the software computes the simulated and predicted output of a nonlinear ARX model as a result of evaluating the output of its nonlinearity estimator for given regressor values.

Estimate nonlinear ARX model with sigmoid network nonlinearity.

load twotankdata estData = iddata(y,u,0.2,'Tstart',0); M = nlarx(estData,[1 1 0],'idSigmoidNetwork');

**Explore Nonlinear ARX Model**

Inspect the model properties and estimation result.

present(M)

M = Nonlinear ARX model with 1 output and 1 input Inputs: u1 Outputs: y1 Regressors: Linear regressors in variables y1, u1 Output function: Sigmoid network with 10 units Sample time: 0.2 seconds Status: Termination condition: Maximum number of iterations reached.. Number of iterations: 20, Number of function evaluations: 243 Estimated using NLARX on time domain data "estData". Fit to estimation data: 96.31% (prediction focus) FPE: 4.804e-05, MSE: 4.666e-05 More information in model's "Report" property.

This command provides information about input and output variables, regressors, and nonlinearity estimator.

Inspect the nonlinearity estimator.

```
NL = M.OutputFcn;
class(NL) % nonlinearity class
```

ans = 'idSigmoidNetwork'

NL

NL = Sigmoid Network Inputs: y1(t-1), u1(t) Output: y1(t) Nonlinear Function: Sigmoid network with 10 units Linear Function: initialized to [-0.161 -0.105] Output Offset: initialized to 0.00119 NonlinearFcn: '<Sigmoid units and their parameters>' LinearFcn: '<Linear function parameters>' Offset: '<Offset parameters>'

Inspect the sigmoid network parameter values.

NL.Parameters;

**Predict Output**

The model output is:

*y1(t)= f(y1(t-1),u1(t))*

where *f* is the sigmoid network function. The model regressors *y1(t-1)* and *u1(t)* are inputs to the nonlinearity estimator. Time *t* is a discrete variable representing *kT* , where `k = 0, 1, ... `

, and *T* is the sampling interval. In this example, `T=0.2`

second.

The output prediction equation is:

*yp(t)=f(y1_meas(t-1),u1_meas(t))*

where *yp(t)* is the predicted value of the response at time *t*. *y1_meas(t-1)* and *u1_meas(t)* are the measured output and input values at times *t-1* and *t*, respectively.

Computing the predicted response includes:

Computing regressor values from input-output data.

Evaluating the nonlinearity for given regressor values.

Specify zero initial states.

x0 = 0;

The model has one state because there is only one delayed term `y1(t-1)`

. The number of states is equal to `sum(getDelayInfo(M))`

.

Compute the predicted output at time *t*=0.

```
RegValue = [0,estData.u(1)]; % input to nonlinear function f
yp_0 = evaluate(NL,RegValue);
```

`RegValue`

is the vector of regressors at `t=0`

. The predicted output is *yp(t=0)=f(y1_meas(t=-1),u1_meas(t=0))*. In terms of MATLAB® variables, this output is `f(0,estData.u(1))`

, where

*y1_meas(t*=0) is the measured output value at`t=0`

, which is to`estData.y(1)`

.*u1_meas(t*=1) is the second input data sample`estData.u(2)`

.

Perform one-step-ahead prediction at all time values for which data is available.

RegMat = getreg(M,[],estData,x0); yp = evaluate(NL,RegMat.Variables);

This code obtains a matrix of regressors `RegMat`

for all the time samples using `getreg`

. `RegMat`

has as many rows as there are time samples, and as many columns as there are regressors in the model - two, in this example.

These steps are equivalent to the predicted response computed in a single step using predict:

yp_direct = predict(M,estData,1,'InitialState',x0); % compare t = estData.SamplingInstants; plot(t,yp, t,yp_direct.OutputData,'.')

**Simulate Output**

The model output is:

*y1(t)=f(y1(t-1),u1(t))*

where *f* is the sigmoid network function. The model regressors *y1(t-1)* and *u1(t)* are inputs to the nonlinearity estimator. Time *t* is a discrete variable representing *kT* , where *k*= 0, 1,.., and *T* is the sampling interval. In this example, *T*=0.2 second.

The simulated output is:

*ys(t) = f(ys(t-1),u1_meas(t))*

where *ys(t)* is the simulated value of the response at time *t*. The simulation equation is the same as the prediction equation, except that the past output value `ys(t-1)`

results from the simulation at the previous time step, rather than the measured output value.

Computing the simulated response includes:

Computing regressor values from input-output data using simulated output values.

Evaluating the nonlinearity for given regressor values.

Specify zero initial states.

x0 = 0;

The model has one state because there is only one delayed term *y1(t-1)*. The number of states is equal to `sum(getDelayInfo(M))`

.

Compute the simulated output at time *t* =0, *ys(t=0)*.

RegValue = [0,estData.u(1)]; ys_0 = evaluate(NL,RegValue);

RegValue is the vector of regressors at *t*=0. *ys(t=0)=f(y1(t=-1),u1_meas(t=0))*. In terms of MATLAB variables, this output is `f(0,estData.u(1))`

, where

*y1(t=-1)*is the initial state`x0 (=0)`

.*u1_meas(t=0)*is the value of the input at*t*=0, which is the first input data sample`estData.u(1)`

.

Compute the simulated output at time *t*=1, *ys(t*=1).

RegValue = [ys_0,estData.u(2)]; ys_1 = evaluate(NL,RegValue);

The simulated output *ys(t=1)=f(ys(t=0),u1_meas(t=1))*. In terms of MATLAB variables, this output is `f(ys_0,estData.u(2))`

, where

*ys(t=0)*is the simulated value of the output at t=0.*u1_meas(t=1)*is the second input data sample estData.u(2).

Compute the simulated output at time *t*=2.

RegValue = [ys_1,estData.u(3)]; ys_2 = evaluate(NL,RegValue);

Unlike for output prediction, you cannot use `getreg`

to compute regressor values for all time values. You must compute regressors values at each time sample separately because the output samples required for forming the regressor vector are available iteratively, one sample at a time.

These steps are equivalent to the simulated response computed in a single step using `sim(idnlarx)`

.

ys = sim(M,estData,x0);

**Perform Low-Level Computation**

Perform a low-level computation of the nonlinearity response for the `idSigmoidNetwork`

function:

$$\begin{array}{l}F(x)=(x-r)PL+{a}_{1}f\left(\left(x-r\right)Q{b}_{1}+{c}_{1}\right)+\dots \\ +{a}_{n}f\left(\left(x-r\right)Q{b}_{n}+{c}_{n}\right)+d\end{array}$$

where *f* is the sigmoid function, given by the following equation:

$$f(z)=\frac{1}{{e}^{-z}+1}$$

In `F(x)`

, the input to the sigmoid function is `x-r`

. `x`

is the regressor value and `r`

is regressor mean, computed from the estimation data. $${a}_{n}$$ , $${n}_{n}$$, and $${c}_{n}$$ are the network parameters stored in the model property `M.nl.par`

, where `M`

is an `idnlarx`

object.

Compute the output value at time t=1, when the regressor values are `x=[estData.y(1),estData.u(2)]`

:

Assign values to the parameters in the expression for `F(x)`

.

x = [estData.y(1),estData.u(2)]; % regressor values at t=1 r = NL.Input.Mean; P = NL.LinearFcn.InputProjection; L = NL.LinearFcn.Value'; d = NL.Offset.Value; Q = NL.NonlinearFcn.Parameters.InputProjection; aVec = NL.NonlinearFcn.Parameters.OutputCoefficient; % [a_1; a_2; ...] cVec = NL.NonlinearFcn.Parameters.Translation; % [c_1; c_2; ...] bMat = NL.NonlinearFcn.Parameters.Dilation; % [b_1; b_2; ...]

Compute the linear portion of the response (plus offset).

yLinear = (x-r)*P*L+d;

Compute the nonlinear portion of the response.

f = @(z)1/(exp(-z)+1); % anonymous function for sigmoid unit yNonlinear = 0; for k = 1:length(aVec) fInput = (x-r)*Q* bMat(:,k)+cVec(k); yNonlinear = yNonlinear+aVec(k)*f(fInput); end