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

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

% Copyright 2015 The MathWorks, Inc.