### 1.1 Creating tf and state space and diﬀerent Conversion of forms

#### 1.1.1 Create continuous time transfer function given the poles, zeros and gain

1.1.1.1 Mathematica
1.1.1.2 Matlab
1.1.1.3 Maple

Problem: Find the transfer function $$H(s)$$ given zeros $$s=-1,s=-2$$, poles $$s=0,s=-4,s=-6$$ and gain 5.

##### 1.1.1.1 Mathematica
 Clear["Global*"]; num = (s+1)(s+2); den = (s)(s+4)(s+6); gain = 5; sys = TransferFunctionModel[ gain (num/den),s]  Out[30]= TransferFunctionModel[{      {{5*(1 + s)*(2 + s)}},      s*(4 + s)*(6 + s)}, s]

##### 1.1.1.2 Matlab
 clear all; m_zeros = [-1 -2]; m_poles = [0 -4 -6]; gain = 5; sys = zpk(m_zeros,m_poles,gain); [num,den] = tfdata(sys,'v'); printsys(num,den,'s')  num/den = 5 s^2 + 15 s + 10 ------------------- s^3 + 10 s^2 + 24 s 

##### 1.1.1.3 Maple
restart;
alias(DS=DynamicSystems):
zeros :=[-1,-2]:
poles :=[0,-4,-6]:
gain  :=5:
sys   :=DS:-TransferFunction(zeros,poles,gain):
#print overview of the system object
DS:-PrintSystem(sys);


exports(sys);
#To see fields in the sys object, do the following
# tf, inputcount, outputcount, statecount, sampletime,
# discrete, systemname, inputvariable, outputvariable,
# statevariable, parameters, systemtype, ModulePrint
Matrix(1,1,{(1,1)=(5*s^2+15*s+10)/(s^3+10*s^2+24*s)})
numer(tf[1,1]); #get the numerator of the tf



$\text {tf} = \left [ {\begin {array}{c} {\frac {5\,{s}^{2}+15\,s+10}{{s}^{3}+10\,{s}^{2}+24\,s}}\end {array}} \right ]$

denom(tf[1,1]); #get the denominator of the tf



$s*(s^2+10*s+24)$

#### 1.1.2 Convert transfer function to state space representation

1.1.2.1 problem 1
1.1.2.2 Mathematica
1.1.2.3 Matlab
1.1.2.4 Maple
1.1.2.5 problem 2

##### 1.1.2.1 problem 1

Problem: Find the state space representation for the continuous time system deﬁned by the transfer function $H(s)=\frac {5s}{s^{2}+4s+25}$

##### 1.1.2.2 Mathematica
 Clear["Global*"]; sys = TransferFunctionModel[(5 s)/(s^2+4 s+25),s]; ss = MinimalStateSpaceModel[StateSpaceModel[sys]]  (a=Normal[ss][[1]])//MatrixForm (b=Normal[ss][[2]])//MatrixForm (c=Normal[ss][[3]])//MatrixForm (d=Normal[ss][[4]])//MatrixForm 

##### 1.1.2.3 Matlab
 clear all; s = tf('s'); sys_tf = (5*s) / (s^2 + 4*s + 25); [num,den] = tfdata(sys_tf,'v'); [A,B,C,D] = tf2ss(num,den)  A = -4 -25 1 0 B = 1 0 C = 5 0 D = 0 

##### 1.1.2.4 Maple
 restart; alias(DS=DynamicSystems): sys := DS:-TransferFunction(5*s/(s^2+4*s+25)): sys := DS:-StateSpace(sys); exports(sys); #to see what fields there are a := sys:-a;  $\left [{\begin {array}{cc} 0 & 1 \\ -25 & -4 \end {array}} \right ]$ b:=sys:-b;  $\left [ {\begin {array}{c} 0\\ 1\end {array}} \right ]$ c:=sys:-c;  $\left [ {\begin {array}{cc} 0&5\end {array}} \right ]$ d:=sys:-d;  $\left [ {\begin {array}{c} 0\end {array}} \right ]$

##### 1.1.2.5 problem 2

Problem: Given the continuous time S transfer function deﬁned by $H(s)=\frac {1+s}{s^{2}+s+1}$ convert to state space and back to transfer function.

 Mathematica Clear["Global*"]; sys = TransferFunctionModel[(s + 1)/(s^2 + 1 s + 1), s]; ss = MinimalStateSpaceModel[StateSpaceModel[sys]]  $\left ( {\begin {array}{cc|c|c} 0 & 1 & 0 \\ -1 & -1 & 1 \\ \hline 1 & 1 & 0 \\ \end {array}} \right )_{}$ TransferFunctionModel[ss]  $\frac {s+1}{s^2+s+1}$

 Matlab clear all; s = tf('s'); sys = ( s+1 )/(s^2 + s + 1); [num,den]=tfdata(sys,'v'); %convert to state space [A,B,C,D] = tf2ss(num,den)  A = -1 -1 1 0 B = 1 0 C = 1 1 D = 0  %convert from ss to tf [num,den] = ss2tf(A,B,C,D); sys=tf(num,den)  sys = s + 1 ----------- s^2 + s + 1 

#### 1.1.3 Create a state space representation from A,B,C,D and ﬁnd the step response

Problem: Find the state space representation and the step response of the continuous time system deﬁned by the Matrices A,B,C,D as shown

A =
0     1     0     0
0     0     1     0
0     0     0     1
-100   -80   -32    -8

B =
0
0
5
60

C =
1     0     0     0

D =
0


 Mathematica Clear["Global*"]; a = {{0,1,0,0}, {0,0,1,0}, {0,0,0,1}, {-100,-80,-32,-8} }; b = {{0} ,{0}, {5},{60}}; c = {{1,0,0,0}}; d = {{0}}; sys = StateSpaceModel[{a,b,c,d}]; y = OutputResponse[sys,UnitStep[t],{t,0,10}]; p = Plot[Evaluate@y, {t, 0, 10}, PlotRange -> All, GridLines -> Automatic, GridLinesStyle -> LightGray] 

 Matlab clear all; A = [0 1 0 0; 0 0 1 0; 0 0 0 1; -100 -80 -32 -8]; B = [0;0;5;60]; C = [1 0 0 0]; D = [0]; sys = ss(A,B,C,D); step(sys); grid; title('unit step response'); 

 Maple restart: alias(DS=DynamicSystems): alias(size=LinearAlgebra:-Dimensions); a:=Matrix([[0,1,0,0],[0,0,1,0], [0,0,0,1],[-100,-90,-32,-8]]): b:=Matrix([[0],[0],[5],[6]]): c:=Matrix([[1,0,0,0]]): d:=Matrix([[1]]): sys:=DS:-StateSpace(a,b,c,d); DS:-ResponsePlot(sys, DS:-Step(), duration=10,color=red,legend="step"); 

#### 1.1.4 Convert continuous time to discrete time transfer function, gain and phase margins

Problem: Compute the gain and phase margins of the open-loop discrete linear time system sampled from the continuous time S transfer function deﬁned by $H(s)=\frac {1+s}{s^{2}+s+1}$ Use sampling period of 0.1 seconds.

 Mathematica Clear["Global*"]; tf = TransferFunctionModel[(s+1)/(s^2+1 s+1),s]; 

 ds = ToDiscreteTimeModel[tf, 0.1, z, Method -> "ZeroOrderHold"] 

 {gm, pm} = GainPhaseMargins[ds];  gm Out[28]={{31.4159,19.9833}} pm Out[29]={{1.41451,1.83932},{0.,Pi}} 

 max = 100; yn = OutputResponse[ds, Table[UnitStep[n],{n, 0, max}]]; ListPlot[yn, Joined -> True, InterpolationOrder -> 0, PlotRange -> {{0, max}, {0, 1.4}}, Frame -> True, FrameLabel ->{{"y[n]", None}, {"n", "unit step response"}}, ImageSize -> 400, AspectRatio -> 1,BaseStyle -> 14] 

 Matlab clear all; close all; Ts = 0.1; %sec, sample period s = tf('s'); sys = ( s + 1 ) / ( s^2 + s + 1); %convert s to z domain sysd = c2d(sys,Ts,'zoh'); tf(sysd)   0.09984 z - 0.09033 ---------------------- z^2 - 1.895 z + 0.9048 Sample time: 0.1 seconds Discrete-time transfer function. Gm = 19.9833 Pm = 105.3851 Wcg = 31.4159 Wcp = 1.4145 

 step(sysd,10); [Gm,Pm,Wcg,Wcp] = margin(sysd) 

#### 1.1.5 Convert diﬀerential equation to transfer functions and to state space

Problem: Obtain the transfer and state space representation for the diﬀerential equation $3\frac {d^{2}y}{dt^{2}}+2\frac {dy}{dt}+y\left ( t\right ) = u(t)$

 Mathematica Clear[y, u, t]; eq = 3 y''[t] + 2 y'[t] + y[t] == u[t]; ss = StateSpaceModel[ {eq}, {{y'[t], 0}, {y[t], 0}}, {{u[t], 0}}, {y[t]}, t]; ss = MinimalStateSpaceModel[ss]  tf = Simplify[TransferFunctionModel[ss]] 

 Matlab clear all; syms y(t) Y eq = 3*diff(y,2)+2*diff(y)+y; lap = laplace(eq); lap = subs(lap,'y(0)',0); lap = subs(lap,'D(y)(0)',0); lap = subs(lap,'laplace(y(t),t,s)',Y); H = solve(lap-1,Y); pretty(H)   1 -------------- 2 3 s + 2 s + 1  [num,den] = numden(H); num = sym2poly(num); den = sym2poly(den); [A,B,C,D] = tf2ss(num,den)  A = -0.6667 -0.3333 1.0000 0 B = 1 0 C = 0 0.3333 D = 0 

 Maple restart; alias(DS=DynamicSystems): ode:=3*diff(y(t),t\$2)+2*diff(y(t),t)+y(t)= Heaviside(t): sys:=DS:-DiffEquation(ode, 'outputvariable'=[y(t)], 'inputvariable'=[Heaviside(t)]): sys:=DS:-TransferFunction(sys): sys:-tf(1,1);  $\frac {1}{3{s}^{2}+2\,s+1}$ sys:=DS:-StateSpace(sys): #show the state space matrices {sys:-a,sys:-b,sys:-c,sys:-d};  \left \{ \left [ {\begin {array}{cc} 0&1\\ \noalign {\medskip }-1/3&-2/3 \end {array}} \right ] , \left [ {\begin {array}{c} 0\\ \noalign {\medskip } 1\end {array}} \right ] , \left [ {\begin {array}{cc} 1/3&0\end {array}} \right ] , \left [ {\begin {array}{c} 0\end {array}} \right ] \right \}

#### 1.1.6 Convert from continuous transfer function to discrete time transfer function

Problem: Convert $H\left (s\right ) = \frac {1}{s^2+10 s+10}$

To $$Z$$ domain transfer function using sampling period of $$0.01$$ seconds and using the zero order hold method.

 Mathematica Clear["Global*"] sys = TransferFunctionModel[ 1/(s^2 +10 s+20),s]; sysd= ToDiscreteTimeModel[sys,0.01,z, Method->"ZeroOrderHold"] 

Matlab

 s = tf('s'); sys = 1/(s^2 + 10*s + 20); T = 0.01; %seconds sysz = c2d(sys,T,'zoh')  sysz = 4.837e-05 z + 4.678e-05 ----------------------- z^2 - 1.903 z + 0.9048 Sample time: 0.01 seconds Discrete-time transfer function. 

#### 1.1.7 Convert a Laplace transfer function to an ordinary diﬀerential equation

Problem: Give a continuous time transfer function, show how to convert it to an ordinary diﬀerential equation. This method works for non-delay systems. The transfer function must be ratio of polynomials. For additional methods see this question at stackexchange

 Mathematica tfToDiff[tf_, s_, t_, y_, u_]:= Module[{rhs,lhs,n,m}, rhs = Numerator[tf]; lhs = Denominator[tf]; rhs = rhs /. m_. s^n_. -> m D[u[t], {t, n}]; lhs = lhs /. m_. s^n_. -> m D[y[t], {t, n}]; lhs == rhs ] tf = (5s)/(s^2+4s+25); tf = C0 s/(R0 C0 s + 1); eq=tfToDiff[tf, s, t, y, u]  25 + 4 y'[t]+y''[t]==5 u'[t]