%  control_mod.m
%  Kap. 5.1  
%  Modeling of linear time-invariant models
%    - continuous time models 
%       * TF Transfer Function 
%       * ZPK Zero-Pole-Gain-Function 
%       * SS State-Space-Model
%       * FRD Frequency Response Data Model
%    - discrete time models 
%       * TF Transfer Function 
%         DSP Transfer Function 
%       * ZPK Zero-Pole-Gain-Function 
%       * SS State-Space-Model
%       * FRD Frequency Response Data Model
%   - time delayed models 
%       * continuous time delays
%       * discret time delays
%       * Pade-approximation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% properties

clear all
format compact
echo off
pause off
% druck = 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% switches for separat running
if exist('alle') == 0 , alle = 1 ; end
if exist('kont') == 0 , kont = 0 ; end
if exist('disk') == 0 , disk = 0 ; end

if exist('tfon')    == 0 , tfon    = 0 + alle + kont ; end
if exist('zpkon')   == 0 , zpkon   = 0 + alle + kont ; end
if exist('sson')    == 0 , sson    = 0 + alle + kont ; end
if exist('frdon')   == 0 , frdon   = 0 + alle + kont ; end
if exist('dtfon')   == 0 , dtfon   = 0 + alle + disk ; end  
if exist('dzpkon')  == 0 , dzpkon  = 0 + alle + disk ; end
if exist('dsson')   == 0 , dsson   = 0 + alle + disk ; end
if exist('dfrdon')  == 0 , dfrdon  = 0 + alle + disk ; end
if exist('delayed') == 0 , delayed = 0 + alle        ; end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    - continuous time models 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       * TF Transfer Function 
if tfon == 1 ,
    echo on
    % SISO-System 
    h = tf([2 -3],[1 1])

    pause
    s = tf('s');
    h = (s+2) / (s^2 + 5*s + 4)

    pause
    % MIMO-System 
    % 1. Variante
    h11 = tf([2 -3],[1 1]);
    h12 = tf([1 1 -6],[1 5]);
    h21 = tf([1 2],[1 5 4]);
    h22 = tf(-1,[2 6 10]);

    H = [h11 h12 ; h21 h22]

    pause
    % 2. Variante 
    s = tf('s');
    h11 = (2*s-3) / (s+1);
    h12 = (s^2+s-6) / (s+5); 
    h21 = (s+2) / (s^2+5*s+4);
    h22 = -1 / (2*s^2+6*s+10);

    H = [h11 h12 ; h21 h22]

    pause
    % 3. Variant: cell arrays H3
    NUM = { [2 -3] [1 1 -6];[1 2] -1 };
    DEN = { [1  1] [1 5];[1 5 4] [2 6 10] };

    H = tf(NUM,DEN)

    pause
    % Verstärkungs-Matrix G
    G = tf([1 2 ; 3 0])
    echo off
end % if tfon == 1 ,


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       * ZPK Zero-Pole-Gain-Function 
if zpkon == 1 ,  
    echo on
    pause
    % SISO-System 
    h =  zpk([-6 1 1],[-5 1],3)

    pause
    s = zpk('s') 
    h = 2 * 1 / ((s-1)*(s+2))
    
    pause
    % MIMO-System 
    % 1. Variant: 
    h11 = zpk([],[-2 1],2);
    h12 = zpk([-6 1 1],[-5 1],3);
    h21 = zpk([1 2],[-4 -1],1);
    h22 = zpk(-1,[-1 -1+i -1-i],-1);
    
    H = [h11 h12 ; h21 h22]
    
    pause
    % 2. Variant: 
    s = zpk('s');
    
    h11 = 2 * 1 / ((s-1)*(s+2));
    h12 = 3 * (s^2-2*s+1)*(s+6) / (s^2+4*s-5); 
    h21 = (s^2-3*s+2) / (s^2+5*s+4);
    h22 = -1 * (s+1) / ((s^2+2*s+2)*(s+1));
    
    H = [h11 h12 ; h21 h22]
    
    pause
    % 3. Variant: cell arrays 
    Z = { [] [-6 1 1 ] ; [1 2] -1 };
    P = { [-2 1] [-5 1] ; [-4 -1] [-1 -1+i -1-i] };
    K = [2 3 ; 1 -1];
    
    H = zpk(Z,P,K)

    echo off
    % for i=1:1:2 , for j=1:1:2, 2*(i-1)+j,
    %   subplot(2,2,2*(i-1)+j); pzmap(H(i,j)); end 
    % end
end % if zpkon == 1 ,


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       * SS State-Space-Model
if sson == 1 ,
    echo on
    pause
    % SISO-System 
    sys = ss([1 2 ; 3 4],[1 ; 0],[0 1],0) 
    pause
    % MIMO-System 
    SYS = ss([1 2 ; 3 4],[1 1 0 ; 0 1 2],[0 1 ; 1 2 ; 3 1],0) 

    echo off
end % if sson == 1 ,


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       * FRD Frequency Response Data Model
if frdon == 1 ,
    echo on
    pause
    % PT1: F = 1 / (1+freq^2) * (1-j*freq)
    freq = [0.01 0.1 1 10 100 1000 10000] ;       % Frequencies
    resp  = (1-j*freq) ./(1+freq.^2) ;            % Create PT1 
    sysfrd   = frd(resp,freq,'Units','rad/s')
    
    pause
    % Create PT1 by tf
    freq = logspace(-2,4,7)                      
    resp2 = tf(1,[1 1])                          
    sysfrd2 = frd(resp2,freq,'Units','rad/s');

    pause
    % PT2
    freq = logspace(-2,4,50);
    sysPT2  = frd(tf([1],[1 .2 1]),freq);
        
    pause
    bode(sysfrd,'r-',sysfrd2,'k--',sysPT2,'b-')
    grid on; zoom on; hold on;
    echo off
end % if frdon == 1 ,

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    - discrete time models 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       * TF Transfer Function 
if dtfon == 1 ,
    echo on
    pause
    h = tf([1 -0.5],[1 1 -2],0.01)

    z = tf('z',0.01)
    h = (z-0.5)/(z^2 + z - 2)

    pause
    % DSP Transfer Function 
    h = filt([1 -0.5],[1 1 -2],0.01)
    echo off
end % if dtfon == 1 ,

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       * ZPK Zero-Pole-Gain-Function 
if dzpkon == 1 ,
    echo on
    pause
    h = zpk(0.5,[-2 1],1,0.01)

    pause
    z = zpk('z',0.01) 
    h = (z-0.5)/((z+2)*(z-1))
    echo off
end % if dzpkon == 1 ,

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       * SS State-Space-Model
if dsson == 1 ,
    echo on
    pause
    sys = ss([ 3 ],[ 1 3 ],[ 0.5 ],0,-1) 
    echo off    
end % if dsson == 1 ,

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       * FRD Frequency Response Data Model (Nicht im Buch)
if dfrdon == 1 ,
    echo on
    pause
    freq = [0.01 0.1 1 10 100 1000] ; 
    ant  = 1./(1+freq.^2) .* (1-j*freq);
    sysfrd   = frd(ant,freq,-1,'Units','rad/s')
    echo off
end % if dfrdon == 1 ,

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   - time delayed models 

if delayed == 1 ,
    echo on
    pause
    % continuous time delays
    h11 = tf([2 -3],[1 1]);
    h12 = tf([1 1 -6],[1 5]);
    h21 = tf([1 2],[1 5 4]);
    h22 = tf(-1,[2 6 10]);
    H = [h11 h12 ; h21 h22]
    
    set(H,'IODelay',[0 0.4 ; 0 0]) ;
    H(1,2)

    pause
    % discret time delays
    h = tf([1 -0.5],[1 1 -2],0.01,'InputDelay',4)
    h = delay2z(h)
    
    pause
    % Pade-approximation
    pade(H(1,2))
    echo off
end % if delayed == 1 ,
