echo off;
%DEMO_EEZ - application of eez.m [Finite Time Settling - (FTS)]
%demonstration of optionally input/output of LTI models
%                            and/or menu switch off
DEMO_EEZ_FGS(5)=0; E=tf(1,1); S=tf(1,[1 1]);
SP2=S; SP3=S; RE5=E; RE6=E; RE7=E; RE8=E; RE9=E;  RE40=E;
RE10=E; RE11=E; RE12=E; RE20=E; RE21=E; RE22=E; RE30=E; RE31=E; RE32=E;
FI10=E; FI11=E; FI12=E; FI20=E; FI21=E; FI22=E; FI30=E; FI31=E; FI32=E;
echo on; 

%plant definition in Laplace domain (two first-order time delay elements)
%using gain V=2 and time constants T1=20ms, T2=10ms):
z=2; n=[200 30 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.0181  0.0164
%nz = 1.0000  -1.7236  0.7408
%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 EEZ (ss/Ls    = standard syntax resp. LTI syntax; 
%                         Md/wM    = menu driven resp. without menu
%                         SV       = control vector without menu - cp. help
%                         varargin = 0 till 5 input arguments - cp. help):
%optimization:  undelayed inputs         or   delayed inputs and prefilter
%a-ss Md: [rz,rn]=eez(zz,nz)             or   [rz,rn,fz,fn]=eez(zz,nz)
%b-Ls Md: [RE]=eez(SZ1)                  or   [RE,FI]=eez(SZ1)
%c-ss wM: [rz,rn]=eez(zz,nz,SV,varargin) or   [rz,rn,fz,fn]=eez(zz,nz,SV,varargin)
%d-Ls wM: [RE]=eez(SZ1,SV,[],varargin)   or   [RE,FI]=eez(SZ1,SV,[],varargin)
%
% >>>text display switch off (e.g. pole display) via SV(6)=1 - cp. help<<<
pause,     %press any key please

%regarding following numerical examples please also see: Geitner, G.-H.  
%"Entwurf digitaler Regler für elektrische Antriebe" pp. 133 / 136.
%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, FTS controller (undelayed input signal, minimum form, complete compensation;
%                    step input signal resp. order=1 of integral part)
%menu input: any/2/1/any/any/any/any/0 or any/ET/ET/any/any/any/any/ET  [ET=enter]
%     resp.: SV=[0 -1 -1 0 0 0]        or [0 -1 -1 0 0 0 0 1 1 ]
%  rz = 28.9854  -49.9584   21.4729  resp. VR=28.9854; d1=-1.7236; d2=0.7408;
%  rn = 1         -0.5250   -0.4750        integral part and b1=0.4750;
pause,     %press any key please

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

%A2, FTS controller (undelayed inputs, suboptimal (A=1), complete compensation;
%                    step input signal resp. order=1 of integral part)
%menu input: e.g. any/2/1/any/any/any/any/1/any
%     resp.: e.g. SV=[0 -1 -1 1 0 0]
%rz = 10.6424  0     -23.7313 13.5888 resp. VR=10.6424; d1=0; d2=-2.2299; d3=1.2769
%rn = 1       -0.1928 -0.5066 -0.3006       integral part and b1=0.8072;  b2=0.3006
pause,     %press any key please

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

%A3, FTS controller (undelayed inputs, suboptimal (A=2), complete compensation;
%                    step input signal resp. order=1 of integral part)
%menu input: e.g. any/1/ET/any/any/any/any/2/any
%     resp.: e.g. SV=[0 -1 -1 2 0 0]
%OR e.g. in case external input of ax: RE=eez(SZ1,SV3,[],[-nz(2) -nz(3)+nz(2)^2])
%rz = 5.8516    0        0      -15.0180  9.6664  resp.  VR=5.8516; d1=0; d2=0;
%rn = 1        -0.1060  -0.2786 -0.4016  -0.2138         d3=-2.5665; d4=1.6519;
%                                                      integral part and b1=0.8940;
%                                                        b2=0.6155;  b3=0.2138;
pause,     %press any key please

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

%A4, FTS controller (undelayed inputs, suboptimal (A=3), complete compensation;
%                    step input signal resp. order=1 of integral part)
%menu input: e.g. any/ET/ET/any/any/any/any/3/any
%     resp.: e.g. SV=[0 -1 -1 3 0 0]
%rz = 3.8545  0       0       0     -10.6830  7.3285 resp. VR=3.8545;d1=0;d2=0;d3=0
%rn = 1      -0.0698 -0.1835 -0.2645 -0.3200 -0.1621       d4=-2.7716; d5=1.9013;
%                                                integral part;b1=0.9302;b2=0.7467;
%                                                              b3=0.4821;b4=0.1621;
pause,     %press any key please

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

%A: step response simulation via SIMULINK - controller output / controlled variable
echo off;
DEMO_EEZ_FGS(1)=1; DEMO_EEZs; sim('DEMO_EEZs',20); DEMO_EEZ_FGS(1)=0; echo on;
pause,     %press any key please

%B1, FTS controller (undelayed inputs, minimum form, complete compensation;
%                    ramp input signal resp. order=2 of integral part)
%menu input: e.g. any/2/2/any/any/any/any/0
%     resp.: e.g. SV=[0 -1 -1 0 0 0 0 0 1]
% rz = 71.7395 -166.4021  126.8356  -31.6730  resp.  VR=71.7395; d1=-2.3195;
% rn =  1.0000   -1.2993   -0.4013    0.7007         d2= 1.7680; d2=-0.4415;
%                                                    double integral part
%                                                                 b1=0.7007;
pause,     %press any key please

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

%B2, FTS controller (undelayed inputs, suboptimal (A=1), complete compensation;
%                    ramp input signal resp. order=2 of integral part)
%menu input: e.g. any/2/2/any/any/any/any/1/any
%     resp.: e.g. SV=[0 -1 -1 1 0 0 0 0 1]
%rz=36.9827 -36.9827 -53.4812  79.7297 -25.7485 resp. VR=36.9827; d1=-1; d2=-1.4461
%rn= 1.0000  -0.6698  -1.0907   0.1910   0.5696       d3= 2.1559; d4=-0.6962;
%                                                     double integral part
%                                                     b1= 1.3302; b2= 0.5696;
pause,     %press any key please

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

%B3, FTS controller (undelayed inputs, suboptimal (A=2), complete compensation;
%                    ramp input signal resp. order=2 of integral part)
%menu input: e.g. any/2/2/any/any/any/any/2/any
%     resp.: e.g. SV=[0 -1 -1 2 0 0 0 0 1]
%rz=26.1859 -26.1859  0.0000 -38.2203 60.5046 -21.7843 resp. VR=26.1859;d1=-1;d2=0
%rn= 1.0000  -0.4743 -0.7723  -0.5506  0.3153   0.4819       d3=-1.4596; d4=2.3106;
%                                                            d5=-0.8319;
%                                                  double integral part; b1=1.5257;
%                                                            b2=1.2791;  b3=0.4819;
pause,     %press any key please

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

%B: step response simulation via SIMULINK - controller output / controlled variable
echo off;
bdclose('DEMO_EEZs');  %not necessary using MATLAB versions R11 / 5.3
DEMO_EEZ_FGS(2)=1; DEMO_EEZs; sim('DEMO_EEZs',15); DEMO_EEZ_FGS(2)=0; echo on;
pause,     %press any key please

%C1, FTS controller (undelayed inputs, minimum form, uncomplete compensation;
%                    step input signal resp. order=1 of integral part)
%menu input: e.g. any/ET/ET/any/any/any/1/ET
%     resp.: e.g. SV=[0 1 -1 0 0 0]
% rz = 62.9168  -90.8608   30.7024   resp.   VR=62.9168; d1=-1.4441; d2=0.4880;
% rn =  1.0000   -0.3208   -0.6792           integral part;          b1=0.6792;
pause,     %press any key please

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

%C2, FTS controller (undelayed inputs, suboptimal (A=1), uncomplete compensation;
%                    step input signal resp. order=1 of integral part)
%menu input: e.g. any/ET/ET/any/any/any/1/ET
%     resp.: e.g. SV=[0 1 -1 1 0 0]
%rz = 22.7246  18.6053 -68.3467  29.7751  resp. VR=22.7246; d1=0.8187; d2=-3.0076;
%rn =  1.0000   0.4071  -0.7485  -0.6587                    d3=1.3103;
%                                            integral part; b1=1.4071; b2= 0.6587;
pause,     %press any key please

%controller calculation
SV9=[0 1 -1 1 0 0]; RE9=eez(SZ1,SV9)
pause,     %press any key please

%C: step response simulation via SIMULINK - controller output / controlled variable
%typically the controller design according C may cause noticeable an overshoot
echo off;
bdclose('DEMO_EEZs');  %not necessary using MATLAB versions R11 / 5.3
DEMO_EEZ_FGS(3)=1; DEMO_EEZs; sim('DEMO_EEZs',15); DEMO_EEZ_FGS(3)=0; echo on;
pause,     %press any key please

%Examples for delayed input signals (!separated determination regarding the
%behaviour in case of undelayed and delayed input signals possible!)
%===========================================================================
%plant definition in Laplace domain: one first-order time delay element,
%mean value measurement, gain V=0.2 and time constant T1=10ms): 
z2=0.2; n2=[10 1]; SP2=tf(z2,n2,'Variable','p');
%z-transform of the plant; sampling time T=2ms;;
T=2; [zz2,nz2]=trans(z2,n2,T,0,2)
pause,     %press any key please
    
%result: transfer function in z-domain via numerator and denominator polynomial:
%zz =      0    0.0937    0.0876
%nz = 1.0000   -0.8187    0
%same number of poles in z- and Laplace domain; dead time = sampling time
%LTI model in z-domain (advantageously by negative powers of z):  
SZ2=tf(zz2,nz2,T,'Variable','z^-1'),
pause,     %press any key please

%D1, FTS controller (delayed inputs, minimum form, complete compensation;
%                    step input signal resp. order=1 of integral part)
%D1a,   ==> prefilter: minimum form
%menu input: e.g. 1/1/1/any/any/any/0/any/any/0
%     resp.: e.g. SV=[1 -1 -1 0 0 0]
%              or SV=[1 -1 -1 0 0 2] <== text display switch off
% rz = 97.0986  -43.7105            resp.     VR=97.0986;  d1=-0.4502;
% rn =  1        -0.0645   -0.9355       integral part and b1= 0.9355;
% fz =  0.2841    0.2658                      VF= 0.2841; df1= 0.9355;
% fn =  1        -0.4502                                  bf1=-0.4502;
pause,     %press any key please

%controller calculation
SV10=[1 -1 -1 0 0 0]; [RE10,FI10]=eez(SZ2,SV10)    
pause,     %press any key please

%D1b,   ==> prefilter: suboptimal (A=1)
%menu input: e.g. 1/1/1/any/any/any/0/any/any/1/any
%     resp.: e.g. SV=[1 -1 -1 1 0 0]
% rz = 97.0986  -43.7105          resp.     VR=97.0986;  d1=-0.4502;
% rn =  1        -0.0645   -0.9355     integral part and b1= 0.9355;
% fz =  0.1562    0.2740    0.1196          VF= 0.1562; df1= 1.7543; df2=0.7659;
% fn =  1        -0.4502                                bf1=-0.4502;
pause,     %press any key please

%controller calculation
SV11=[1 -1 -1 1 0 0]; [RE11,FI11]=eez(SZ2,SV11)
pause,     %press any key please

%D1c,   ==> prefilter suboptimal (A=2)
%menu input: e.g. 1/1/1/any/any/any/0/any/any/2/any
%     resp.: e.g. SV=[1 -1 -1 2 0 0]
% rz = 97.0986 -43.7105               resp.  VR=97.0986; d1=-0.4502;
% rn =  1       -0.0645 -0.9355        integral part and b1= 0.9355;
% fz =  0.1141   0.2002  0.1639 0.0716      df1=1.7543; df2= 1.4363; df3=0.6271;
% fn =  1       -0.4502                      VF=0.1141; bf1=-0.4502;
pause,     %press any key please

%controller calculation
SV12=[1 -1 -1 2 0 0]; [RE12,FI12]=eez(SZ2,SV12)
pause,     %press any key please

%D2, FTS controller (delayed inputs: suboptimal (A=1), complete compensation;
%                    step input signal resp. order=1 of integral part)
%D2a,   ==> prefilter minimum form
%menu input: e.g. 1/1/1/any/any/any/1/any/any/any/0
%     resp.: e.g. SV=[1 -1 -1 0 1 0]
% rz = 73.0651  0      -19.6770         resp. VR=73.0651;  d1= 0;      d2=-0.2693;
% rn =  1       0.3857  -0.9645 -0.4211; integral part and b1= 1.3857; b2= 0.4211;
% fz =  0.3775  0.3532                        VF=0.3775;  df1= 0.9355;
% fn =  1       0       -0.2693               bf1=0;      bf2=-0.2693;
pause,     %press any key please

%controller calculation
SV20=[1 -1 -1 0 1 0]; [RE20,FI20]=eez(SZ2,SV20)
pause,     %press any key please

%D2b,   ==> prefilter suboptimal (A=1)
%menu input: e.g. 1/1/1/any/any/any/1/any/any/any/1/any
%     resp.: e.g. SV=[1 -1 -1 1 1 0]
% rz = 73.0651  0      -19.6770        resp.   VR=73.0651; d1=0;        d2=-0.2693;
% rn =  1       0.3857  -0.9645 -0.4211   integral part and b1=1.3857;   b2=0.4211;
% fz =  0.2076  0.3641   0.1590                VF= 0.2076; df1=1.7543; df2= 0.7659;
% fn =  1       0       -0.2693                            bf1=0;      bf2=-0.2693;
pause,     %press any key please

%controller calculation
SV21=[1 -1 -1 1 1 0]; [RE21,FI21]=eez(SZ2,SV21)
pause,     %press any key please

%D2c,   ==> prefilter suboptimal (A=2)
%menu input: e.g. 1/1/1/any/any/any/1/any/any/any/2/any
%     resp.: e.g. SV=[1 -1 -1 2 1 0]
% rz = 73.0651 0      -19.6770          resp. VR=73.0651;   d1=0;       d2=-0.2693;
% rn =  1      0.3857  -0.9645 -0.4211   integral part and  b1=1.3857;  b2= 0.4211;
% fz =  0.1517 0.2661   0.2178  0.0951         df1=1.7543; df2=1.4363; df3= 0.6271;
% fn =  1      0       -0.2693                 VF=0.1517;  bf1=0;      bf2=-0.2693;
pause,     %press any key please

%controller calculation
SV22=[1 -1 -1 2 1 0]; [RE22,FI22]=eez(SZ2,SV22)
pause,     %press any key please

%D3, FTS controller (delayed inputs: suboptimal (A=2), complete compensation;
%                    step input signal resp. order=1 of integral part)
%D3a,   ==> prefilter minimum form
%menu input: e.g. 1/1/1/any/any/any/2/any/any/any/0
%     resp.: e.g. SV=[1 -1 -1 0 2 0]
% rz = 65.1597 0       0      -11.7716       resp. d1= 0;  d2=0; d3=-0.1807;
% rn =  1      0.5338 -0.7048  -0.5770 -0.2519     VR=65.1597; integral part and 
% fz =  0.4233 0.3960                              b1=1.5338; b2=0.8290; b3=0.2519;
% fn =  1      0       0       -0.1807             VF=0.4233;   df1=0.9355;
%                                                  bf1=0; bf2=0; bf3=-0.1807;
pause,     %press any key please

%controller calculation
SV30=[1 -1 -1 0 2 0]; [RE30,FI30]=eez(SZ2,SV30)
pause,     %press any key please

%D3b,   ==> prefilter suboptimal (A=1)
%menu input: e.g. 1/1/1/any/any/any/2/any/any/any/1/any
%     resp.: e.g. SV=[1 -1 -1 1 2 0]
% rz = 65.1597 0       0      -11.7716       resp. d1= 0; d2=0; d2=-0.1807;
% rn =  1      0.5338 -0.7048  -0.5770 -0.2519     VR=65.1597; int.part; b1=1.5338;
% fz =  0.2328 0.4083  0.1783                      b2= 0.8290; b3=0.2519;
% fn =  1      0       0       -0.1807             VF= 0.2328;
%                                                  df1=1.7543;   df2= 0.7659;
%                                                  bf1=0; bf2=0; bf3=-0.1807;
pause,     %press any key please

%controller calculation
SV31=[1 -1 -1 1 2 0]; [RE31,FI31]=eez(SZ2,SV31)
pause,     %press any key please

%D3c,   ==> prefilter suboptimal (A=2)
%menu input: e.g. 1/1/1/any/any/any/2/any/any/any/2/any
%     resp.: e.g. SV=[1 -1 -1 2 2 0]
%rz = 65.1597 0       0      -11.7716          resp.  VR=65.1597;   d1 = 0;
%rn =  1      0.5338 -0.7048  -0.5770 -0.2519         d2=0;         d3 =-0.1807;
%fz =  0.1701 0.2983  0.2443   0.1067            integral part and  b1 = 1.5338;
%fn =  1      0       0       -0.1807                 b2=0.8290;    b3 = 0.2519;
%                                                     VF=0.1701;    df1= 1.7543;
%                                                     df2=1.4363;   df3= 0.6271;
%                                                     bf1=0; bf2=0; bf3=-0.1807;
pause,     %press any key please

%controller calculation
SV32=[1 -1 -1 2 2 0]; [RE32,FI32]=eez(SZ2,SV32)
pause,     %press any key please

%D: step response simulation via SIMULINK - controller output / controlled variable
%   including prefilter
%since the separated determination of undelayed and delayed input signal behaviour
%the nine combinations of controllers and prefilters result in three different step
%responses only
echo off;
bdclose('DEMO_EEZs');  %not necessary using MATLAB versions R11 / 5.3
DEMO_EEZ_FGS(4)=1; DEMO_EEZs; sim('DEMO_EEZs',25), DEMO_EEZ_FGS(4)=0; echo on;
pause,     %press any key please

%E, example according Pohl, Automatisierungstechnik(at) 8(1988)36 pp.306/10
%plant transfer function in Laplace and z-domain:
z3=1.65*conv([0.78 -0.09 1],[0.12 -0.01 1]);
n3=conv(conv([0.73 0.4 1],[0.14 0.15 1]),[0.5 1]);
SP3=tf(z3,n3,'Variable','p'), T=1; SZ3=trans(SP3,T,0,1),
pause,     %press any key please

%MATLAB calculation of the step response: continuous and discontinuous
grid on; step(SP3,SZ3),
pause,     %press any key please

%do NOT compensate the smallest (according to amount) pole [varies widely]
RE40=eez(SZ3,[0 4 0 0 0 2]),
[z40,n40]=tfdata(RE40,'v'); z40(5)=abs(z40(5)); z40(6)=-abs(z40(6)); %real values!
RE40=tf(z40,n40,T,'variable','z^-1'),
%MATLAB: calculation of the step response of the closed loop: controlled variable
RK40o=series(RE40,SZ3); RK40x=feedback(RK40o,1); figure; grid on; step(RK40x);
pause,     %press any key please

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

%SIMULINK: step responses of controlled variable and controller output
echo off;
bdclose('DEMO_EEZs');  %not necessary using MATLAB versions R11 / 5.3
DEMO_EEZ_FGS(5)=1; DEMO_EEZs; sim('DEMO_EEZs',10), DEMO_EEZ_FGS(5)=0;
clear DEMO_EEZ_FGS T z n z2 n2 z3 n3 zz nz zz2 nz2 SP1 SP2 SP3 SZ1 SZ2 SZ3
clear SV1 SV2 SV3 SV4 SV5 SV6 SV7 SV8 SV9 RE1 RE2 RE3 RE4 RE5 RE6 RE7 RE8 RE9
clear SV10 SV11 SV12 SV20 SV21 SV22 SV30 SV31 SV32 RE40 RK40o RK40x RK40y
clear RE10 RE11 RE12 RE20 RE21 RE22 RE30 RE31 RE32
clear FI10 FI11 FI12 FI20 FI21 FI22 FI30 FI31 FI32 E S tout
