Main Content

Estimate parameters of nonlinear ARX model

specifies
the nonlinearity to use for model estimation.`sys`

= nlarx(`Data`

,`Orders`

,`Nonlinearity`

)

specifies
additional attributes of the estimated model using one or more `sys`

= nlarx(`Data`

,`Orders`

,`Nonlinearity`

,`Name,Value`

)`Name,Value`

pair
arguments. These attributes include the nonlinear and custom regressor
structure, and the data
properties of the `idnlarx`

model.

specifies
the nonlinearity to use for model estimation.`sys`

= nlarx(`Data`

,`LinModel`

,`Nonlinearity`

)

specifies
additional attributes of the `sys`

= nlarx(`Data`

,`LinModel`

,`Nonlinearity`

,`Name,Value`

)`idnlarx`

model structure
using one or more `Name,Value`

pair arguments.

refines
the parameters of the nonlinear ARX model, `sys`

= nlarx(`Data`

,`sys0`

)`sys0`

.

Use this syntax to:

Update the parameters of a previously estimated model to improve the fit to the estimation data. In this case, the estimation algorithm uses the parameters of

`sys0`

as initial guesses.Estimate the parameters of a model previously created using the

`idnlarx`

constructor. Prior to estimation, you can configure the model properties using dot notation.

Load the estimation data.

`load twotankdata;`

Create an `iddata`

object from the estimation data with a sample time of 0.2 min.

Ts = 0.2; z = iddata(y,u,Ts);

Estimate the nonlinear ARX model.

sys = nlarx(z,[4 4 1]);

Create time and data arrays.

dt = 0.01; t = 0:dt:10; y = 10*sin(2*pi*t)+rand(size(t));

Create an `iddata`

object with no input signal specified.

z = iddata(y',[],dt);

Estimate the nonlinear ARX model.

sys = nlarx(z,2);

Load the estimation data.

`load twotankdata;`

Create an `iddata`

object from the estimation data.

z = iddata(y,u,0.2);

Create a wavelet network nonlinearity estimator with 5 units.

`NL = wavenet('NumberOfUnits',5);`

Estimate the nonlinear ARX model.

sys = nlarx(z,[4 4 1],NL);

Generating a custom network nonlinearity requires the definition of a user-defined unit function.

Define the unit function and save it as `gaussunit.m`

.

% Copyright 2015 The MathWorks, Inc. function [f, g, a] = gaussunit(x) f = exp(-x.*x); if nargout>1 g = -2*x.*f; a = 0.2; end

Create a custom network nonlinearity using the `gaussunit`

function.

H = @gaussunit; CNet = customnet(H);

Load the estimation data.

```
load iddata1;
```

Estimate a nonlinear ARX model using the custom network.

sys = nlarx(z1,[1 2 1],CNet);

Load the estimation data.

`load motorizedcamera;`

Create an `iddata`

object.

z = iddata(y,u,0.02,'Name','Motorized Camera','TimeUnit','s');

`z`

is an `iddata`

object with 6 inputs and 2 outputs.

Specify the model orders.

Orders = [ones(2,2),2*ones(2,6),ones(2,6)];

Specify different nonlinearity estimators for each output channel.

`NL = [wavenet('NumberOfUnits',2),linear];`

Estimate the nonlinear ARX model.

sys = nlarx(z,Orders,NL);

Load the estimation data and create an `iddata`

object.

```
load motorizedcamera;
z = iddata(y,u,0.02);
```

Specify the model orders.

Orders = [ones(2,2),2*ones(2,6),ones(2,6)];

Estimate a nonlinear ARX model using a sigmoidnet nonlinearity with 4 units for all output channels.

`m = nlarx(z,Orders,sigmoidnet('numberOfUnits',4));`

Load the estimation data.

`load iddata1;`

Create a cell array with two custom regressors.

C = {'y1(t-1)^2','y1(t-2)*u1(t-3)'};

Estimate a nonlinear ARX model with custom regressors and no standard regressors.

sys = nlarx(z1,[0 0 0],'linear','CustomRegressors',C);

Load the estimation data.

`load iddata1;`

Define a custom regressor object for `y1(t-1)^2`

.

`C1 = customreg(@(x)x^2,{'y1'},[1]);`

Define a custom regressor object for `y1(t-2)*u1(t-3)`

.

C2 = customreg(@(x,y)x*y,{'y1','u1'},[2 3]);

Create a custom regressor object array.

C = [C1,C2];

Estimate a nonlinear ARX model with custom regressors.

sys = nlarx(z1,[0 0 0],'linear','CustomRegressors',C);

List the model regressors.

getreg(sys);

Regressors: y1(t-1)^2 y1(t-2)*u1(t-3)

Load the estimation data.

`load iddata1;`

Estimate a Nonlinear ARX model using the `'search'`

option.

sys = nlarx(z1,[4 4 1],'sigmoidnet','NonlinearRegressors','search');

List the model nonlinear regressor indices.

sys.NonlinearRegressors

`ans = `*1×4*
3 5 6 7

List all of the model regressors.

getreg(sys)

Regressors: y1(t-1) y1(t-2) y1(t-3) y1(t-4) u1(t-1) u1(t-2) u1(t-3) u1(t-4)

The optimum set of nonlinear regressors for this model includes `y1(t-3)`

, `u1(t-1)`

, `u1(t-2)`

, and `u1(t-3)`

.

Load the estimation data.

`load iddata1;`

Create a sigmoid network nonlinearity estimator with no linear term.

SNL = sigmoidnet('LinearTerm','off');

Estimate the nonlinear ARX model.

sys = nlarx(z1,[2 2 1],SNL);

Load the estimation data.

`load throttledata;`

Detrend the data.

Tr = getTrend(ThrottleData); Tr.OutputOffset = 15; DetrendedData = detrend(ThrottleData,Tr);

Estimate the linear ARX model.

LinearModel = arx(DetrendedData,[2 1 1]);

Estimate the nonlinear ARX model using the linear model. The model orders, delays, and linear parameters of `NonlinearModel`

are derived from `LinearModel`

.

NonlinearModel = nlarx(ThrottleData,LinearModel);

Load the estimation data.

`load iddata1;`

Create an `idnlarx`

model.

sys = idnlarx([2 2 1]);

Configure the model using dot notation to set the following parameters:

Use a sigmoid network nonlinearity

Search for an optimum nonlinear regressor subset

sys.Nonlinearity = 'sigmoidnet'; sys.NonlinearRegressors = 'search';

Estimate a nonlinear ARX model with the structure and properties specified in the `idnlarx`

object.

sys = nlarx(z1,sys);

If an estimation stops at a local minimum, you can perturb the model using `init`

and reestimate the model.

Load the estimation data.

`load iddata1;`

Estimate the initial nonlinear model using specific nonlinear regressors.

sys1 = nlarx(z1,[4 2 1],'sigmoidnet','NonlinearRegressors',[1:3]);

Randomly perturb the model parameters to avoid local minima.

sys2 = init(sys1);

Estimate the new nonlinear model with the perturbed values.

sys2 = nlarx(z1,sys2);

Load the estimation data.

`load twotankdata;`

Create an `iddata`

object from the estimation data.

z = iddata(y,u,0.2);

Create an `nlarxOptions`

option set specifying a simulation error minimization objective and a maximum of 50 estimation iterations.

```
opt = nlarxOptions;
opt.Focus = 'simulation';
opt.SearchOptions.MaxIterations = 50;
```

Estimate the nonlinear ARX model.

`sys = nlarx(z,[4 4 1],'sigmoidnet',opt);`

Load the regularization example data.

load regularizationExampleData.mat nldata;

Create a `sigmoidnet`

nonlinearity with 30 units, and specify the model orders.

```
NL = sigmoidnet('NumberOfUnits',30);
Orders = [1 2 1];
```

Create an estimation option set and set the estimation search method to `lm`

.

opt = nlarxOptions('SearchMethod','lm');

Estimate an unregularized model.

sys = nlarx(nldata,Orders,NL,opt);

Configure the regularization `Lambda`

parameter.

opt.Regularization.Lambda = 1e-8;

Estimate a regularized model.

sysR = nlarx(nldata,Orders,NL,opt);

Compare the two models.

compare(nldata,sys,sysR)

The large negative fit result for the unregularized model indicates a poor fit to the data. Estimating a regularized model produces a significantly better result.

`Data`

— Time-domain estimation data`iddata`

objectTime-domain estimation data, specified as an `iddata`

object. `Data`

can
have one or more output channels and zero or more input channels.
Data must be uniformly sampled and cannot contain missing (`NaN`

)
samples.

`Orders`

— Model orders and delays1-by-3 vector of positive integers | 1-by-3 vector of matrices

Model orders and delays for defining the regressor configuration,
specified as a 1-by-3 vector, `[na nb nk]`

.

For a model with *n _{y}* output
channels and

`na`

is an*n*-by-_{y}*n*matrix, where_{y}`na(i,j)`

specifies the number of regressors from the*j*th output used to predict the*i*th output.`nb`

is an*n*-by-_{y}*n*matrix, where_{u}`nb(i,j)`

specifies the number of regressors from the*j*th input used to predict the*i*th output.`nk`

is an*n*-by-_{y}*n*matrix, where_{u}`nk(i,j)`

specifies the lag in the*j*th input used to predict the*i*th output.

na = [1 2; 2 3] nb = [1 2 3; 2 3 1]; nk = [2 0 3; 1 0 5];

The estimation data for this system has three inputs (`u1`

, `u2`

, `u3`

)
and two outputs (`y1`

, `y2`

). Consider
the regressors used to predict output, `y2(t)`

:

Since

`na(2,:)`

is`[2 3]`

, the contributing regressors from the outputs are:`y1(t-1)`

and`y1(t-2)`

`y2(t-1)`

,`y2(t-2)`

, and`y2(t-3)`

Since

`nb(2,:)`

is`[2 3 1]`

and`nk(2,:)`

is`[1 0 5]`

, the contributing regressors from the inputs are:`u1(t-1)`

and`u1(t-2)`

`u2(t)`

,`u2(t-1)`

, and`u2(t-2)`

`u3(t-5)`

**Note**

The minimum lag for regressors based on output variables is
always `1`

, while the minimum lag for regressors
based on input variables is dictated by `nk`

. Use `getreg`

to view the complete set of regressors
used by the nonlinear ARX model.

`Nonlinearity`

— Nonlinearity estimator`'wavenet'`

(default) | `'sigmoidnet'`

| `'treepartition'`

| `'linear'`

| nonlinearity estimator object | array of nonlinearity estimator objectsNonlinearity estimator, specified as one of the following:

`'wavenet'` or `wavenet` object | Wavelet network |

`'sigmoidnet'` or `sigmoidnet` object | Sigmoid network |

`'treepartition'` or `treepartition` object | Binary-tree |

`'linear'` or `[]` or `linear` object | Linear function |

`neuralnet` object | Neural network — Requires Deep Learning Toolbox™. |

`customnet` object | Custom network — Similar to `sigmoidnet` ,
but with a user-defined replacement for the sigmoid function. |

For more information, see Available Nonlinearity Estimators for Nonlinear ARX Models.

Specifying a character vector, for example `'sigmoidnet'`

,
creates a nonlinearity estimator object with default settings. Alternatively,
you can specify nonlinearity estimator settings in two ways:

Use the associated nonlinearity estimator function with Name-Value pair arguments.

`NL = sigmoidnet('NumberOfUnits',10);`

Create and modify a default nonlinearity estimator object.

NL = sigmoidnet; NL.NumberOfUnits = 10;

For `ny`

output channels, you can specify nonlinear
estimators individually for each channel by setting `Nonlinearity`

to
an `ny`

-by-1 array of nonlinearity estimator objects.
To specify the same nonlinearity for all outputs, specify `Nonlinearity`

as
a character vector or a single nonlinearity estimator object.

**Example: **`'sigmoidnet'`

specifies
a sigmoid network nonlinearity with a default configuration.

**Example: **`treepartition('NumberOfUnits',5)`

specifies
a binary-tree nonlinearity with `5`

terms in the
binary tree expansion.

**Example: **`[wavenet('NumberOfUnits',10);sigmoidnet]`

specifies
different nonlinearity estimators for two output channels.

`LinModel`

— Discrete time input-output polynomial model of ARX structure`idpoly`

modelDiscrete time input-output polynomial model of ARX structure,
specified as an `idpoly`

model. Create this object
using the `idpoly`

constructor or estimate it using
the `arx`

command.

`sys0`

— Nonlinear ARX model`idnlarx`

modelNonlinear ARX model, specified as an `idnlarx`

model. `sys0`

can
be:

A model previously estimated using

`nlarx`

. The estimation algorithm uses the parameters of`sys0`

as initial guesses. In this case, use`init`

to slightly perturb the model properties to avoid being trapped in local minima.sys = init(sys); sys = nlarx(data,sys);

A model previously created using

`idnlarx`

and with properties set using dot notation. Use this method to avoid complicated Name-Value pair syntax when configuring multiple model properties. For example, usesys1 = idnlarx([4 3 1]); sys1.Nonlinearity = 'treepartition'; sys1.CustomRegressors = {'sin(u1(t-1))'}; sys1.NonlinearRegressors = 'search'; sys2 = nlarx(data,sys1);

in place of the equivalent

sys2 = nlarx(data,[4,3,1],'treepartition','CustomRegressors',... {'sin(u1(t-1))'},'NonlinearRegressors','search');

`Options`

— Estimation options`nlarxOptions`

option setEstimation options for nonlinear ARX model identification, specified
as an `nlarxOptions`

option set.

Specify optional
comma-separated pairs of `Name,Value`

arguments. `Name`

is
the argument name and `Value`

is the corresponding value.
`Name`

must appear inside quotes. You can specify several name and value
pair arguments in any order as
`Name1,Value1,...,NameN,ValueN`

.

`'NonlinearRegressors','output'`

specifies
that only the regressors containing output variables are used as inputs
to the nonlinear block of the model.`'TimeVariable'`

— Independent variable name`'t'`

(default) | character vectorIndependent variable name, specified as the comma-separated
pair consisting of `'TimeVariable'`

and a character
vector. For example, `'t'`

.

`'CustomRegressors'`

— Regressors constructed from combinations of inputs and outputs`{}`

(default) | cell array of character vectors | array of `customreg`

objectsRegressors constructed from combinations of inputs and outputs,
specified as the comma-separated pair consisting of `'CustomRegressors'`

and
one of the following for single-output systems:

Cell array of character vectors. For example:

`{'y1(t-3)^3','y2(t-1)*u1(t-3)','sin(u3(t-2))'}`

Each character vector must represent a valid formula for a regressor contributing towards the prediction of the model output. The formula must be written using the input and output names and the time variable name as variables.

Array of custom regressor objects, created using

`customreg`

or`polyreg`

.

For a model with *n _{y}* outputs,
specify an

`customreg`

object arrays or character
arrays.These regressors are in addition to the standard regressors
based on `Orders`

.

**Example: **`'CustomRegressors',{'y1(t-3)^3','y2(t-1)*u1(t-3)'}`

**Example: **`'CustomRegressors',{'sin(u3(t-2))'}`

`'NonlinearRegressors'`

— Subset of regressors that enter as inputs to the nonlinear block of the model`'all'`

(default) | `'output'`

| `'input'`

| `'standard'`

| `'custom'`

| `'search'`

| vector of positive integers | `[]`

| cell arraySubset of regressors that enter as inputs to the nonlinear block
of the model, specified as the comma-separated pair consisting of `'NonlinearRegressors'`

and
one of the following values:

`'all'`

— All regressors`'output'`

— Regressors containing output variables`'input'`

— Regressors containing input variables`'standard'`

— Standard regressors`'custom'`

— Custom regressors`'search'`

— The estimation algorithm performs a search for the best regressor subset. This is useful when you want to reduce a large number of regressors entering the nonlinear function block of the nonlinearity estimator. This option must be applied to all output models simultaneously.`[]`

— No regressors. This creates a linear-in-regressor model.Vector of regressor indices. To determine the number and order of regressors, use

`getreg`

.

For a model with multiple outputs, specify a cell array of *n _{y}* elements,
where

`[]`

or any of the character vector options
alone, for example `'standard'`

.**Example: **`'NonlinearRegressors','search'`

performs
a best regressor search for the only output of a single output model,
or all of the outputs of a multiple output model.

**Example: **`'NonlinearReg','input'`

applies only
input regressors to the inputs of the nonlinear function.

**Example: **`'NonlinearRegressors',{'input','output'}`

applies
input regressors to the first output, and output regressors to the
second output of a model with two outputs.

`sys`

— Nonlinear ARX model`idnlarx`

objectNonlinear ARX model that fits the given estimation data, returned
as an `idnlarx`

object. This model
is created using the specified model orders, nonlinearity estimator,
and estimation options.

Information about the estimation results and options used is
stored in the `Report`

property of the model. The
contents of `Report`

depend upon the choice of nonlinearity
and estimation focus you specified for `nlarx`

. `Report`

has
the following fields:

Report Field | Description | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

`Status` | Summary of the model status, which indicates whether the model was created by construction or obtained by estimation. | ||||||||||||||||||

`Method` | Estimation command used. | ||||||||||||||||||

`Fit` | Quantitative assessment of the estimation, returned as a structure. See Loss Function and Model Quality Metrics for more information on these quality metrics. The structure has the following fields:
| ||||||||||||||||||

`Parameters` | Estimated values of model parameters. | ||||||||||||||||||

`OptionsUsed` | Option set used for estimation. If no custom options
were configured, this is a set of default options. See | ||||||||||||||||||

`RandState` | State of the random number stream at the start of estimation. Empty,
| ||||||||||||||||||

`DataUsed` | Attributes of the data used for estimation, returned as a structure with the following fields:
| ||||||||||||||||||

`Termination` | Termination conditions for the iterative search used for prediction error minimization, returned as a structure with the following fields:
For estimation methods that do not require numerical search optimization,
the |

For more information on using `Report`

, see Estimation Report.

A nonlinear ARX model consists of model regressors and a nonlinearity estimator. The nonlinearity estimator comprises both linear and nonlinear functions that act on the model regressors to give the model output. This block diagram represents the structure of a nonlinear ARX model in a simulation scenario.

The software computes the nonlinear ARX model output *y* in
two stages:

It computes regressor values from the current and past input values and past output data.

In the simplest case, regressors are delayed inputs and outputs, such as

*u(t-1)*and*y(t-3)*. These kind of regressors are called*standard regressors*. You specify the standard regressors using the model orders and delay. For more information, see Nonlinear ARX Model Orders and Delay. You can also specify*custom*regressors, which are nonlinear functions of delayed inputs and outputs. For example,*u*(*t*-1)**y*(*t*-3). To create a set of polynomial type regressors, use`polyreg`

.By default, all regressors are inputs to both the linear and the nonlinear function blocks of the nonlinearity estimator. You can choose a subset of regressors as inputs to the nonlinear function block.

It maps the regressors to the model output using the nonlinearity estimator block. The nonlinearity estimator block can include linear and nonlinear blocks in parallel. For example:

$$F(x)={L}^{T}(x-r)+d+g\left(Q(x-r)\right)$$

Here,

*x*is a vector of the regressors, and*r*is the mean of the regressors*x*. $${L}^{T}(x)+d$$ is the output of the linear function block and is affine when d ≠ 0.*d*is a scalar offset. $$g\left(Q(x-r)\right)$$ represents the output of the nonlinear function block.*Q*is a projection matrix that makes the calculations well conditioned. The exact form of*F*(*x*) depends on your choice of the nonlinearity estimator. You can select from available nonlinearity estimators, such as tree-partition networks, wavelet networks, and multilayer neural networks. You can also exclude either the linear or the nonlinear function block from the nonlinearity estimator.When estimating a nonlinear ARX model, the software computes the model parameter values, such as

*L*,*r*,*d*,*Q*, and other parameters specifying*g*.

Resulting nonlinear ARX models are `idnlarx`

objects
that store all model data, including model regressors and parameters
of the nonlinearity estimator. For more information about these objects,
see Nonlinear Model Structures.

Accelerate code by automatically running computation in parallel using Parallel Computing Toolbox™.

Parallel computing support is available for estimation using the
`lsqnonlin`

search method (requires Optimization Toolbox™). To enable parallel computing, use `nlarxOptions`

, set `SearchMethod`

to
`'lsqnonlin'`

, and set
`SearchOptions.Advanced.UseParallel`

to
`true`

.

For example:

```
opt = nlarxOptions;
opt.SearchMethod = 'lsqnonlin';
opt.SearchOptions.Advanced.UseParallel = true;
```

A modified version of this example exists on your system. Do you want to open this version instead?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- América Latina (Español)
- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)