%% Design Kalman Filter for MIMO Plant
% Consider a plant with three inputs, one of which represents process noise w, and two measured outputs. The plant has four states.
%
% Assuming the following state-space matrices, create sys.
%

A = [-0.71  0.06 -0.19 -0.17;
      0.06 -0.52 -0.03  0.30;
     -0.19 -0.03 -0.24 -0.02;
     -0.17  0.30 -0.02 -0.41];

B = [ 1.44  2.91   0;
     -1.97  0.83 -0.27;
     -0.20  1.39  1.10;
     -1.2   0    -0.28];

C = [ 0    -0.36 -1.58 0.28;
     -2.05  0     0.51 0.03];

D = zeros(2,3);

sys = ss(A,B,C,D);
sys.InputName = {'u1','u2','w'};
sys.OutputName = {'y1','y2'};

%
% Because the plant has only one process noise input, the covariance Q is a scalar. For this example, assume the process noise has unit covariance.

Q = 1;

% kalman uses the dimensions of Q to determine which inputs are known and which are the noise inputs. For scalar Q, kalman assumes one noise input and uses the last input, unless you specify otherwise (see Plant with Unmeasured Outputs).
% For the measurement noise on the two outputs, specify a 2-by-2 noise covariance matrix. For this example, use a unit variance for the first output, and variance of 1.3 for the second output. Set the off-diagonal values to zero to indicate that the two noise channels are uncorrelated.

R = [1 0;
     0 1.3];

%
% Design the Kalman filter.

[kalmf,L,P] = kalman(sys,Q,R);

%
% Examine the inputs and outputs.  kalman uses the InputName, OutputName, InputGroup, and OutputGroup properties of kalmf to help you keep track of what the inputs and outputs of kalmf represent.

kalmf.InputGroup
kalmf.InputName
kalmf.OutputGroup
kalmf.OutputName

% Thus the two known inputs u1 and u2 are the first two inputs of kalmf and the two measured outputs y1 and y2 are the last two inputs to kalmf. For the outputs of kalmf, the first two are the estimated outputs, and the remaining four are the state estimates. To use the Kalman filter, connect these inputs to the plant and noise signals in a manner analogous to that shown for a SISO plant in Kalman Filtering.
%
% Copyright 2020 The MathWorks, Inc.