%Cvejn, J.: The magnitude optimum design of the PI controller for plants 
%with complex roots and dead time. Archives of Control Sciences, vol. 32, 
%no. 1, pp. 5-35, 2022; plant examples: number 3, 4, 5 and 7
%  controllers: PID,  optimized pre-filters

p3den=     [ 0.4907^2   2*0.8660*0.4907   1 ];             p3dt=0.05;
p4den=     [ 0.4950^2   2*0.7071*0.4950   1 ];             p4dt=0.2;
p5den=conv([ 0.4^2     2*0.5*0.4        1 ],[ 0.3   1 ]);  p5dt=0;
p7den=     [ 1         2*0.5*1          1 ];               p7dt=0.2;
Gp3=tf(1,p3den,'Variable','p','IODelay',p3dt); % plants: LTI definitions 
Gp4=tf(1,p4den,'Variable','p','IODelay',p4dt);
Gp5=tf(1,p5den,'Variable','p','IODelay',p5dt);
Gp7=tf(1,p7den,'Variable','p','IODelay',p7dt);

% undelayed inputs, controller parameter sequence: VR_TN_TR0_TV
R3u=bo_u_gen(13,1,1,p3den,p3dt,39);  R3uzae=[ R3u(4)*R3u(2)  R3u(2)  1]; 
                                     R3unen=[ 0              R3u(3)  0];
R4u=bo_u_gen(13,1,1,p4den,p4dt,39);  R4uzae=[ R4u(4)*R4u(2)  R4u(2)  1]; 
                                     R4unen=[ 0              R4u(3)  0];
R5u=bo_u_gen(13,1,1,p5den);          R5uzae=[ R5u(4)*R5u(2)  R5u(2)  1]; 
                                     R5unen=[ 0              R5u(3)  0];
R7u=bo_u_gen(13,1,1,p7den,p7dt,39);  R7uzae=[ R7u(4)*R7u(2)  R7u(2)  1]; 
                                     R7unen=[ 0              R7u(3)  0];
C3u=tf(R3uzae,R3unen);    % controllers (undelayed inputs): LTI definitions
C4u=tf(R4uzae,R4unen);
C5u=tf(R5uzae,R5unen);
C7u=tf(R7uzae,R7unen);

% delayed inputs, controller parameter sequence: VR_TN_TR0_TV
% Rxd(4) =TV;    Rxd(2) =TN;    Rxd(3) = TR0 = integrator
optim=optimset('MaxFunEvals',8000,'MaxIter',4000);
R3d=bo_d_gen(13,1,1,p3den,p3dt,39,[],optim);
                                     R3dzae=[ R3d(4)*R3d(2)  R3d(2)  1]; 
                                     R3dnen=[ 0              R3d(3)  0];
R4d=bo_d_gen(13,1,1,p4den,p4dt,39);  R4dzae=[ R4d(4)*R4d(2)  R4d(2)  1];
                                     R4dnen=[ 0              R4d(3)  0];
R5d=bo_d_gen(13,1,1,p5den);          R5dzae=[ R5d(4)*R5d(2)  R5d(2)  1];
                                     R5dnen=[ 0              R5d(3)  0];
R7d=bo_d_gen(13,1,1,p7den,p7dt,39);  R7dzae=[ R7d(4)*R7d(2)  R7d(2)  1];
                                     R7dnen=[ 0              R7d(3)  0];
%controllers(delayed inputs): LTI definitions, pre-filters: LTI definitions
C3d=tf(R3dzae,R3dnen);  Fi3=tf(1,R3dzae);
C4d=tf(R4dzae,R4dnen);  Fi4=tf(1,R4dzae);
C5d=tf(R5dzae,R5dnen);  Fi5=tf(1,R5dzae);
C7d=tf(R7dzae,R7dnen);  Fi7=tf(1,R7dzae);

% pre-filter optimization
OptPreFi3_4 = bo_prefi_op(Gp3,C3u,C3d,Fi3),     % order =4 (default)
   OptPreFi3_3 = bo_prefi_op(Gp3,C3u,C3d,Fi3,3),   % order =3
   OptPreFi3_2 = bo_prefi_op(Gp3,C3u,C3d,Fi3,2),   % order =2
OptPreFi4_4 = bo_prefi_op(Gp4,C4u,C4d,Fi4),     % order =4 (default)
   OptPreFi4_3 = bo_prefi_op(Gp4,C4u,C4d,Fi4,3),   % order =3
   OptPreFi4_2 = bo_prefi_op(Gp4,C4u,C4d,Fi4,2),   % order =2
OptPreFi5_4 = bo_prefi_op(Gp5,C5u,C5d,Fi5),     % order =4 (default)
   OptPreFi5_3 = bo_prefi_op(Gp5,C5u,C5d,Fi5,3),   % order =3
   OptPreFi5_2 = bo_prefi_op(Gp5,C5u,C5d,Fi5,2),   % order =2
OptPreFi7_4 = bo_prefi_op(Gp7,C7u,C7d,Fi7),     % order =4 (default)
   OptPreFi7_3 = bo_prefi_op(Gp7,C7u,C7d,Fi7,3),   % order =3
   OptPreFi7_2 = bo_prefi_op(Gp7,C7u,C7d,Fi7,2),   % order =2

cv_PID_3_4and5_7_sim; sim('cv_PID_3_4and5_7_sim',70)
