% u31 SS examples
% from https://ctms.engin.umich.edu/CTMS/index.php?example=AircraftPitch&section=SystemModeling

clear all; close all;

s = tf('s');
%P_pitch = (1.151*s+0.1774)/(s^3+0.739*s^2+0.921*s)
%P_pole = pole( P_pitch );
%P_zero = zero( P_pitch );
%K = 1;

% [ At, Bt, Ct, Dt ] = zp2ss( P_zero, P_pole, K )
% SSt = ss( At, Bt, Ct, Dt )


% model from Diff Eqn

Am = [-0.313 56.7 0; -0.0139 -0.426 0; 0 56.7 0];
Bm = [0.232; 0.0203; 0];
Cm = [0 0 1];
Dm = [0];
SSm = ss( Am, Bm, Cm, Dm )

% transfer function from SS model

[TFm_b, TFm_a] = ss2tf( Am, Bm, Cm, Dm )


% transfer function to new SS model: controllable canonical form

[ Ac, Bc, Cc, Dc ] = tf2ss( TFm_b, TFm_a )


% transfer function from controllable canonical form

[TFc_b, TFc_a] = ss2tf( Ac, Bc, Cc, Dc )


% to new SS model: observable canonical form

Ao = transpose(Ac);
Bo = transpose(Cc);
Co = transpose(Bc);
Do = transpose(Dc);

% transfer function from observable canonical form

[TFo_b, TFo_a] = ss2tf( Ao, Bo, Co, Do )



 s = tf('s');

 tf1 = (1.151*s+0.1774)/(s^3+0.739*s^2+0.921*s)
 tf1_pole = pole( tf1 );
 tf1_zero = zero( tf1 );
 K1 = 1;

 [ At1, Bt1, Ct1, Dt1 ] = zp2ss( tf1_zero, tf1_pole, K1 )
 SSt1 = ss( At1, Bt1, Ct1, Dt1 )
 [ tf1_ss_b, tf1_ss_a ] = ss2tf( At1, Bt1, Ct1, Dt1 );
 
 % 2nd tf
 
 tf2 = ( (1.151*s+0.1774)*(s+1) ) / ( (s^3+0.739*s^2+0.921*s)*(s+1) )
 tf2_pole = pole( tf2 );
 tf2_zero = zero( tf2 );
 K2 = 1;

 [ At2, Bt2, Ct2, Dt2 ] = zp2ss( tf2_zero, tf2_pole, K2 );
 SSt2 = ss( At2, Bt2, Ct2, Dt2 );
  [ tf2_ss_b, tf2_ss_a ] = ss2tf( At2, Bt2, Ct2, Dt2 );
 

  [ At3, Bt3, Ct3, Dt3 ] = minreal( At2, Bt2, Ct2, Dt2 );
 [ tf3_ss_b, tf3_ss_a ] = ss2tf( At3, Bt3, Ct3, Dt3 );
 
 
 
% different model from different Diff Eqn

Am2 = Am;
Am2(1,2) = Am(1,2)/2;
Am2(1,3) = Am(1,3)/2;
Am2(2,1) = Am(2,1)*2;
Am2(3,1) = Am(3,1)*2;

Bm2 = Bm;
Bm2(1,1) = Bm(1,1)/2;

Cm2 = Cm;
Cm2(1,1) = Cm(1,1)*2;

Dm2 = Dm;

SSm2 = ss( Am2, Bm2, Cm2, Dm2 )
[TFm2_b, TFm2_a] = ss2tf( Am2, Bm2, Cm2, Dm )

 