%% Plant with Unmeasured Outputs
% Consider a plant with four inputs and two outputs. The first and third inputs are known, while the second and fourth inputs represent the process noise. The plant also has two outputs, but only the second of them is measured.
%
% Use the following state-space matrices to create sys.

A = [-0.37  0.14 -0.01  0.04;
     0.14 -1.89  0.98 -0.11;
    -0.01  0.98 -0.96 -0.14;
     0.04 -0.11 -0.14 -0.95];

B = [-0.07 -2.32  0.68  0.10;
     -2.49  0.08  0     0.83;
      0    -0.95  0     0.54;
     -2.19  0.41  0.45  0.90];

C = [ 0     0    -0.50 -0.38;
     -0.15 -2.12 -1.27  0.65];

D = zeros(2,4);

sys = ss(A,B,C,D,-1);    % Discrete with unspecified sample time

sys.InputName = {'u1','w1','u2','w2'};
sys.OutputName = {'yun','ym'};

% To use kalman to design a filter for this system, use the known and sensors input arguments to specify which inputs to the plant are known and which output is measured.

known = [1 3];
sensors = [2];

% Specify the noise covariances and design the filter.

Q = eye(2);
R = 1;
N = 0;

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

%
% Examining the input and output labels of kalmf shows the inputs that the filter expects and the outputs it returns.

kalmf.InputGroup
kalmf.InputName

% kalmf takes as inputs the two known inputs of sys and the noisy measured outputs of sys.

kalmf.OutputGroup

% The first output of kalmf is its estimate of the true value of the measured plant output. The remaining outputs are the state estimates.
%
% Copyright 2020 The MathWorks, Inc.