## Estimate State-Space Models at the Command Line

### Black Box vs. Structured State-Space Model Estimation

You can estimate state-space models in two ways at the command line, depending upon your prior knowledge of the nature of the system and your requirements.

#### Black Box Estimation

In this approach, you specify the model order, and, optionally, additional model structure
attributes that configure the overall structure of the state-space matrices. You call `ssest`

, `ssregest`

or `n4sid`

with data and model order as primary input arguments, and use name-value
pairs to specify any additional attributes, such as model sample time, presence of feedthrough,
absence of noise component, etc. You do not work directly with the coefficients of the
*A*, *B*, *C*, *D*,
*K*, and *X0* matrices.

#### Structured Estimation

In this approach, you create and configure an `idss`

model that contains the initial values for all the system matrices. You use
the `Structure`

property of the `idss`

model to specify all
the parameter constraints. For example, you can designate certain coefficients of system
matrices as fixed and impose minimum/maximum bounds on the values of the others. For quick
configuration of the parameterization and whether to estimate feedthrough and disturbance
dynamics, use `ssform`

.

After configuring the `idss`

model with desired constraints, you
specify this model as an input argument to the `ssest`

command. You cannot use `n4sid`

or `ssregest`

for structured estimation.

**Note**

The structured estimation approach is also referred to as grey-box modeling. However, in this toolbox, the “grey box modeling” terminology is used only when referring to

`idgrey`

and`idnlgrey`

models.Using the structured estimation approach, you cannot specify relationships among state-space coefficients. Each coefficient is essentially considered to be independent of others. For imposing dependencies, or to use more complex forms of parameterization, use the

`idgrey`

model and`greyest`

estimator.

### Estimating State-Space Models Using ssest, ssregest and n4sid

You can estimate continuous-time and discrete-time state-space models using the iterative
estimation command `ssest`

that minimizes the prediction errors to obtain
maximum-likelihood values.

Use the following general syntax to both configure and estimate state-space models:

m = ssest(data,n,opt,Name,Value)

where `data`

is the estimation data, `n`

is the model
order, and `opt`

contains options for configuring the estimation of the
state-space models. These options include the handling of the initial conditions, input and
output offsets, estimation focus and search algorithm options. opt can be followed by name-value
pair input arguments that specify optional model structure attributes such as the presence of
feedthrough, the canonical form of the model, and input delay.

As an alternative to `ssest`

, you can use the noniterative subspace
estimators `n4sid`

or `ssregest`

:

m = n4sid(data,n,opt,Name,Value) m = ssregest(data,n,opt,Name,Value)

Unless you specify the sample time as a name-value pair input argument,
`n4sid`

and `ssregest`

estimate a discrete-time model,
while `ssest`

estimates a continuous-time model.

**Note**

`ssest`

uses `n4sid`

to initialize the state-space
matrices, and takes longer than `n4sid`

to estimate a model but typically
provides a better fit to data.

For information about validating your model, see Validating Models After Estimation

### Choosing the Structure of A, B, C Matrices

By default, all entries of the *A*, *B*, and
*C* state-space matrices are treated as free parameters. Using the
`Form`

name-value pair input argument of `ssest`

, you can choose various canonical forms, such as the companion and modal
forms, that use fewer parameters.

For more information about estimating a specific state-space parameterization, see:

### Choosing Between Continuous-Time and Discrete-Time Representations

For estimation of state-space models, you have the option of switching the model sample
time between zero and that of the estimation data. You can do this using the
`Ts`

name-value pair input argument.

By default,

`ssest`

estimates a continuous-time model. If you are using data set with nonzero sample time,`data`

, which includes all time domain data, you can also estimate a discrete-time model by using:`model = ssest(data,nx,'Ts',data.Ts);`

If you are using continuous-time frequency-domain data, you cannot estimate a discrete-time model.

By default,

`n4sid`

and`ssregest`

estimate a model whose sample time matches that of the data. Thus, for time-domain data,`n4sid`

and`ssregest`

deliver a discrete-time model. You can estimate a continuous-time model by using:`model = n4sid(data,nx,'Ts',0);`

or

`model = ssregest(data,nx,'Ts',0);`

### Choosing to Estimate D, K, and X0 Matrices

For state-space models with any parameterization, you can specify whether to estimate the
*D*, *K* and *X0* matrices, which
represent the input-to-output feedthrough, noise model and the initial states,
respectively.

For state-space models with structured parameterization, you can also specify to estimate
the *D* matrix. However, for free and canonical forms, the structure of the
*D* matrix is set based on your choice for the
`'Feedthrough'`

name-value pair input argument.

#### D Matrix

By default, the *D* matrix is not estimated and its value is fixed to
zero, except for static models.

**Black box estimation:**Use the`Feedthrough`

name-value pair input argument to denote the presence or absence of feedthrough from individual inputs. For example, in case of a two input model such that there is feedthrough from only the second input, use:`model = n4sid(data,n,'Feedthrough',[false true]);`

**Structured estimation:**Configure the values of the`init_sys.Structure.D`

, where`init_sys`

is an`idss`

model that represents the desired model structure. To force no feedthrough for the*i*-th input, set:init_sys.Structure.D.Value(:,i) = 0; init_sys.Structure.D.Free = true; init_sys.Structure.D.Free(:,i) = false;

The first line specifies the value of the

*i*-th column of D as zero. The next line specifies all the elements of*D*as free, estimable parameters. The last line specifies that the*i*-th column of the*D*matrix is fixed for estimation.Alternatively, use

`ssform`

with`'Feedthrough'`

name-value pair.

#### K Matrix

*K* represents the noise matrix of the model, such that the noise
component of the model is:.

$$\begin{array}{l}\dot{x}=Ax+Ke\\ {y}_{n}=Cx+e\end{array}$$

For frequency-domain data, no noise model is estimated and *K* is set
to 0. For time-domain data, *K* is estimated by default in the black box
estimation setup. *y ^{n}* is the contribution of the
disturbances to the model output.

**Black box estimation:**Use the`DisturbanceModel`

name-value pair input argument to indicate if the disturbance component is fixed to zero (specify`Value = 'none'`

) or estimated as a free parameter (specify`Value = 'estimate'`

). For example, use :model = n4sid(data,n,'DisturbanceModel','none');

**Structured estimation:**Configure the value of the`init_sys.Structure.K`

parameter, where`init_sys`

is an`idss`

model that represents the desired model structure. You can fix some*K*matrix coefficients to known values and prescribe minimum/maximum bounds for free coefficients. For example, to estimate only the first column of the*K*matrix for a two output model:`kpar = init_sys.Structure.K; kpar.Free(:,1) = true; kpar.Free(:,2) = false; kpar.Value(:,2) = 0; % second column value is fixed to zero init_sys.Structure.K = kpar;`

Alternatively, use

`ssform`

.

When not sure how to easily fix or free all coefficients of *K*,
initially you can omit estimating the noise parameters in *K* to focus on
achieving a reasonable model for the system dynamics. After estimating the dynamic model, you
can use `ssest`

to refine the model while configuring the
*K* parameters to be free. For example:

init_sys = ssest(data, n,'DisturbanceModel','none'); init_sys.Structure.K.Free = true; sys = ssest(data,init_sys);

where `init_sys`

is the dynamic model without noise.

To set *K* to zero in an existing model, you can set its
`Value`

to `0`

and `Free`

flag to
`false`

:

m.Structure.K.Value = 0; m.Structure.K.Free = false;

#### X0 Matrices

The initial state vector *X0* is obtained as the by-product of model
estimation. The `n4sid`

, `ssest`

and
`ssregest`

commands return the value of *X0* as their
second output arguments. You can choose how to handle initial conditions during model
estimation by using the `InitialState`

estimation option. Use `n4sidOptions`

(for `n4sid`

), `ssestOptions`

(for `ssest`

) or `ssregestOptions`

(for `ssregest`

) to create the estimation
option set. For example, in order to hold the initial states to zero during estimation using
`n4sid`

:

```
opt = n4sidOptions;
opt.InitialState = 'zero';
[m,X0] = n4sid(data,n,opt);
```

The returned `X0`

variable is a zero vector of length
`n`

.

When you estimate models using multiexperiment data, the `X0`

matrix
contains as many columns as data experiments.

For a complete list of values for the `InitialStates`

option, see Specifying Initial States for Iterative Estimation Algorithms.