%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 1, 2, 5 and 6
%  controllers: PI,  optimized pre-filters


p1den=     [ 1  1 ];                                      p1dt=0.1;
p2den=conv([ 1  1 ]                        ,[ 0.2 1 ]);   p2dt=0;
p5den=conv([ 0.4^2     2*0.5*0.4        1 ],[ 1   1 ]);   p5dt=0;
p6den=     [ 0.7071^2  2*0.7071*0.7071  1 ];              p6dt=4;

Gp1=tf(1,p1den,'IODelay',p1dt); % plants: LTI definitions 
Gp2=tf(1,p2den);
Gp5=tf(1,p5den);
Gp6=tf(1,p6den,'IODelay',p6dt);

% undelayed inputs, controller parameter sequence: VR_TN_TR0_TV
R1u=bo_u_gen(12,1,1,p1den,p1dt,39);  R1uzae=[ R1u(2)  1 ]; 
                                     R1unen=[ R1u(3)  0 ];
R2u=bo_u_gen(12,1,1,p2den);          R2uzae=[ R2u(2)  1 ]; 
                                     R2unen=[ R2u(3)  0 ];
R5u=bo_u_gen(12,1,1,p5den);          R5uzae=[ R5u(2)  1 ]; 
                                     R5unen=[ R5u(3)  0 ];
R6u=bo_u_gen(12,1,1,p6den,p6dt,39);  R6uzae=[ R6u(2)  1 ]; 
                                     R6unen=[ R6u(3)  0 ];
C1u=tf(R1uzae,R1unen),    % controllers (undelayed inputs): LTI definitions
C2u=tf(R2uzae,R2unen),
C5u=tf(R5uzae,R5unen),
C6u=tf(R6uzae,R6unen),

% delayed inputs, controller parameter sequence: VR_TN_TR0_TV
% Rxd(4) =TV;    Rxd(2) =TN;    Rxd(3) = TR0 = integrator
R1d=bo_d_gen(12,1,1,p1den,p1dt,39);  R1dzae=[ R1d(2)  1 ]; 
                                     R1dnen=[ R1d(3)  0 ];
R2d=bo_d_gen(12,1,1,p2den);          R2dzae=[ R2d(2)  1 ];
                                     R2dnen=[ R2d(3)  0 ];
R5d=bo_d_gen(12,1,1,p5den);          R5dzae=[ R5d(2)  1 ];
                                     R5dnen=[ R5d(3)  0 ];
R6d=bo_d_gen(12,1,1,p6den,p6dt,39);  R6dzae=[ R6d(2)  1 ];
                                     R6dnen=[ R6d(3)  0 ];
% controllers (delayed inputs): LTI definitions, pre-filters: LTI definitions
C1d=tf(R1dzae,R1dnen);  Fi1=tf(1,R1dzae);
C2d=tf(R2dzae,R2dnen);  Fi2=tf(1,R2dzae);
C5d=tf(R5dzae,R5dnen);  Fi5=tf(1,R5dzae);
C6d=tf(R6dzae,R6dnen);  Fi6=tf(1,R6dzae);

% pre-filter optimization
%OptPreFi1_4 = bo_prefi_op(Gp1,C1u,C1d,Fi1),     % order =4 (default)
   OptPreFi1_3 = bo_prefi_op(Gp1,C1u,C1d,Fi1,3),   % order =3
   OptPreFi1_2 = bo_prefi_op(Gp1,C1u,C1d,Fi1,2),   % order =2
OptPreFi2_4 = bo_prefi_op(Gp2,C2u,C2d,Fi2),     % order =4 (default)
   OptPreFi2_3 = bo_prefi_op(Gp2,C2u,C2d,Fi2,3),   % order =3
   OptPreFi2_2 = bo_prefi_op(Gp2,C2u,C2d,Fi2,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
OptPreFi6_4 = bo_prefi_op(Gp6,C6u,C6d,Fi6),     % order =4 (default)
   OptPreFi6_3 = bo_prefi_op(Gp6,C6u,C6d,Fi6,3),   % order =3
   OptPreFi6_2 = bo_prefi_op(Gp6,C6u,C6d,Fi6,2),   % order =2

cv_PI_1_2and5_6_sim; sim('cv_PI_1_2and5_6_sim',70)
