% ControlEngineering, 2019 (Springer), Keviczky Bars Hetthéssy Bányász
% Example 7.2 (7.25), p.262: lead-lag, 2. order
% solution fig. 7.13, p.263, optimal controller
Vs2=1; num2=conv([5  1],[6  1]); den2=conv([10  1],[8  1]);
%num2A=[5  1];  % plant remaining; --> assumed controller plus PT1 with Ts=6
%num2B=[2.5  1]; % half
%num2C=[5.5  1]; % +10  <== Vorzugsvariante
   
%num2help=1;    % plant remaining; --> 2 PT1 only
%den2help=conv(conv([10  1],[8  1]),[1  1]);%add. PT1 with 10% of largest TC

num22=conv([5  1],[6  1]);
den22=conv(conv([10  1],[8  1]),[6.6  1]); % 6+10%
den22B=conv(conv(conv([10  1],[8  1]),[6.6  1]),[5.5  1]); % 6+10%, 5+10%
%den23=conv(den22,[1  1]); % 10% of 10
%den23B=conv(den22B,[1  1]); % 10% of 10
% book, p.263, control structure given with inner optimal controller and
%              feed forward structure

% BO-Toolbox: PI undelayed
%RegPIu= bo_u_gen(12,Vs2,num2C,den2),
RegPIu= bo_u_gen(12,Vs2,num22,den22),
% RegPIu:  VR=,   TN=,   TR0=,
cn2_uPI=[RegPIu(2)  1], cd2_uPI=[RegPIu(3)  0],
% BO-Toolbox: PID undelayed
%RegPIDu= bo_u_gen(13,Vs2,num2help,den2help)
RegPIDu= bo_u_gen(13,Vs2,num22,den22B)
% RegPIDu:  VR=,   TN=,    TR0=,    TV=,
N=1*10^-3;   % alternative D-term approximation: apply Simulink PID block
cn2_PIDu=[RegPIDu(4)*RegPIDu(2)  RegPIDu(2)  1], 
cd2_PIDu=[N*RegPIDu(3)  RegPIDu(3)  0],

% BO-Toolbox: PI delayed
%[RegPId,A21,A31]= bo_d_gen(12,Vs2,num2C,den2), %A2,A3: solver check
[RegPId,A21,A31]= bo_d_gen(12,Vs2,num22,den22), %A2,A3: solver check
%RegPId: VR=,    TN=,    TR0=,
cn2_PId=[RegPId(2)  1], cd2_PId=[RegPId(3)  0],
fn2_PId=[0          1], fd2_PId=[RegPId(2)  1],

% BO-Toolbox: PID delayed
%[RegPIDd,A22,A32]= bo_d_gen(13,Vs2,num2help,den2help),
[RegPIDd,A22,A32]= bo_d_gen(13,Vs2,num22,den22B),
%RegPIDd: VR=,    TN=,    TR0=,    TV=,
N=1*10^-3;                                      % N: see comment above
cn2_PIDd=[RegPIDd(4)*RegPIDd(2)  RegPIDd(2)  1],
cd2_PIDd=[N*RegPIDd(3)           RegPIDd(3)  0],
fn2_PIDd=[0                      0           1],
fd2_PIDd=[RegPIDd(4)*RegPIDd(2)  RegPIDd(2)  1],

disp('"LK_ex072_sim.slx" will be called - compare parameters')
LK_ex072_sim; sim('LK_ex072_sim',250),
disp('Repeat bo_d_gen call at MATLAB promt, if search algorithm fails')
