0% found this document useful (0 votes)
9 views

Lec 1 Model Building in MATLAB

matlab course

Uploaded by

hosnyabdelghany3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lec 1 Model Building in MATLAB

matlab course

Uploaded by

hosnyabdelghany3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Chapter 1

Model Building in MATLAB

MATLAB Toolboxes are more than just collections of useful


functions; they represent the efforts of some of the world’s top researchers
in fields such as
 controls,
 signal processing,
 system identification,
 etc.
MATLAB Application Toolboxes therefore let you “stand on the
shoulders” of world class scientists. All toolboxes are built using
MATLAB.
List of Some Professional Toolboxes
 Control System Toolbox
 Neural Network Toolbox
 Fuzzy Logic Toolbox
 Optimization Toolbox
 Financial Toolbox
 Partial Differential Equation Toolbox
 Robust Control Toolbox
 Symbolic Math Toolbox
 System Identification Toolbox
 Spline Toolbox
 Image Processing Toolbox
 etc…

Prof. Dr. Taha Helmy 1


 Control System Toolbox
This is a toolbox for control system design and analysis. It supports
transfer function and state-space forms (continuous/discrete time,
frequency domain), as well as functions for step, impulse, and arbitrary
input responses.
Functions for Bode, Nyquist, Nichols plots, design with root-locus,
pole-placement, and LQR optimal control are also included.
The description of a mathematical model in MATLAB can be made
four different forms:
1. Transfer function form.
2. Zero-pole form.
3. State-space form.
4. Block diagram form.

1. Transfer function form


The transfer function of a linear system is defined as the ratio of the
Laplace transform of the output variable and to the Laplace transform of
the input variable, with all initial conditions assumed to be zero
Y ( s) output
G(s)   (1)
U ( s) input

So, a transfer function is an input-output description of behaviour of a


system.
Let the dynamic system is represented by the following differential
equation
d n y (t ) dy (t ) d m u (t ) dr (t )
an  K  a1  a 0 y (t )  bm  K  b1  b0 r (t ) (2)
dt n dt dt m dt
where y(t) is the response function,
r(t) is the input function.
If the initial conditions are all zero, then the transfer function is:

Prof. Dr. Taha Helmy 2


Y ( s ) b m s m  bm 1 s m 1  K  b1 s  b0
G ( s)   (3)
U ( s ) a n s n  a n 1 s n 1  K  a1 s  a 0

A discrete transfer function is described in the following form:


Y ( z ) bm z m  bm 1 z m 1  K  b1 z  b0
G( z)   (4)
U ( z ) a n z n  a n 1 z n 1  K  a1 z  a 0

U(S) G(S) Y(S)

In the MATLAB the transfer function of the model is represented the


numerator (num) and denominator (den) polynomials by row vectors whose
elements are coefficients of the corresponding powers of s or z in the
polynomials.
So, the two row vectors are:
num = [bm bm-1 ... b1 b0]
den = [an an-1 ... a1 a0]

Coefficient of s1 or z1
Coefficient of sn or zn

The highest order The lowest order

Example 1
The transfer function of a linear time-invariant system is:
2 s 2  4s  1
G( s) 
3s 4  8s 3  9s 2  10s

Prof. Dr. Taha Helmy 3


Enter the transfer function in MATLAB.
Solution:

>>num = [2 4 1]; G(s)


>>den = [3 8 9 10 0];
>>printsys(num,den) Print G(s) in readable
num/den = form

2 s^2 + 4 s + 1
---------------------------------
3 s^4 + 8 s^3 + 9 s^2 + 10 s
If we want to perform some operation on G(s), such as computing the
step response, the quantities num and den are used as arguments to the
command.

2. Zero-Pole-Gain form
Y (s)
A transfer function G ( s )  can be expressed in terms of gain constant,
U (s)
its zeros (the solutions of Y(s) = 0) and its poles (the roots of U(s) = 0).
To create a model in this form, we enter the gain as a scalar and zeros
and poles as column vectors.
A transfer function is expressed in zero-pole-gain form:
Z (s) ( s  Z (1))(s  Z (2)) K ( s  Z (n))
G(s)  k (5)
p( s) ( s  p(1))(s  p(2)) K ( s  p(n))

where k is the gain,


Z(i) are the zeros,
p(i) are the poles.
Discrete-time LTI systems are represented the same way as
continuous-time systems:
Z ( z) ( z  Z (1))( z  Z (2)) K ( z  Z (n))
G( z )  k (6)
p( z ) ( z  p(1))( z  p(2)) K ( z  p(n))

Prof. Dr. Taha Helmy 4


Example 2
The transfer function is:
10( s  2)(s  5)
G( s) 
( s  1)(s  3  2i )(s  3  2i )

It has a gain of 10, zeros s = –2 and s = –5, and poles s = –1, s = –3+2i
and s = –3–2i. So, we could enter the lines:
>>k = 10; Gain

>>zro = [-2; -5]; Zeros

new row sign


>>pol = [-1; -3+2*i; -3-2*i]; Poles

the i (or j) represent -1 (it is a


complex number) in MATLAB

>> sys = zpk([-2; -5], [-1; -3+2*i; -3-2*i],10)

When using any MATLAB command to operate on G(s), we must list the
arguments in the command in the following order: zeros, poles, and gain.

3. State-Space form
The state-space approach is a unified method for modeling, analyzing, and
designing a wide range of systems. For example, the state-space approach
can be used to represent time-varying systems, nonlinear systems that have
backlash, saturation, and dead zone. Many systems do not have just a
single-input and single-output (SISO). Multiple-input, multiple-output
systems (MIMO) can be compactly represented in state-space with a model
similar in form and complexity to that used for SISO systems.

Prof. Dr. Taha Helmy 5


The state of a system is described by the set of first-order differential
equations written in terms of the state variables (x1, x2, ..., xn). These
differential equations can be written in general form as
x&1  a11 x1  a12 x 2  K  a1n x n  b11 u1  K  b1m u m
x& 2  a 21 x1  a 22 x 2  K  a 2n x n  b21u1  K  b2 m u m
(7)
M
x& n  a n1 x1  a n 2 x 2  K  a nn x n  bn1u1  K  bnm u m

and can be written in matrix form as follows:


 x&1   a11 a12 K a1n  x1 
      b L b1m  u1 
 x& 2   a 21 a 22 K a 2n  x 2   11  
 M  M K M  M   M M  M  (8)
      bn1 L bnm  u m 
 x&   a a n2 K a nn  x n 

 n   n1

x is the state vector u is the input signal vector

Then the system can be represented by the compact notation of the


state differential equation as
x&  Ax  Bu (9)
In general, the output of a linear system can be related to the state
variables and the input signals by the state equation
y  Cx  Du (10)
where y is the set of output signals.

Prof. Dr. Taha Helmy 6


We enter this model form in MATLAB by defining the four matrices A,
B, C, and D. To analyze a model that exists in state-variable form within
MATLAB, we need only specify the names that have been assigned to the
four coefficient matrices for that model as argument of the appropriate
command.

Example 3
Let's consider a passive RLC circuit shown in Fig. 1.

R L

u(t) i(t) C y(t)

Fig. 1: RLC circuit.


The balance of voltages in the loop can be expressed by the following
equation:
di (t ) 1
Ri (t )  L   i (t ) dt  u (t ) (11)
dt C
The number of state variables for a RLC circuit is equal to the number
of independent energy-storage elements. Therefore, in this case we use two
state variables (x1, x2). Where
x1 (t )  i (t )
(12)
x 2 (t )   i (t )

The x1(t0) and x2(t0) represent the total initial energy of the network.
We can write two first-order differential equations in terms of the state
variables x1 and x2 as follows:

Prof. Dr. Taha Helmy 7


dx1 (t ) R 1 1
  x1 (t )  x 2 (t )  u (t )
dt L LC L
(13)
dx 2 (t )
 x1 (t )
dt
The output signal is the voltage that can be measured on the capacitor:
1 1
y (t ) 
C  i (t ) dt  x 2 (t )
C
(14)

Then we may describe the Eq. (13) and Eq. (14), then the two
matrices' equations as follows:
 dx1 (t ) 
   R 1  x (t )  1 
 
 dt     L  LC  1    L u (t )
 dx 2   1  x 2 (t )   

   0  0 (15)
 dt 
 1  x1 (t ) 
y (t )   0    0  u (t )
 C  x 2 (t ) 

Thus,
 R 1  1

A L
  , B    , C  0 1  , D  0 .
 LC  L  
 C
 1 0  0
 
For example, let R = 5, L = 1 and C = 0.2. Then the matrices are the
following:
  5  5 1
A    , B    , C  0 5 , D  0 .
 1 0   0
So, we can enter in MATLAB the following lines:

Prof. Dr. Taha Helmy 8


>>A = [-5 -5; 1 0];
>>B = [1; 0];
>>C = [0 5];
>>D = 0;

A string variable that contains the input


>>ulabel=['input-volt.']; label

>>ylabel=['output-volt.']; A string variable that contains the output


label

A string variable that contains the state


>>xlabel=['x1 x2']; labels delimited by space

>>printsys(A,B,C,D,ulabel,ylabel,xlabel) Print the state-


a= space model

x1 x2
x1 -5.00000 -5.00000
x2 1.00000 0
b=
input-volt.
x1 1.00000
x2 0
c=
x1 x2
output-volt. 0 5.00000
d=
input-volt.
output-volt. 0

Prof. Dr. Taha Helmy 9


4. Block Diagram Form
A system model can be represented by the interrelationships of the transfer
functions of subsystems. The relationships between the transfer functions
are expressed by interconnection of some graphical blocks. For example
the block diagram of a dc-servo speed control system is shown in Fig. 2.

Fig. 2: The block diagram model of a speed control system.

 Basic Diagram Parts


The transmittance relates incoming and outgoing signals and is
indicated within the block (Fig. 3).

X(s) Y(s)
G(s)

Y(s) = G(s)X(s)

Fig. 3: Transmittance block.


The summer is used to show additions and subtractions of signals (Fig.
4).

Prof. Dr. Taha Helmy 10


X1(s)
+
X2(s) Y(s)
-

+
X3(s)

Y(s) = X1(s)-X2(s)+X3(s)

Fig. 4: Summer block.


The junction indicates that the same signal is to go several places (Fig.
5).

X(s)

X(s) X(s)

X(s)

Fig. 5: Junction block.

 Block Diagram Reduction


In reducing a block diagram, it is helpful to proceed step by step,
always maintaining the same overall relationship between input and output.
Some useful simplifications are the following.
Two blocks in series with no additional connections between them,
are equivalent so far as the incoming and outgoing signals are connected to
a single "product of transmittances" block, as indicated in Fig. 6(a), and the
series MATLAB function is indicated in Fig. 6(b).

Prof. Dr. Taha Helmy 11


X(s) G1(s)X(s) G1(s)G2(s)X(s)
G1(s) G2(s) G1(s)G2(s)

(a) Block diagram


[num, den] = series(numG1,denG1,numG2,denG2)

num numG1 numG 2


G12 ( s)  G1 ( s )  G2 (s) 
den denG1 denG 2

(b) MATLAB function


Fig. 6: Serial connection.

Example 4
Enter the subsystems that have the transfer functions:
2s  2
G1 ( s ) 
s 2  4s
and
4
G2 ( s)  2
s  4 s  10
and connect them in series to form a new system that has the transfer
function:
G12 ( s )  G1 ( s )G2 ( s )

Solution:

Prof. Dr. Taha Helmy 12


>>numG1 = [2 2];
>>denG1 = [1 4 0]; G1 ( s )

>>numG2 = [4]; G2 (s)


>>denG2 = [1 4 10];
>>[num, den] = series(numG1,denG1,numG2,denG2)
>>printsys(num,den) Series
connection
num/den =
8s+8
------------------------------- G12 ( s)  G1 ( s )G 2 ( s )

s^4 + 8 s^3 + 26 s^2 + 40 s

Two blocks in parallel (Fig. 7) are equivalent to a single "sum of


transmittances" block.

G1(s)X(s)
G1(s)
+
X(s) [G1(s) + G2(s)]X(s)
G1(s) + G2(s)

+
G2(s)
G2(s)X(s)

(a) Block diagram


[num, den] = parallel(numG1,denG1,numG2,denG2)

num numG1 numG 2


G12 ( s) = G1 (s) = G2 ( s ) =
den denG1 denG2

(b) MATLAB function


Fig. 7: Parallel connection.

Prof. Dr. Taha Helmy 13


Example 5
Enter the subsystems that have the transfer functions:
2s  2
G1 ( s ) 
s 2  4s
and
4
G2 ( s) 
s 2  4 s  10
and connect them in parallel to form a new system that has the transfer
function:
G12 ( s )  G1 ( s )  G2 ( s )

Solution:
>>numG1 = [2 2];
G1 ( s )
>>denG1 = [1 4 0];
>>numG2 = [4];
G 2 ( s)
>>denG2 = [1 4 10];
>>[num, den] = parallel(numG1,denG1,numG2,denG2);
>>printsys(num,den) Parallel
connection
num/den =
2 s^3 + 14 s^2 + 44 s + 20
---------------------------------- G12 ( s)  G1 ( s )  G 2 ( s )

s^4 + 8 s^3 + 26 s^2 + 40 s

For two blocks in feedback configuration (Fig. 8) two possible


algebraic signs on the summer are considered. G(s) is termed the forward
transmittance and H(s) is the feedback transmittance in this arrangement.
Thus, the transfer function is:
Y ( s) G ( s)
T (s)   (16)
R(s) 1  G( s) H (s)

Prof. Dr. Taha Helmy 14


(a) Block diagram

[num, den] = feedback(numG,denG,numH,denH,sign)

Y ( s ) num numG numH +1 - pos. feedback


T ( s)   G (s)  H (s) 
R( s) den denG denH -1 - neg. feedback
(default)

(b) MATLAB function


Fig. 8: Feedback connect.

Example 6
Enter the following transfer functions:
2
G (s)  2
s 4
and
s 1
H ( s) 
s5
and connect them in negative feedback to form a new system that has the
transfer function:
G (s)
T (s) 
1  G (s) H (s)

Solution:

Prof. Dr. Taha Helmy 15


>>numG = [2];
G (s )
>>denG = [1 0 4];
>>numH = [1 1]; H (s )
>>denH = [1 5];
>>[num,den] = feedback(numG,denG,numH,denH,-1);
>>printsys(num,den)
num/den =
2 s + 10
G( s)
---------------------- T (s) 
1  G(s) H (s)
s^3 + 5 s^2 + 6 s + 22

The MATLAB has another possibility that you compute a closed-loop


transfer function. This is the cloop function. The cloop function calculates
the closed-loop transfer function, as shown in Fig. 9.

R(s) E(s) Y(s)


+
G(s)
-

(a) Block diagram


[num, den] = cloop(numG,denG,sign)

Y ( s) num numG +1 - pos. feedback


T (s)   G (s)  -1 - neg. feedback
R( s) den denG
(default)

(b) MATLAB function


Fig. 9: Cloop connection.

Prof. Dr. Taha Helmy 16


Example 7
Enter the process transfer function:
s
G p (s) 
s2  s  4
and the controller transfer function:
s 1
Gc ( s ) 
s5
and connect them in negative feedback with the cloop MATLAB function as
shown in Fig. 10.

R(s) E(s) U(s) Y(s)


+
Gc(s) Gp(s)
-

Fig. 10: Cloop example.


There the closed-loop transfer function is:
G c ( s )G p ( s )
T (s) 
1  G c ( s )G p ( s )

Solution:

Prof. Dr. Taha Helmy 17


>>numGp = [1 0];
G p (s )
>>denGp = [1 1 4];
>>numGc = [1 1]; Gc (s )
>>denGc = [1 5];
>>[num1,den1] = series(numGc,denGc,numGp,denGp);
>>[num,den] = cloop(num1,den1,-1);
>>printsys(num,den)
num/den =
s^2 + s
G c ( s )G p ( s )
----------------------------- T (s) 
1  G c ( s )G p ( s )
s^3 + 7 s^2 + 10 s + 20

Example 8
This is an example for the multiloop system reduction. A multiloop
feedback system is shown in Fig. 11. Our objective is to compute the
closed-loop transfer function
Y (s)
T (s) 
R( s)

Fig. 11 A multiloop feedback system


Solution:

Prof. Dr. Taha Helmy 18


We can reduce the system in four steps as shown in Fg. 12.
>>numG1 = 6;
G1 ( s )
>>denG1 = 1;
>>numG2 = [1 0];
G2 (s)
>>denG2 = [1 1 4];
>>numH1 = [1 0]; H 1 (s)
>>denH1 = [1 2];
>>numH2 = [3];
H 2 ( s)
>>denH2 = [1 3];
1
>>[num1,den1]=parallel(numG1,denG1,-1*numH1,denH1) ;

2
>>[num2,den2]=cloop(num1,den1,-1);

3
>>[num3,den3]=series(num2,den2,numG2,denG2) ;

4
>>[num4,den4]=feedback(num3,den3,numH2,denH2);
>>printsys(num4,den4)
num/den =
5 s^3 + 27 s^2 + 36 s
Y ( s)
------------------------------------------------ T (s) 
R( s )
6 s^4 + 38 s^3 + 113 s^2 + 206 s + 168

Prof. Dr. Taha Helmy 19


Step 1

Step 2

Step 3

Step 4

Prof. Dr. Taha Helmy 20


Fig. 12: Multiloop feedback system reduction.

5. Model conversion
The CONTROL SYSTEM TOOLBOX contains a set of functions that allow LTI
models to be converted between the various representations. These
functions are shown in Table 1.

Table 1: MATLAB functions for converting the model.


Name Description Syntax
ss2tf State-space to transfer function [num,den] = ss2tf(A,B,C,D,1)
conversion.
ss2zp State-space to zero-pole-gain [z,p,k] = ss2zp(A,B,C,D,1)
conversion.
tf2ss Transfer function to state-space [A,B,C,D] = tf2ss(num,den)
conversion.
tf2zp Transfer function to zero-pole- [z,p,k] = tf2zp(num,den)
gain conversion.
zp2tf Zero-pole-gain to transfer [num,den] = zp2tf(z,p,k)
function conversion.
zp2ss Zero-pole-gain to state-space [A,B,C,D] = zp2ss(z,p,k)
conversion.

• Converting From State Space To a Transfer Function

Prof. Dr. Taha Helmy 21


[num, den] = ss2tf (A, B, C, D, iu)
Where
iu corresponds to the system with more than one input. iu is either 1, 2 or 3,
where 1 implies input u1, 2 implies input u2, and 3 implies input u3.

Conversion between different models

tf2ss

Transfer State
function Space
ss2tf

ss2zp
zp2tf
zp2ss
tf2zp

Zero-pole-gain

For two functions that convert from state-space form (ss2tf and
ss2zp), it necessary to include an additional integer argument that
designates the particular input involved. For single-input single-output
systems (SISO), this integer value is always 1.
We are illustrating the use of these functions in the following
example.

Prof. Dr. Taha Helmy 22


Example 9
Enter the model of a system whose transfer function is:
10( s  1)(s  2)
G(s) 
s( s  3)( s  3  4i)( s  3  4i )

and this zero-pole form let transform to the two other forms, and finally
returning it to the original form.
Solution:

>>k = 10; Gain


>>z = [-1;-2]; Zeros
>>p = [0;-3;-3+4*i;-3-4*i]; Poles
>>[num,den] = zp2tf(z,p,k);
>>printsys(num,den) Zero-pole to transfer function
num/den =
10 s^2 + 30 s + 20
---------------------------------
s^4 + 9 s^3 + 43 s^2 + 75 s

(continued on the next page)

Prof. Dr. Taha Helmy 23


>>[A,B,C,D] = tf2ss(num,den); Transfer function to
>>printsys(A,B,C,D) state-space

a=
x1 x2 x3 x4
x1 -9.00000 -43.00000 -75.00000 0
x2 1.00000 0 0 0
x3 0 1.00000 0 0
x4 0 0 1.00000 0
b=
u1
x1 1.00000
x2 0
x3 0
x4 0
c=
x1 x2 x3 x4
y1 0 10.00000 30.00000 20.00000
d=
u1
y1 0 State-space to
zero-pole
>>[z1,p1,k1] = ss2zp(A,B,C,D,1)
z1 =
-1.0000
-2.0000
p1 =
0
-3.0000 + 4.0000i
-3.0000 - 4.0000i
-3.0000
k1 =
Prof. Dr. Taha Helmy 24
10.0000
6. Discretisation
We also can convert between continuous-time and discrete-time models in
MATLAB as shown in the Table 2.

Table 2: MATLAB functions for discreting the model.


Name Description
c2d Continuous to discrete-time conversion.
c2dt Continuous to discrete-time conversion with delay.
c2dm Continuous to discrete-time conversion with method.
d2c Discrete to continuous-time conversion.
d2cm Discrete to continuous-time conversion with method.

For example, the c2dm function has the following parameters:

[numd,dend] = c2dm(num,den,Ts,'method');

Y ( z) Y (s) Sample Conversion method.


G( z )  G(s)  time Default: 'zoh' -
U ( z) U (s)
zero-order hold

Fig. 13 The c2dm function

Example 10
Let the continuous-time transfer function of a system is:
s2 1
G(s) 
s 2  4s  4
Enter this transfer function and discretise it with the c2dm MATLAB
function, if the sample time is 0.5 sec and the conversion method is zero-
order hold (zoh).

Prof. Dr. Taha Helmy 25


Solution:
>>num = [1 0 1];
G(s)
>>den = [1 4 4];
>>[numd,dend] = c2dm(num,den,0.5,'zoh');
>>printsys(num,den)
num/den =
s^2 + 1
--------------- G(s)

s^2 + 4 s + 4
>>printsys(numd,dend,'z')
num/den =
z^2 - 1.67 z + 0.7696
---------------------------- G(z)

z^2 - 0.7358 z + 0.1353

Prof. Dr. Taha Helmy 26


Model Building in MATLAB Sheet 1

1. Write a Matlab script to compute the closed-loop transfer function for


the block diagrams shown in figure.

Controller Plant
C(s)
R(s) + 1 1
s2 s  2s  4
2
-

Prof. Dr. Taha Helmy 27


R(s) + + + C(s)
1 50
s
s2 s 1
- - -

2
2
s

Prof. Dr. Taha Helmy 28

You might also like