%% Convert Discrete-Time Dynamic System to 2-DOF Standard-Form PID Controller
% Convert a discrete-time dynamic system that represents a 2-DOF PID controller with derivative filter to standard pidstd2 form.
% The following state-space matrices represent a discrete-time 2-DOF PID controller with a sample time of 0.05 s.

A = [1,0;0,0.6643];
B = [0.05,-0.05; -0.004553,0.3357];
C = [0.2301,10.66];
D = [0.8905,-11.79];
Ts = 0.05;
sys = ss(A,B,C,D,Ts);

%
% When you convert sys to 2-DOF PID form, the result depends on which discrete integrator formulas you specify for the conversion. For instance, use the default, ForwardEuler, for both the integrator and the derivative.

C2fe = pidstd2(sys)

%
% Now convert using the Trapezoidal formula.

C2trap = pidstd2(sys,'IFormula','Trapezoidal','DFormula','Trapezoidal')

% The displays show the difference in resulting coefficient values and functional form.
% For some dynamic systems, attempting to use the Trapezoidal or BackwardEuler integrator formulas yields invalid results, such as negative Ti, Td, or N values. In such cases, pidstd2 returns an error.
% Copyright 2015 The MathWorks, Inc.