% inspired by Papadopolous, page73: (3.143) and page74: (3.145)
Vs=1;
nump=1
%denp=conv(conv([25  5  1],[5  1]),[3  1]);  % ok
denp=conv([9 1],conv([8 1],conv([7 1],[6 1])));
SP3=tf(nump,denp),

Regu2=bo_u_gen(12,Vs,nump,denp), %PI
Regu3=bo_u_gen(13,Vs,nump,denp), %PID
Regu4=bo_u_gen(14,Vs,nump,denp), %PID2

Regd2=bo_d_gen(12,Vs,nump,denp), %PI
Regd3=bo_d_gen(13,Vs,nump,denp), %PID
Regd4=bo_d_gen(14,Vs,nump,denp), %PID2
       
%z-transform of the plant; sampling time T=0.1ms;
T=2;
[zz,nz]=trans(nump,denp,T,0,1);
       SZ3=tf(zz,nz,T);
SV3a=[0 0 0 0 1 2]; BODu2=bod(SZ3,SV3a), % PI undel
SV3b=[0 0 0 0 2 2]; BODu3=bod(SZ3,SV3b), % PID undel
SV3c=[0 0 0 0 3 2]; BODu4=bod(SZ3,SV3c), % PID2 undel

SV8a=[1 0 0 0 1 2]; [BODd2,FId2]=bod(SZ3,SV8a), % PI delayed
SV8b=[1 0 0 0 2 2]; [BODd3,FId3]=bod(SZ3,SV8b), % PID delayed
SV8c=[3 0 0 1 1];   [BODd4,FId4]=bod_gen(SZ3,SV8c), % PID2 delayed

Pla=SZ3;  C_ref=BODu2;,   PFi_ref=tf(1,1,T);
          C_sys=BODd2;    PFi_sys=FId2;
OPF_PI2 = bod_prefi_opt(Pla,C_ref,PFi_ref,C_sys,PFi_sys),
OPF_PI3 = bod_prefi_opt(Pla,C_ref,PFi_ref,C_sys,PFi_sys,3),
OPF_PI4 = bod_prefi_opt(Pla,C_ref,PFi_ref,C_sys,PFi_sys,4),

Pla=SZ3;  C_ref=BODu3;,   PFi_ref=tf(1,1,T);
          C_sys=BODd3;    PFi_sys=FId3;
OPF_PID2 = bod_prefi_opt(Pla,C_ref,PFi_ref,C_sys,PFi_sys),
OPF_PID3 = bod_prefi_opt(Pla,C_ref,PFi_ref,C_sys,PFi_sys,3),
OPF_PID4 = bod_prefi_opt(Pla,C_ref,PFi_ref,C_sys,PFi_sys,4),

Pla=SZ3;  C_ref=BODu4;,   PFi_ref=tf(1,1,T);
          C_sys=BODd4;    PFi_sys=FId4;
OPF_PID22 = bod_prefi_opt(Pla,C_ref,PFi_ref,C_sys,PFi_sys),
OPF_PID23 = bod_prefi_opt(Pla,C_ref,PFi_ref,C_sys,PFi_sys,3),
OPF_PID24 = bod_prefi_opt(Pla,C_ref,PFi_ref,C_sys,PFi_sys,4),

disp('"bod_prefi_opt_check03_sim.slx" and "bod_prefi_opt_check03_RA_sim.slx" will be called - compare parameters')
bod_prefi_opt_check03_sim; sim('bod_prefi_opt_check03_sim',600),
bod_prefi_opt_check03_RA_sim; sim('bod_prefi_opt_check03_RA_sim',600),
disp('Repeat bod or bo_d_gen call at MATLAB promt, if search algorithm fails')
