## Controller State Estimation

### Controller State Variables

As the controller operates, it uses its current state, xc, 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}\left(k\right)& {x}_{id}^{T}\left(k\right)& \begin{array}{cc}{x}_{od}^{T}\left(k\right)& {x}_{n}^{T}\left(k\right)\end{array}\end{array}\right].$`

Here,

• xc is the controller state, comprising nxp + nxid + nxod + nxn state variables.

• xp is the plant model state vector, of length nxp.

• xid is the input disturbance model state vector, of length nxid.

• xod is the output disturbance model state vector, of length nxod.

• xn is the measurement noise model state vector, of length nxn.

Thus, the variables comprising xc 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 ny 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 kth control interval, the controller state is estimated with the following steps:

1. Obtain the following data:

• xc(k|k–1) — Controller state estimate from previous control interval, k–1

• uact(k–1) — Manipulated variable (MV) actually used in the plant from k–1 to k (assumed constant)

• uopt(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

• ym(k) — Current measured plant outputs

• Bu, Bv — Columns of observer parameter B corresponding to u(k) and v(k) inputs

• Cm — Rows of observer parameter C corresponding to measured plant outputs

• Dmv — Rows and columns of observer parameter 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.

2. Revise xc(k|k–1) when uact(k–1) and uopt(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]$`
3. 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]$`
4. 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 xc(k|k) to solve the quadratic program at interval k. The solution is uopt(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, wid(k), wod(k), and wn(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, xc, are detectable. (If not, or if the observer is numerically close to undetectability, the Kalman gain calculation fails, generating an error message.)

• Stochastic inputs wid(k), wod(k), and wn(k) are independent white noise, each with zero mean and identity covariance.

• Additional white noise wu(k) and wv(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, Cm, and the following covariance matrices:

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)

• xc(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, Bu, Bv, C, Dv — State observer constants, where Bu, Bv, and Dv denote columns of the B and D matrices corresponding to inputs u and v. Du is a zero matrix because of no direct feedthrough

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 nxn elements of xc(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.