## Controller State Estimation

### Controller State Variables

As the controller operates, it uses its current state,
*x _{c}*, as the basis for predictions. By
definition, the state vector is the following:

${x}_{c}^{T}\left(k\right)=\left[\begin{array}{ccc}{x}_{p}^{T}(k)& {x}_{id}^{T}(k)& \begin{array}{cc}{x}_{od}^{T}(k)& {x}_{n}^{T}(k)\end{array}\end{array}\right].$

Here,

*x*is the controller state, comprising_{c}*n*+_{xp}*n*+_{xid}*n*+_{xod}*n*state variables._{xn}*x*is the plant model state vector, of length_{p}*n*._{xp}*x*is the input disturbance model state vector, of length_{id}*n*._{xid}*x*is the output disturbance model state vector, of length_{od}*n*._{xod}*x*is the measurement noise model state vector, of length_{n}*n*._{xn}

Thus, the variables comprising *x _{c}* represent
the models appearing in the following diagram of the MPC system.

Some of the state vectors may be empty. If not, they appear in the sequence defined within each model.

By default, the controller updates its state automatically using the latest plant measurements. See State Estimation for details. Alternatively, the custom state estimation feature allows you to update the controller state using an external procedure, and then supply these values to the controller. See Custom State Estimation for details.

### State Observer

Combination of the models shown in the diagram yields the state observer:

$\begin{array}{c}{x}_{c}\left(k+1\right)=A{x}_{c}\left(k\right)+B{u}_{o}\left(k\right)\\ y\left(k\right)=C{x}_{c}\left(k\right)+D{u}_{o}\left(k\right).\end{array}$

MPC controller uses the state observer in the following ways:

To estimate values of unmeasured states needed as the basis for predictions (see State Estimation).

To predict how the controller’s proposed manipulated variable (MV) adjustments will affect future plant output values (see Output Variable Prediction).

The observer’s input signals are the dimensionless plant manipulated and measured disturbance inputs, and the white noise inputs to the disturbance and noise models:

${u}_{o}^{T}\left(k\right)=\left[\begin{array}{ccc}{u}^{T}\left(k\right)& {v}^{T}\left(k\right)& \begin{array}{cc}\begin{array}{cc}{w}_{id}^{T}\left(k\right)& {w}_{od}^{T}\left(k\right)\end{array}& {w}_{n}^{T}\left(k\right)\end{array}\end{array}\right].$

The observer’s outputs are the *n _{y}*
dimensionless plant outputs.

In terms of the parameters defining the four models shown in the diagram, the observer’s parameters are:

$$\begin{array}{cc}A=\left[\begin{array}{cccc}{A}_{p}& {B}_{pd}{C}_{id}& 0& 0\\ 0& {A}_{id}& 0& 0\\ 0& 0& {A}_{od}& 0\\ 0& 0& 0& {A}_{n}\end{array}\right],& B=\left[\begin{array}{ccccc}{B}_{pu}& {B}_{pv}& {B}_{pd}{D}_{id}& 0& 0\\ 0& 0& {B}_{id}& 0& 0\\ 0& 0& 0& {B}_{od}& 0\\ 0& 0& 0& 0& {B}_{n}\end{array}\right]\\ C=\left[\begin{array}{cccc}{C}_{p}& {D}_{pd}{C}_{id}& {C}_{od}& \left[\begin{array}{c}{C}_{n}\\ 0\end{array}\right]\end{array}\right],& D=\left[\begin{array}{ccccc}0& {D}_{pv}& {D}_{pd}{D}_{id}& {D}_{od}& \left[\begin{array}{c}{D}_{n}\\ 0\end{array}\right]\end{array}\right].\end{array},$$

Here, the plant and output disturbance models are resequenced so that the measured outputs precede the unmeasured outputs.

### State Estimation

In general, the controller states are unmeasured and must be estimated. By default, the controller uses a steady-state Kalman filter that derives from the state observer.

At the beginning of the *k*th control interval, the controller state is estimated with the following steps:

Obtain the following data:

*x*(_{c}*k*|*k*–1) — Controller state estimate from previous control interval,*k*–1*u*(^{act}*k*–1) — Manipulated variable (MV) actually used in the plant from*k*–1 to*k*(assumed constant)*u*(^{opt}*k*–1) — Optimal MV recommended by MPC and assumed to be used in the plant from*k*–1 to*k**v*(*k*) — Current measured disturbances*y*(_{m}_{k}) — Current measured plant outputs*B*,_{u}*B*— Columns of observer parameter_{v}*B*corresponding to*u*(*k*) and*v*(*k*) inputs*C*— Rows of observer parameter C corresponding to measured plant outputs_{m}*D*— Rows and columns of observer parameter_{mv}*D*corresponding to measured plant outputs and measured disturbance inputs*L*,*M*— Constant Kalman gain matrices

Plant input and output signals are scaled to be dimensionless prior to use in calculations.

Revise

*x*(_{c}*k*|*k*–1) when*u*(^{act}*k*–1) and*u*(^{opt}*k*–1) are different.$${x}_{c}^{rev}\left(k\text{\hspace{0.17em}}\text{|}k-1\right)={x}_{c}\left(k\text{\hspace{0.17em}}\text{|}k-1\right)+{B}_{u}\left[{u}^{act}\left(k-1\right)-{u}^{opt}\left(k-1\right)\right]$$

Compute the innovation.

$e\left(k\right)={y}_{m}\left(k\right)-\left[{C}_{m}{x}_{c}^{rev}\left(k\text{\hspace{0.17em}}\text{|}k-1\right)+{D}_{mv}v\left(k\right)\right]$

Update the controller state estimate to account for the latest measurements.

${x}_{c}\left(k\text{\hspace{0.17em}}\text{|}k\right)={x}_{c}^{rev}\left(k\text{\hspace{0.17em}}\text{|}k-1\right)+Me\left(k\right)$

Then, the software uses the current state estimate

*x*(_{c}*k*|*k*) to solve the quadratic program at interval*k*. The solution is*u*(^{opt}*k*), the MPC-recommended manipulated-variable value to be used between control intervals*k*and*k*+1.Finally, the software prepares for the next control interval assuming that the unknown inputs,

*w*(_{id}*k*),*w*(_{od}*k*), and*w*(_{n}*k*) assume their mean value (zero) between times*k*and*k*+1. The software predicts the impact of the known inputs and the innovation as follows:$${x}_{c}\left(k+1|k\right)=A{x}_{c}^{rev}\left(k|k-1\right)+{B}_{u}{u}^{opt}\left(k\right)+{B}_{v}v\left(k\right)+Le\left(k\right)$$

### Built-in Steady-State Kalman Gains Calculation

Model Predictive Control Toolbox™ software uses the `kalman`

command to calculate Kalman estimator
gains *L* and *M*. The following assumptions apply:

State observer parameters

*A*,*B*,*C*,*D*are time-invariant.Controller states,

*x*, are detectable. (If not, or if the observer is numerically close to undetectability, the Kalman gain calculation fails, generating an error message.)_{c}Stochastic inputs

*w*(_{id}*k*),*w*(_{od}*k*), and*w*(_{n}*k*) are independent white noise, each with zero mean and identity covariance.Additional white noise

*w*(_{u}*k*) and*w*(_{v}*k*) with the same characteristics adds to the dimensionless*u*(*k*) and*v*(*k*) inputs respectively. This improves estimator performance in certain cases, such as when the plant model is open-loop unstable.

Without loss of generality, set the *u*(*k*) and
*v*(*k*) inputs to zero. The effect of the stochastic
inputs on the controller states and measured plant outputs is:

$\begin{array}{c}{x}_{c}\left(k+1\right)=A{x}_{c}\left(k\right)+Bw\left(k\right)\\ {y}_{m}\left(k\right)={C}_{m}{x}_{c}\left(k\right)+{D}_{m}w\left(k\right).\end{array}$

Here,

${w}^{T}\left(k\right)=\left[\begin{array}{ccc}{w}_{u}^{T}\left(k\right)& {w}_{v}^{T}\left(k\right)& \begin{array}{cc}\begin{array}{cc}{w}_{id}^{T}\left(k\right)& {w}_{od}^{T}\left(k\right)\end{array}& {w}_{n}^{T}\left(k\right)\end{array}\end{array}\right].$

Inputs to the `kalman`

command are the state observer parameters
*A*, *C _{m}*, and the following
covariance matrices:

$\begin{array}{c}Q=E\left\{Bw{w}^{T}{B}^{T}\right\}=B{B}^{T}\\ R=E\left\{{D}_{m}w{w}^{T}{D}_{m}^{T}\right\}={D}_{m}{D}_{m}^{T}\\ N=E\left\{Bw{w}^{T}{D}_{m}^{T}\right\}=B{D}_{m}^{T}.\end{array}$

Here, *E*{...} denotes the expectation.

### Output Variable Prediction

Model Predictive Control requires prediction of noise-free future plant outputs used in optimization. This is a key application of the state observer (see State Observer).

In control interval *k*, the required data are as follows:

*p*— Prediction horizon (number of control intervals, which is greater than or equal to 1)*x*(_{c}*k*|*k*) — Controller state estimates (see State Estimation)*v*(*k*) — Current measured disturbance inputs (MDs)*v*(*k*+*i*|*k*) — Projected future MDs, where*i*=1:*p*–1. If you are not using MD previewing, then*v*(*k*+*i*|*k*) =*v*(*k*).*A*,*B*,_{u}*B*,_{v}*C*,*D*— State observer constants, where_{v}*B*,_{u}*B*, and_{v}*D*denote columns of the_{v}*B*and*D*matrices corresponding to inputs*u*and*v*.*D*is a zero matrix because of no direct feedthrough_{u}

Predictions assume that unknown white noise inputs are zero (their expectation). Also,
the predicted plant outputs are to be noise-free. Thus, all terms involving the measurement
noise states disappear from the state observer equations. This is equivalent to zeroing the
last *n _{xn}* elements of

*x*(

_{c}*k*|

*k*).

Given the above data and simplifications, for the first step the state observer predicts:

${x}_{c}\left(k+1\text{\hspace{0.17em}}\text{|}k\right)=A{x}_{c}\left(k\text{\hspace{0.17em}}\text{|}k\right)+{B}_{u}u\left(k\text{\hspace{0.17em}}\text{|}k\right)+{B}_{v}v\left(k\right).$

Continuing for successive steps, *i* = 2:*p*, the
state observer predicts:

$${x}_{c}\left(k+i\text{\hspace{0.17em}}\text{|}k\right)=A{x}_{c}\left(k+i-1\text{\hspace{0.17em}}\text{|}k\right)+{B}_{u}u\left(k+i-1\text{\hspace{0.17em}}\text{|}k\right)+{B}_{v}v\left(k+i-1\text{\hspace{0.17em}}\text{|}k\right).$$

At any step, *i* = 1:*p*, the predicted
noise-free plant outputs are:

$y\left(k+i|k\right)=C{x}_{c}\left(k+i|k\right)+{D}_{v}v\left(k+i|k\right).$

All of these equations employ dimensionless plant input and output variables. See Specify Scale Factors. The equations also assume zero offsets. Inclusion of nonzero offsets is straightforward.

For faster computations, the MPC controller uses an alternative form of the above equations in which constant terms are computed and stored during controller initialization. See QP Matrices.

## See Also

### Functions

`kalman`

|`review`

|`getEstimator`

|`setEstimator`