%  control_arb.m
%  Kap. 5.2  
%  WORKING WITH LTI MODELS
%   - GET AN SET MODEL PROPERTIES 
%   - DATA RETRIEVAL 
%   - PRECEDENCE AND PROPERTY INHERITANCE
%   - ARITHMETIC OPERATIONS
%   - EXTRACTING AND MODIFYING LTI MODELS
%   - SPECIFIC MODELS
%   - CONVERSION OF CONTINUOUS-TIME AND DISCRET-TIME MODELS

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% setup

clear all
format compact
echo off
pause off

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Switches for separate perfomance
if exist('alle') == 0 , alle = 1 ; end

if exist('eigen')    == 0 , eigen    = 0 + alle ; end
if exist('daten')    == 0 , daten    = 0 + alle ; end       
if exist('rang')     == 0 , rang     = 0 + alle ; end
if exist('arithm')   == 0 , arithm   = 0 + alle ; end
if exist('exmod')    == 0 , exmod    = 0 + alle ; end
if exist('spez')     == 0 , spez     = 0 + alle ; end
if exist('kontdisk') == 0 , kontdisk = 0 + alle ; end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   - GET AN SET MODEL PROPERTIES 
if eigen == 1 ,
    echo on
    sys = tf([1 -0.5],[1 1 -2],-1,'Notes','LTI-Objekt')
    pause
    set(sys,'Ts',0.05,'InputName','Eingang')
    pause
    sys.OutputName = 'Ausgang'
    pause
    abtastzeit = sys.Ts
    pause
    notiz = get(sys,'Notes') 
    pause
    get(sys)
    echo off
end % if eigen == 1 ,

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   - DATA RETRIEVAL 
if daten == 1 ,
    echo on
    h = tf([2 -3],[1 1]) ;

    pause
    [num,den,Ts] = tfdata (h)
    num{1,1}
    
    pause
    [num,den,Ts] = tfdata (h,'v'); 
    num
    echo off
end % daten == 1 ,


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   - PRECEDENCE AND PROPERTY INHERITANCE
if rang == 1 ,
    echo on 
    systf = tf([2 -3],[1 1]);
    sysss = ss([ 1 2 ; 3 4 ],[ 1 ; 0 ],[ 0 1 ],0);
  
    pause
    sys = systf + tf(sysss)           % conversion after calculation  
    sys = tf(systf + sysss)           % calculation after conversion
    
    pause
    sysdtf = tf([2 -3],[1 1],-1,'Notes','Notiz')
    sysdss = ss([ 1 2 ; 3 4 ],[ 1 ; 0 ],[ 0 1 ],0,0.05,'Notes','Notiz')
  
    pause
    sysd = sysdtf + tf(sysdss)                                         
    sysd.Notes
    echo off 
end % if rang == 1 ,

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   - ARITHMETIC OPERATIONS
if arithm == 1 ,
    echo on
    sys1 = ss([ 1 2 ; 3 4 ],[ 1 1 ; 0 1 ],[ 3 1 ],0) ;
    sys2 = ss([ 4 3 ; 2 1 ],[ 2 3 ; 1 4 ],[ 0 1 ],0) ;
    
    pause
    % Addieren und Subtrahieren
    sys = sys1 + sys2 ;
%    sys = sys1 - sys2 ;
                           
    echo on
    sys1.a , sys2.a , sys.a  
    sys1.b , sys2.b , sys.b  
    sys1.c , sys2.c , sys.c  
    sys1.d , sys2.d , sys.d  
    echo off
    pause
end % if arithm == 1 ,

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   - EXTRACTING AND MODIFYING LTI MODELS
if exmod == 1 ,
    echo on
    h11 = tf([2 1],[1 0.1]);       % Eingang 1, Ausgang 1
    h12 = tf(1,[1 0.5]);           % Eingang 2, Ausgang 1
    h21 = tf(0,1);                 % Eingang 1, Ausgang 2
    h22 = tf(1,[1 -1]);            % Eingang 2, Ausgang 2
  
    systf = tf([h11 h12;h21 h22]) 
  
    pause
    % Extrahieren
    teilsys = systf(1,2)            % H von Eingang 2 nach Ausgang 1
  
    pause
    % Zuweisen
    teilsys = tf(1,[1 0])
    systf(1,2) = teilsys

    pause
    % Löschen von Eingang 1
    systf(:,1) = []

    pause
    % Hinzufügen neuer Eingang 
    systf(:,2) = [ tf(1,[1 -1]) ; tf(1,1) ]
    echo off
    pause

    % Rückgekoppelter Integrator
    integrator   = tf(1,[3 0])    
    proportional = tf(2,1)    
    
    feedback(integrator,proportional)
    echo off
    pause
end % if exmod == 1 ,

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   - SPECIFIC MODELS
if spez == 1 ,
    echo on
    % Zufallswertsysteme
    sysr  = rss(2,3) 
    [num,den] = drmodel(2)
    
    pause
    % System 2. Ordnung
    [num,den] = ord2(10,sqrt(2))
    step(num,den)   
    echo off
    pause
end % if spez == 1 ,

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   - CONVERSION OF CONTINUOUS-TIME AND DISCRET-TIME MODELS
if kontdisk == 1 ,
    echo on
    pause
    sysc  = tf([1],[1 .7 1])            % PT_2
    sysd  = c2d (sysc,1.5)               % Halteglied 0.~Ordnung
    sysd1 = c2d (sysc,1.5,'foh')         % Halteglied 1.~Ordnung
    sysd2 = c2d (sysc,1.5,'tustin')      % Tustin--Approximation
             
    figure
    step(sysc,'r-',sysd,'c--',sysd1,'g-.',sysd2,'k-',20)
    legend('sysc','sysd (ZOH)','sysd1 (FOH)','sysd2 (Tustin)')
%    set(get(gca,'Title'),'FontSize',14)
%    set(gcf,'PaperPosition',[1 1 20 9])
    if exist('druck') == 1 , print('-depsc','bsp_control_arb1.eps') , end
    if exist('druck') == 1 , print('-deps' ,'bsp_control_arb1_sw.eps') , end

    pause
    % Diskretisierung verzögerter Systeme
    sysc  = tf(1,[1 1])                % PT_1
    sysd  = c2d (sysc,2)               % Halteglied 0.~Ordnung
    sysdd = d2d (sysd,0.7)             % Neue Abtastzeit

    figure
    step(sysc,'r-',sysd,'c--',sysdd,'g-.',10)
    legend('sysc','sysd (ZOH)','sysdd  (ZOH resampled)')
%    set(get(gca,'Title'),'FontSize',14)
%    set(gcf,'PaperPosition',[1 1 20 9])
    if exist('druck') == 1 , print('-depsc','bsp_control_arb2.eps') , end
    if exist('druck') == 1 , print('-deps' ,'bsp_control_arb2_sw.eps') , end
    
    echo off
end % if kontdisk == 1 ,
