%% Array of 2-DOF PID Controllers
% Create a 2-by-3 grid of 2-DOF PI controllers with proportional gain ranging from 1–2 across the array rows and integral gain ranging from 5–9 across columns.
% To build the array of PID controllers, start with arrays representing the gains.

Kp = [1 1 1;2 2 2];
Ki = [5:2:9;5:2:9];

%
% When you pass these arrays to the pid2 command, the command returns the array of controllers.

pi_array = pid2(Kp,Ki,0,0,0.5,0,'Ts',0.1,'IFormula','BackwardEuler');
size(pi_array)

%
% If you provide scalar values for some coefficients, pid2 automatically expands them and assigns the same value to all entries in the array. For instance, in this example, Kd = Tf = 0, so that all entries in the array are PI controllers. Also, all entries in the array have b = 0.5.
% Access entries in the array using array indexing. For dynamic system arrays, the first two dimensions are the I/O dimensions of the model, and the remaining dimensions are the array dimensions. Therefore, the following command extracts the (2,3) entry in the array.

pi23 = pi_array(:,:,2,3)

%
% You can also build an array of PID controllers using the stack command.

C2 = pid2(1,5,0.1,0,0.5,0.5);         % PID controller
C2f = pid2(1,5,0.1,0.5,0.5,0.5);      % PID controller with filter
pid_array = stack(2,C2,C2f);          % stack along 2nd array dimension

%
% These commands return a 1-by-2 array of controllers.

size(pid_array)

% All PID controllers in an array must have the same sample time, discrete integrator formulas, and dynamic system properties such as InputName and OutputName.
% Copyright 2015 The MathWorks, Inc.