% easy examples - Magnitude Optimum: use of bo_check

% plant example: 3 first-order time-delay elements {10,7,3}, dead time = 5:
Vs=0.5;  T1=10;  T2=7;  T3=3;  Ttot=5;
Tt_num=[-(1/120)*Ttot^3  0.1*Ttot^2  -0.5*Ttot  1]; % approximation pade 3
Tt_den=[+(1/120)*Ttot^3  0.1*Ttot^2   0.5*Ttot  1]; % (code 23 below)
plt_num=Vs*Tt_num;
plt_den=conv(conv(conv([T1  1],[T2  1]),[T3  1]),Tt_den);
PLT=tf(plt_num,plt_den);% LTI plant complete, i.e. with gain and dead time
plt_den_opt=conv(conv([T1  1],[T2  1]),[T3  1]); % for controller design

% Controller for undelayed inputs:
Ru=bo_u_gen(13,Vs,1,plt_den_opt,Ttot,23);   % gain and dead time separately
VRu=Ru(1); TNu=Ru(2); TR0u=Ru(3);  TVu=Ru(4);
% LTI controller undelayed:
REGun=tf([ TVu*TNu   TNu   1 ],[ 0  TR0u  0 ],...
                   'Variable','p','Name','Controller for undelayed inputs')
% use of bo_check (check of basic Magnitude Optimum equations:
disp('PID undelayed inputs: first 3 basic MO equations accuracy fulfillment')
[C1u,C2u,C3u]=bo_check(3,PLT,REGun);  C1u_C2u_C3u=[C1u C2u C3u],

% Controller and pre-filter for delayed inputs:
Rd=bo_d_gen(13,Vs,1,plt_den_opt,Ttot,23);   % gain and dead time separately
VRd=Rd(1); TNd=Rd(2); TR0d=Rd(3);  TVd=Rd(4);
% LTI controller delayed
REGde=tf([ TVd*TNd   TNd   1 ],[ 0  TR0d  0 ],...
                     'Variable','p','Name','Controller for delayed inputs')
% LTI pre-filter delayed inputs
FILde=tf(1,[ TVd*TNd   TNd   1],...
                     'Variable','p','Name','Pre-filter for delayed inputs')
% use of bo_check (check of basic Magnitude Optimum equations:
disp('PID delayed inputs: first 3 basic MO equations accuracy fulfillment')
[C1d,C2d,C3d]=bo_check(4,PLT,REGde,FILde); C1d_C2d_C3d=[C1d C2d C3d],

%optimized pre-filter for delayed inputs:
FIL_OP=bo_prefi_op(PLT,REGun,REGde,FILde,2);

disp('"bo_check_01_sim.slx" will be called - compare parameters')
bo_check_01_sim; sim('bo_check_01_sim',300),
