# setEstimator

Modify a model predictive controller’s state estimator

## Description

`setEstimator(`

sets the gain matrices used for estimation of the states of an MPC controller. For more
information, see State Estimator Equations.`MPCobj`

,`L`

,`M`

)

`setEstimator(`

specifies that
controller state estimation will be performed by a user-supplied procedure. This option
suppresses calculation of `MPCobj`

,'custom')`L`

and `M`

. When the controller
is operating in this way, the procedure must supply the state estimate `x[n|n]`

to the controller at the beginning of each control interval.

## Examples

### Design State Estimator by Pole Placement

Design an estimator using pole placement, assuming the linear system $$AM=L$$ is solvable.

Create a plant model.

G = tf({1,1,1},{[1 .5 1],[1 1],[.7 .5 1]});

To improve the clarity of this example, call `mpcverbosity`

to suppress messages related to working with an MPC controller.

`old_status = mpcverbosity('off');`

Create a model predictive controller for the plant. Specify the controller sample time as 0.2 seconds.

MPCobj = mpc(G, 0.2);

Obtain the default state estimator gain.

[~,M,A1,Cm1] = getEstimator(MPCobj);

Calculate the default observer poles.

e = eig(A1-A1*M*Cm1); abs(e)

`ans = `*6×1*
0.9402
0.9402
0.8816
0.8816
0.7430
0.9020

Specify faster observer poles.

new_poles = [.8 .75 .7 .85 .6 .81];

Compute a state-gain matrix that places the observer poles at `new_poles`

.

L = place(A1',Cm1',new_poles)';

`place`

returns the controller-gain matrix, whereas you want to compute the observer-gain matrix. Using the principle of duality, which relates controllability to observability, you specify the transpose of `A1`

and `Cm1`

as the inputs to `place`

. This function call yields the observer gain transpose.

Obtain the estimator gain from the state-gain matrix.

M = A1\L;

Specify `M`

as the estimator for `MPCobj`

.

setEstimator(MPCobj,L,M)

The pair, ($${A}_{1},{C}_{m1}$$), describing the overall state-space realization of the combination of plant and disturbance models must be observable for the state estimation design to succeed. Observability is checked in Model Predictive Control Toolbox software at two levels: (1) observability of the plant model is checked *at construction* of the MPC object, provided that the model of the plant is given in state-space form; (2) observability of the overall extended model is checked *at initialization* of the MPC object, after all models have been converted to discrete-time, delay-free, state-space form and combined together.

Restore `mpcverbosity`

.

mpcverbosity(old_status);

## Input Arguments

`MPCobj`

— MPC controller

MPC controller object

MPC controller, specified as an MPC controller object. Use the `mpc`

command to create the MPC controller.

`L`

— Kalman gain matrix for time update

`A*M`

(default) | matrix

Kalman gain matrix for the time update, specified as a matrix. The dimensions of
`L`

are
*n _{x}*-by-

*n*, where

_{ym}*n*is the total number of controller states, and

_{x}*n*is the number of measured outputs.

_{ym}If `L`

is empty, it defaults to `L = A*M`

, where
`A`

is the state-transition matrix.

`M`

— Kalman gain matrix for measurement update

`0`

(default) | matrix

Kalman gain matrix for the measurement update, specified as a matrix. The dimensions of
`L`

are
*n _{x}*-by-

*n*, where

_{ym}*n*is the total number of controller states, and

_{x}*n*is the number of measured outputs.

_{ym}If `M`

is omitted or empty, it defaults to
a zero matrix, and the state estimator becomes a Luenberger observer.

## Algorithms

### State Estimator Equations

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. For more information, see Controller State Estimation.

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)$$

## Version History

**Introduced in R2014b**

## See Also

`getEstimator`

| `mpc`

| `mpcstate`

| `kalman`

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

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.

# Select a Web Site

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: .

You can also select a web site from the following list:

## How to Get Best Site Performance

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

### Americas

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

### Europe

- 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)