echo off;
%DEMO_BOD - application of bod.m [Discontinuous Amplitude Optimum - (BOD)]
%demonstration of optionally input/output of LTI models
%                            and/or menu switch off
DEMO_BOD_FGS(3)=0; E=tf(1,1); S=tf(1,[1 1]);
SP2=S; RE6=E; RE7=E; RE8=E; RE9=E; FI6=E; FI7=E; FI8=E;
echo on;

%plant definition in Laplace domain (two first-order time delay elements)
%using gain V=1 and time constants T1=8ms, T2=2ms):
z=1; n=[16 10 1];
%z-transform of the plant; sampling time T=2ms;
T=2; [zz,nz]=trans(z,n,T,0,1)
pause,     %press any key please

%result: transfer function in z-domain via numerator and denominator polynomial:
%zz =      0   0.0842  0.0556
%nz = 1.0000  -1.1467  0.2865
%same number of poles in z- and Laplace domain; dead time = sampling time
%LTI model in z-domain (advantageously by negative powers of z):
SZ1=tf(zz,nz,T,'Variable','z^-1'),
pause,     %press any key please

%MATLAB: step responses of the plant - continuously and discontinuously
SP1=tf(z,n,'Variable','p'); grid on; step(SP1,SZ1);
pause,     %press any key please

%calling variants of BOD (SV=control vector if menu is switched off - cp. help):
%             optimization:  undelayed inputs      or delayed inputs and prefilter
%a standard syntax menu on:  [rz,rn]=bod(zz,nz)    or [rz,rn,fz,fn]=bod(zz,nz)
%b LTI      syntax menu on:  [RE]=bod(SZ1)         or [RE,FI]=bod(SZ1)
%c standard syntax menu off: [rz,rn]=bod(zz,nz,SV) or [rz,rn,fz,fn]=bod(zz,nz,SV)
%d LTI      syntax menu off: [RE]=bod(SZ1,SV)      or [RE,FI]=bod(SZ1,SV)
%
% >>>text display switch off (e.g. pole display) via SV(6)=1 - cp. help<<<
pause,     %press any key please

%for another numerical examples please see: Geitner, G.-H.  
%"Entwurf digitaler Regler für elektrische Antriebe" pp. 94 / 107,
%publisher: VDE-Verlag GmbH Berlin und Offenbach 1996; ISBN 3-8007-1847-2
pause,     %press any key please

%A examples: optimization regarding undelayed input signals:
%==========================================================
%A1, PI-type controller (undelayed input signals) without pole compensation
%a  [rz,rn]=bod(zz,nz)    ===> using menu input: any/any/any/1
%    rz = 1.6297  -1.2748
%    rn = 1       -1
%b  [RE]=bod(SZ1)         ===> using menu input: any/any/any/1
%c  [rz,rn]=bod(zz,nz,SV) ===> using SV=[0 0 0 0 1]
%d  [RE]=bod(SZ1,SV)      ===> using SV=[0 0 0 0 1]
pause,     %press any key please

%controller calculation
SV1=[0 0 0 0 1]; RE1=bod(SZ1,SV1)
pause,     %press any key please

%A2, PI-type controller (undelayed input signals) with pole compensation
%    menu input:    any/1/1/any/any/any  or  SV=[0 1 0 1 0]
%    rz = 1.5277  -1.1898
%    rn = 1       -1
pause,     %press any key please

%controller calculation
SV2=[0 1 0 1 0]; RE2=bod(SZ1,SV2)
pause,     %press any key please

%A3, PID-type controller (undelayed input signals) without pole compensation
%    menu input:    any/any/any/2   or  SV=[0 0 0 0 2]
%    rz = 4.6218   -5.4465    1.4386
%    rn = 1         -1        0
pause,     %press any key please

%controller calculation
SV3=[0 0 0 0 2]; RE3=bod(SZ1,SV3)
pause,     %press any key please

%A4, PID-type controller (undelayed input signals) with compensation of one pole
%    menu input:   any/1/1/any/any/1   or  SV=[0 1 0 1 1]
%    rz = 4.5803   -5.3887    1.4186
%    rn = 1         -1        0
pause,     %press any key please

%controller calculation
SV4=[0 1 0 1 1]; RE4=bod(SZ1,SV4)
pause,     %press any key please

%A5, PID-type controller (undelayed input signals) with compensation of both poles
%    menu input:  any/any/any/2    or  SV=[0 1 0 2 0]
%    rz = 3.9837   -4.5680    1.1413
%    rn = 1        -1         0
pause,     %press any key please

%controller calculation
SV5=[0 1 0 2 0]; RE5=bod(SZ1,SV5)
pause,     %press any key please

%step response simulation via SIMULINK - controller output / controlled variable
echo off;
DEMO_BOD_FGS(1)=1; DEMO_BODs; sim('DEMO_BODs',30); DEMO_BOD_FGS(1)=0; echo on;
pause,     %press any key please

%B examples: optimization regarding delayed input signals:
%==========================================================
%B1, PI-type controller (delayed input signals) without pole compensation
%a  [rz,rn,fz,fn]=bod(zz,nz)    ===> using menu input: 1/any/any/1/any
%   rz = 1.8460   -1.2610
%   rn = 1        -1
%   fz = 0.3169    0
%   fn = 1.0000   -0.6831  
%b  [RE,FI]=bod(SZ1)            ===> using menu input: 1/any/any/1/any
%c  [rz,rn,fz,fn]=bod(zz,nz,SV) ===> using SV=[1 0 0 0 1 1]
%d  [RE,FI]=bod(SZ1,SV)         ===> using SV=[1 0 0 0 1 1]
pause,     %press any key please

%controller calculation
SV6=[1 0 0 0 1 1]; [RE6,FI6]=bod(SZ1,SV6)
pause,     %press any key please

%B2, PID-type controller (delayed input signals) with compensation of one pole
%    For demonstration of warnings ONLY - please pay attention to the WARNING!!!
%    menu input:  1/1/1/any/any/1/any   or  SV=[1 1 0 1 1 1]
%    rz = 5.1170  -4.9835   1.1408
%    rn = 1       -1        0
%    fz = 0.3940   0
%    fn = 1.0000  -0.606
pause,     %press any key please

%controller calculation
SV7=[1 1 0 1 1 1]; [RE7,FI7]=bod(SZ1,SV7)
pause,     %press any key please

%B3, PID-type controller (delayed input signals) without pole compensation
%    VARIANT 1 - search algorithm gets automatic initial values:
%                menu input:  1/any/any/2/any/any    
%                         or  SV=[1 0 0 0 2 1]
%    VARIANT 2 - search algorithm gets controller parameter estimates
%                menu input:  1/any/any/2/1/any/4.9/-0.85/0.5
%                         or  SV=[1 0 0 0 2 1 0 0 0 1 4.9 -0.85 0.5]
%    VARIANT 3 - search algorithm gets controller zeros estimates
%                menu input:  1/any/any/2/1/1/0.5/any
%                         or  SV=[1 0 0 0 2 1 0 0 0 2 0.5]
%    rz = 6.3309  -5.9589  1.4063
%    rn = 1       -1       0
%    fz = 0.2809   0       0
%    fn = 1.0000  -0.9413  0.2221
pause,     %press any key please

%controller calculation
SV8=[1 0 0 0 2 1]; [RE8,FI8]=bod(SZ1,SV8)
pause,     %press any key please

%simulation of setpoint and disturbance variable step response via SIMULINK
%including prefilter - controller output / controlled variable
echo off;
bdclose('DEMO_BODs');  %not necessary using MATLAB versions R11 / 5.3
DEMO_BOD_FGS(2)=1; DEMO_BODs; sim('DEMO_BODs',100), DEMO_BOD_FGS(2)=0; echo on;
pause,     %press any key please

%C example: optimization regarding undelayed inputs AND damped derivative part:
%=============================================================================
%plant definition in Laplace domain: three first-order time delay elements,
%two numerator polynomials of first order and expicite dead time Tt=4*T;
%time constant definition depends on sampling time T.
pause,     %press any key please

%numerator and denominator polynomials without dead time consideration still
z2=conv([6*T 1],[-4*T 1]);
n2=conv(conv([40*T 1],[12*T 1]),[4*T 1]);
%transfer function in Laplace domain including dead time consideration:
SP2=tf(z2,n2,'InputDelay',4*T,'Variable','p'),

%z-transform of the plant:
[zz2,nz2]=trans(z2,n2,T,4*T,1),
SZ2=tf(zz2,nz2,T,'Variable','z^-1'),
pause,     %press any key please

%calculation via MATLAB - plants step response: continuous and discontinuous
grid on; step(SP2,SZ2),
pause,     %press any key please

%   PID-type controller (undelayed input signals) with compensation of two poles
%   menu input:  any/1/1/1/any/any/any/1/1 
%            or  SV=[0 1 0 2 0 0 1 0]
%   rz = 7.3482  -13.9275    6.5938
%   rn = 1.0000   -1.7219    0.7219  = (1 - z^-1)*(1 - 0.7219z^-1)
%   cp. reference above p.99
pause,     %press any key please

%controller calculation
SV9=[0 1 0 2 0 0 1 0]; RE9=bod(SZ2,SV9)

%MATLAB: calculation of the step response of the closed loop: controlled variable
RK9o=series(RE9,SZ2); RK9x=feedback(RK9o,1); figure; grid on; step(RK9x);
pause,     %press any key please

%MATLAB: calculation of the step response of the closed loop:: controller output
RK9y=feedback(RE9,SZ2); figure; grid on; step(RK9y);
pause,     %press any key please

%SIMULINK: step responses of controlled variable and controller output
echo off;
bdclose('DEMO_BODs');  %not necessary using MATLAB versions R11 / 5.3
DEMO_BOD_FGS(3)=1; DEMO_BODs; sim('DEMO_BODs',160), DEMO_BOD_FGS(3)=0;
clear DEMO_BOD_FGS T z n z2 n2 zz nz zz2 nz2 SP1 SP2 SZ1 SZ2
clear SV1 SV2 SV3 SV4 SV5 SV6 SV7 SV8 SV9 RE1 RE2 RE3 RE4 RE5 RE6 RE7 RE8 RE9
clear RK9o RK9x RK9y FI6 FI7 FI8 E S tout
