You are on page 1of 60

Matlab Exercises

for Introductory Control Theory

Jenő Hetthéssy
Ruth Bars
András Barta

Department of Automation and Applied Informatics


Budapest University of Technology and Economics

2004
Contents
Foreword ..........................................................................................................................................3

1. Introduction to Matlab..................................................................................................................4

2. Introduction to Matlab Control System Toolbox .......................................................................10

3. The Frequency Function.............................................................................................................16

4. Elements of a Linear System......................................................................................................20

5. Feedback and Closed Loop ........................................................................................................27

6. Stability ......................................................................................................................................30

7. Series PID Compensation...........................................................................................................34

8. Series Compensation in Case of Dead Time ..............................................................................41

9. Controlling Unstable Processes..................................................................................................44

10. Discrete-time Systems..............................................................................................................46

11. Discrete PID Control................................................................................................................50

12. State Space Representation, Observability, Controllability .....................................................53

13. State Feedback Control ............................................................................................................56

2
Foreword .

Foreword
This book is intended to aid students in their study of MATLAB/SIMULINK for use in solving control problems.
Specifically, 13 labs for an introductory control course have been developed at the Department of Automation and
Applied Informatics. This book is a collection of these labs.

The importance to accompany text books by labs using CAD software was recognized decades ago at the
Department. That time a set of FORTRAN libraries supported the instructions both in control systems analysis and
design. We still believe that learning control theory is best motivated by applications and simulations rather than by
concepts only. In fact, the use of MATLAB allows a lot of theoretical concepts to be easily implemented. If students
can immediately show for themselves how certain concepts work in practice, they will go back to the theoretical
considerations with higher confidence and improved ability to move to the next field to study. Well, feedback is
around us, anyway.

The problems discussed in this book are limited to linear, time-invariant control systems. Both continuous-time and
discrete-time systems are considered with deterministic inputs.

MATLAB/SIMULINK is useful only for those students, who master the tools offered. Though the application of
MATLAB commands is simple and straightforward, a systematic introduction together with control related examples
is a must in our opinion. Time should be devoted to practice fundamental MATLAB facilities, alternative command
sequences and visualization capabilities. Two introductory labs are devoted to demonstrate the availability and power
of MATLAB in this respect.

Frequency functions and transfer functions form essential tools in classical control theory. Interestingly enough, the
frequency domain considerations gave remarkable impetus for the post-modern control era, as well. Four labs have
been devoted to discuss fundamental analysis for continuous-time systems including feedback and stability, as well.
As far as the controller synthesis is concerned, labs to treat PID compensation and series compensation for processes
with time delay have also been elaborated. The case of controlling unstable processes is involved in a separate lab.
Theoretical discussion of state-space representations is supported by two labs offering a gentle introduction to the
subject, as well as demonstrating the efficient algorithms and MATLAB commands available for state variable
feedback.

In our days controllers are implemented as digital controllers. As most of the processes to be controlled are of
continuous-time in nature, digital control needs additional tools to cover sampled-data systems. Just to support the
development of a proper view on discrete-time systems, an introductory lab has been added to this field. The wide
class of digital controllers is limited to the design of discrete-time PID controllers in this book.

Each lab is introduced by summarizing the basic concepts and definitions of the topic discussed. The MATLAB
related functions are discussed in details. Labs have been designed to be accomplished within a 2 hours period each.
Solved examples and reinforcement problems intend to help the better understanding. Examples range from simple
drills just to demonstrate MATLAB commands to more complex problems, and in most cases a short evaluation
completes the lab.

It is to be emphasized that this set of labs is not a substitute of a textbook in any respect. Excellent textbooks are
available for students with deep and comprehensive treatment of control related subjects. Labs in this book are
intended to serve as pedagogical tools offering the students a chance for active learning and experimenting. The
present set of the labs have been instructed for several semesters.

3
Introduction to Matlab .

1. Introduction to Matlab
MATLAB is an interactive environment for scientific and engineering calculations, simulations and data
visualization. Mathematics is the common language of much of science and engineering. Matrices, differential
equations, arrays of data, plots and graphs are the basic building blocks of both applied mathematics and MATLAB.
It is the underlying mathematical base that makes MATLAB accessible and powerful. To run external programs
easily a given sequence of MATLAB commands can be composed in a script file (a text file) with a .m extension.
The basic set of the MATLAB operations and functions can be extended by powerful toolboxes. To support control
courses the application of the Control system toolbox is highly recommended.
The goal of this introduction is to enable the newcomers to use MATLAB as quickly as possible. However,
for detailed descriptions the users should consult with the MATLAB manuals. In electronic form they can be found
in the ‘matlab/help’ directory. Also, an on-line help is at the MATLAB users’ disposal.
» helpdesk
The help command displays information about any command. For example:
» help sqrt

Variable names: Maximum length is 31 characters (letters, numbers and underscore). First character must be a
letter. Lower and upper cases are distinguished. The casesen command alters the case sensitivity.
» casesen
You will find that a=A . The casesen command toggles the case sensitivity status.
Every variable is treated as a matrix. A scalar variable is a 1 by 1 matrix.

Data entry: If data entry or any other statement/operation is not terminated by semicolon, the result of the
statement will always be displayed. Matlab can use several types of variables and the type declaration is automatic.
Integer:
» k=2
» J=-4
Real:
» s=3.6
» F2=-12.6e-5
Complex:
» z=3+4*i
» r=5*exp(i*pi/3)
i=sqrt(-1) is predefined, however, you may want to denote the unity imaginary vector by another variable. You are
allowed to do so, e.g. simply type
» j=sqrt(-1)
Vectors:
» x=[1, 2, 3] % row vector, elements are separated by comma or space
» q=[4; 5; 6] % column vector, elements are separated by semicolon
transpose:
» v=[4, 5, 6]’ % same as q
Remark: be careful when using the above transpose operation! For complex z values z’ results in
complex conjugate:
» i’
0 - 1.000i
Matrices:
» A=[7, 8, 9; 5, 6, 7] % A is a 2x3 matrix, » MATRIX=[row1; row2; . . . ; rowN];

Special vectors and matrices:


» u=1:3; % generates v=[1 2 3] as a row vector, » u=start:stop
» w=1:2:10; % generates w=[1 3 5 7 9], » w=start:increment:stop
» E=eye(4)
E=
1 0 0 0
0 1 0 0

4
Introduction to Matlab .

0 0 1 0
0 0 0 1
» B=eye(3,4)
B=
1 0 0 0
0 1 0 0
0 0 1 0
» C=zeros(2,4)
C=
0 0 0 0
0 0 0 0
» D=ones(3,5)
D=
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

Variable values: Typing the name of a variable displays its value:


» A
A=
7 8 9
5 6 7
» A(2, 3)
ans =
7
The first index is the row number and the second index is the column number. The answer is stored in the ans
variable.
Changing one single value in v results in the printing of the entire vector v, unless printing is not suppressed by a
semicolon:
» v(2)= -6
v=
4
-6
6
Subscripting: Colon (:) can be used to access multiple elements of a matrix. It can be used in several ways for
accessing and setting matrix elements.
Start index : end index - means a part of the matrix
: - means all the elements in a row or in a column
For vectors: v=[v(1) v(2) . . . v(N)]
For matrices: M=[M(1,1)...M(1,m); M(2,1)...M(2,m); ... ; M(n,1)...M(n,m)]
Assume B is an 8x8 matrix, then
B(1:5,3) is a column vector like [B(1,3); B(2,3); B(3,3); B(4,3); B(5,3)]
B(2:3,4:5) is a matrix like [B(2,4) B(2,5); B(3,4) B(3,5)]
B(:,3) assigns all the elements of the third column of B
B(2,:) assigns the second row of B
B(1:3,:) assigns the first three rows of B.

» A(2,1:2) % second row , first and second elements


» A(:,2) % all the elements in the second column

Workspace: The used variables are stored in a memory area called workspace. The workspace is displayed by the
following commands:
» who
» whos % displays the size of the variables

The size of the variables can be displayed by commands length and size.

5
Introduction to Matlab .

For vectors:
» lng=length(v)
lng=
3
For matrices and vectors:
» [m,n]=size(A)
m=
4 % number of rows
n=
4 % number of columns
The workspace can be saved, loaded and cleared:
» save % saves the workspace to the default matlab.mat file.
» save filename.mat % saves the workspace to filename.mat file.
» clear % clears the workspace, deletes all the variables.
» load % loads the default matlab.mat file from the workspace.
» load filename.mat % loads the filename.mat file from the workspace.

Arithmetic operations:
Addition and subtraction:
» A=[1 2; 3 4];
» B=A’;
» C=A+B;
C=
2 5
5 8
» D=A-B
D=
0 -1
1 0
» x=[-1 0 2]’;
» y=x-1 (Observe that all entries will be affected!)
y=
-2
-1
1
Multiplication:
Vector by scalar:
» 2*x
ans=
-2
0
4
Matrix by scalar:
» 3*A
ans=
3 6
9 12
Inner (scalar) product:
» s=x’*y
s=
4
» y’*x
ans=
4

Outer product:

6
Introduction to Matlab .

» M=x*y’
M=
2 1 -1
0 0 0
-4 -2 2
» y*x’
ans=
2 0 -4
1 0 -2
-1 0 2

Matrix by vector:
» b=M*x
b=
-4
0
8
Division:
» C/2
For matrices: B/A corresponds to BA-1
A\B corresponds to A-1B

Powers: A^p , where A is a square matrix and p is a real constant


e.g. inverse of A : A^(-1) , or equivalently you can use inv(A)

Manipulations on complex numbers:


» r
r=
2.5000 + 4.3301i
» real(r)
ans=
2.5000
» imag(r)
ans=
4.3301
» abs(r)
ans=
5
» angle(r)
ans=
1.0472
To get the phase in degrees type
» 180/pi*angle(r)
ans=
60.0000

Array operations in an element-by-element way:


Array operations form an extremely important class of operations and they mean arithmetic operations in an element
by element way. To indicate an array operation the operator should be proceeded by a period (.).
Example:
» a=[2 4 6]
» b=[5 3 1]
» a.*b
ans=
10 12 6

7
Introduction to Matlab .

The matrix element operator can be used for different operations, e.g. for division and power: ./, .^

Elementary math functions: (use the on-line help for details and additional items)
abs absolute value or magnitude of complex numbers
sqrt square root
real real part
imag imaginary part
conj comlplex conjugate
round round to nearest integer
fix round towards zero
floor round towards -infinity
ceil round towards +infinity
sign signum function
rem remainder
sin sine
cos cosine
tan tangent
asin arcsine
acos arccosine
atan arctangent
atan2 four quadrant arctangent
sinh hyperbolic sine
cosh hyperbolic cosine
tanh hyperbolic tangent
exp exponential base e
log natural logarithm
log10 log base 10
bessel Bessel function
rat rational approximation
expm matrix exponential
logm matrix logarithm
sqrtm matrix square root.
For example:
» g=sqrt(2)

Graphic output:
The most basic graphic command is plot.
» plot(2,3) % plots the x=2, y=3 point
Multiple points can be plotted by storing the coordinate values in vectors.
» x=[1,2,3]
» y=[0,2,1]
» plot(x,y) % notice that the points are connected with a line
» plot(x,y,’*’) % only the points are plotted
More complex curves can also be displayed by the plot command.
Example:
» t=0:0.05:4*pi;
» y=sin(t);
» plot(t,y)
» title(‘Sine function’),xlabel(‘Time’),ylabel(‘sin(t)’),grid;
where title , xlabel , ylabel and grid are optional.

Plotting multiple lines:


» y1=3*sin(2*t);
» plot(t,y,’r’,t,y1,’b’); % r-red, b-blue

8
Introduction to Matlab .

Type and colour for plotting (optional): » plot(t,y,’@#’), where ‘@’ means line type as follows:
- solid
-- dashed
: dotted
. point
+ plus
* star
o circle
x x-mark

and ‘#’ means colour as follows: r red


g green
b blue
w white
y yellow.

Polynomials: To define a polynomial, e.g. P(x)=4x4-6x3+9x2-5 simply introduce a vector containing the
coefficients of the polynomial:
» v=[4 -6 9 0 -5]
You may want to see the roots of the equation P(x)=0. MATLAB offers you the function roots to find the roots:
» p=roots(v)
p =
0.6198 + 1.4337i
0.6198 - 1.4337i
0.8577
-0.5974
In case of having the p1, p2, ... , pn roots of an equation you can easily derive the related P(x)=(x- p1)(x- p2)...(x- pn)
polynomial:
» v1=poly(p)
v1 =
1.0000 -1.5000 2.2500 0 -1.2500
The poly command results in a polynomial with a leading coefficient of 1, therefore to get the original polynomial
we have to multiply it by 4.
» v2=4*poly(p)
v2 =
4.0000 -6.0000 9.0000 0 -5.0000
Consider now the following matrix:
» M=[3 5 ; 7 -1]
The eigenvalues of M can be computed by command eig.
» e=eig(M)
e =
7.2450
-5.2450
Taking the above values as roots of a polynomial into account the related polynomial can be calculated by
» poly(e)
ans =
1 -2 -38
The above polynomial, i.e. x2-2x-38 is the so called characteristic polynomial of the M matrix and it is defined as
det(xI-M) . The characteristic polynomial can be directly calculated from the M matrix:
» poly(M)
ans =
1 -2 -38
As seen, sometimes commands can be called in different ways. MATLAB help of the particular command will
provide all the possibilities how to call it.

9
Introduction to Matlab Control System Toolbox .

2. Introduction to Matlab Control System Toolbox


Consider a single-input, single-output (SISO), continuous-time, linear, time invariant (LTI) system defined by its
transfer function:
u(t) Y (S ) num y(t)
H ( s) = =
U(s) U ( s) den Y(s)
2
Using MATLAB calculate the step response of the system if H ( s ) = . Recall that the step response is
s 2 + 2s + 4
defined as the output y(t) of the system applying unity step function input u(t)=1(t) assuming zero initial conditions.
The transfer function can be defined in MATLAB by its numerator and denominator in a polynomial form:
num = 2 , den = s 2 + 2 s + 4 . The polynomials are defined by their coefficients put in a vector by descending order
of s :
» num=2 Step response
» den=[1 2 4] 0.7
The step response of the system can be displayed directly by the 0.6
MATLAB step command:
» step(num,den); 0.5

Note that equivalently, the compact form of 0.4


» step(2,[1 2 4]);
0.3
is also applicable. Observe that the time scale is automatically selected
by MATLAB. 0.2
Expanding the above command by a left-hand side argument it is 0.1
possible to store the values of the step response function in an array:
» [y,x,t]=step(num,den) % or simpler 0
0 1 2 3 4 5 6
» y=step(num,den)
Note that no plot is generated in this case. If a semicolon (;) terminates the line the numerical values are not
displayed either. The left-hand side variables are the output variables, y is the output of the step response, t gives
the time points where it has been calculated, while x provides two so-called inner variables.
The values stored in a variable can be displayed any time by typing the name of the variable:
» y
The result is a column vector whose elements are the calculated samples of the step response function. The sampling
time applied by MATLAB can be calculated from the time interval 0 ≤ t ≤ 6 and the size of the vector:
» n=length(y)
n = 109
» T=6/n
T = 0.055
The calculated sampling time: T=6/109=0.055 sec.
The help command shows further possible forms of the step command:
» help step
It is seen then that there are other ways to use the step command. E.g. if the time interval 0 ≤ t ≤ 10 and the sampling
time T = 0.1 are explicitly selected by
»t=0:0.1:10
the following form is applicable:
» y=step(num,den,t)
The output vector can now be displayed with the plot command:
» plot(t,y);
or adding the grid option to support the easy reading of the plot
» plot(t,y),grid;
As far as the visualization is concerned, the plot command uses linear interpolation between the calculated samples.
To avoid this interpolation the
» plot(t,y,'.');
command displays only the calculated samples. The maximum of the step response (more exactly the largest
calculated sample) can be determined by

10
Introduction to Matlab Control System Toolbox .

» ym=max(y)
ym = 0.5815
The steady state value of the step response is obtained as
» ys=dcgain(num,den)
ys = 0.5
and the percentage overshoot of the output is
» yovrsht=(ym-ys)/ys*100
yovrsht = 16.2971

Inverse Laplace Transforms:


MATLAB supports a number of control-related analytical calculations. One example is to derive analytical solutions
for inverse Laplace transforms.
Calculate the inverse Laplace transform of Y(s) in analytical form:
3s 2 +13s+16
Y(s)=
(s+2)(s+3) 2
The function has to be converted to a sum of components whose inverse Laplace transform are known, e.g.
−1
k 
L
→ k 1(t )
r L −1
→ re − pt
s+ p
r L −1
→ rte − pt
(s + p) 2

This partial fraction expansion conversion can be done by the residue command. Define the function in
polynomial form:
» num=[3 13 16];
» den=poly([-3 -3 -2]);
The partial fraction expansion is obtained by
» [r,p,k]=residue(num,den)
r = 1.0000
-4.0000
2.0000
p = -3.0000
-3.0000
-2.0000
k = []

The partial fraction form in the Laplace domain


r (1) r (2) r (3) 1 4 2
Y (s) = + + +k = − +
s − p (1) ( s − p (2)) 2 s − p (3) s + 3 ( s + 3) 2 s + 2
and in time domain:
y (t ) = e −3t − 4te −3t + 2e −2t , t ≥ 0
Notice the form of the double poles.
The time function can be calculated from the analytical expression:
» t=0:0.05:6;
» y=r(1)*exp(p(1)*t)+r(2)*t.*exp(p(2)*t)+r(3)*exp(p(3)*t);
(The ’.*’ means element-by-element type multiplication). The analytical expression can be verified by numerical
simulation. Then the two curves can be plotted in the same diagram.
» yi=impulse(num,den,t);
» plot(t,[y,yi]),grid;

11
Introduction to Matlab Control System Toolbox .

LTI model structures (sys):


In order to simplify the commands the Control System Toolbox can also use data-structures. There are three basic
forms to describe linear time-invariant (LTI) systems in MATLAB:

s m + bm −1 s m −1 + .... + b2 s 2 + b1 s + b0 2
• transfer function form: H tf ( s ) = n −1
= 2
an s + an −1 s + .... + a2 s + a1 s + a0 s + 3s + 2
n 2

( s − z1 )( s − z2 )...( s − zm ) 2
• zero-pole-gain form: H zpk ( s ) = k =
( s − p1 )( s − p2 )...( s − pn ) ( s + 1)( s + 2)

x = Ax + Bu  −3 −1 1 
• state space model form: A=  , B =   , C = [ 0 1] , D = 0 . .
y = Cx + Du 2 0 0 

Using the MATLAB commands tf, zpk and ss, the transfer function H(s) can be defined as an LTI data-structure:
» num=2
» den=[1, 3, 2]
» H=tf(num,den)
Transfer function:
2
-------------
s^2 + 3 s + 2
or directly
» Htf=tf(2,[1, 3, 2])
The other models can be defined in a similar way:
» Hzpk=zpk([],[-1, -2],2)
Zero/pole/gain:
2
-----------
(s+1)(s+2)
» A=[-3, -1; 2, 0]; B=[1; 0]; C=[0, 1]; D=0;
» Hss=ss(A,B,C,D);
Conversions between a pair of the models are available as follows:
» Hzpk1=zpk(Htf)
» Hss1=ss(Htf)
» Htf1=tf(Hss)
The models contain parameters. These parameters together with the data-structure can be listed by
» get(Htf)
» get(Hzpk)
» get(Hss)
The parameters (properties) in an LTI structure can be accessed. The 'v' flag means that the result is in vector format:
» [num1,den1]=tfdata(Htf,'v')
num1 = 0 0 2
den1 = 1 3 2
» [z,p,k]=zpkdata(Hzpk,'v')
or they can be accessed directly
» num2=Htf.num{1}
num2 = 0 0 2
The term {1} has to be used since the transfer function can represent MIMO (multiple input-multiple output)
systems, in general. Also, {1} identifies cell-array type. The cell-array is a matrix with matrices of variable size. For
example
» ca={1, [1,2],[1,2,3]}
ca = [1] [1x2 double] [1x3 double]
» ca{2}
ans = 1 2

12
Introduction to Matlab Control System Toolbox .

The other models can be accessed in a similar way:


» Hzpk.k=2; Hzpk.z=[]; Hzpk.p=[-1, -2];
» Hzpk
k: 2
z: []
p: [-1 -2]
» Hss.a
ans = -3 -1
2 0 .
Symbolic data input can also be used if the s variable is defined
» s=tf('s')
» H=1/((s+1)*(s+2))
You can apply arithmetic operators to LTI models. The defined operators are: +, - , / , \ , ' , inv, ^ .
For example a resulting transfer function Hcl can be given by the following symbolic calculation:
» Hcl=H/(1+H)
There is a hierarchy of the LTI structures: tf -> zpk -> ss. If in an operation different structures are used the result is
stored in the highest hierarchy structure. For example the result of
» Htf*Hzpk
is stored in zpk form.

Time domain analysis:


The Control System Toolbox contains several commands that provide basic tools for time domain analysis.
2
Define the system H ( s ) = 2 and a time horizon for the analysis:
s + 2s + 4
» H=2/(s^2+2*s+4)
» t=0:0.1:10;
• Step response: All the previously discussed versions of the step command can be used. Additionally, we
have
» step(H);
• Impulse response: The impulse response is the output of the system in case of u(t)= δ (t ) (Dirac impulse)
input.
» impulse(H);
» yi=impulse(H,t);
» plot(t,yi)
• Nonzero initial condition: The system behavior can also be analyzed for nonzero initial conditions. Nonzero
initial conditions can only be taken into account if state space models are used. Accordingly, to apply the initial
command the system has to be transformed into state space representation.
» H=ss(H) Bode Diagrams

» x0=[1, -2] 0
From: U(1)

» [y,t,x]=initial(H,x0); -10
Phase (deg); Magnitude (dB)

» plot(t,y),grid
-20
Note that the above commands result in y as a column vector and x
-30
as a matrix having as many columns as dictated by the number of
-40
the state variables (2 in this case), and as many rows as dictated by 0
the time instants (109 in this case). Just to check: -50
» size(y)
To: Y(1)

-100
ans = 109 1
-150
» size(x)
-200
ans = 109 2 10 10 10 -1 0 1

The state trajectory can also be calculated and plotted. The first Frequency (rad/sec)
column of the x matrix contains the first state variable, while the
second state variable will show up in the second column.
» x1=x(:,1); x2=x(:,2);
The state trajectory can then be plotted by
» plot(x1,x2)

13
Introduction to Matlab Control System Toolbox .

• Output response to an arbitrary input: The output can also be calculated for any input signal. Calculate the
system response, if the input is u(t)= 2*sin(3*t).
» usin=2*sin(3*t);
» ysin=lsim(H,usin,t);
Plot both the input (red) and output (blue):
» plot(t,usin,'r',t,ysin,'b'), grid;

Frequency domain analysis:


The system behavior can also be analyzed in frequency domain.
• Bode diagram of the system can be calculated by the bode command. There are several ways to use this
command.
The gain and phase shift of the system can be calculated at a fixed frequency point. For a given system calculate the
gain and the phase shift at the frequency of w=5:
» w=5;
» [gain,phase]=bode(H,w);
The calculations can be repeated for a selected frequency range (logarithmic scale is used for better visualization). A
logarithmic frequency vector can be generated by the logspace command.
» w=logspace(-1,1,200);
This creates 200 logarithmically equidistant frequency points between 10-1=0.1 and 101=10.
» [gain,phase]=bode(H,w);
Just to plot (not to calculate and store as above) the gain and phase functions:
» bode(H,w);
The Bode diagram can also be displayed directly. In this case the MATLAB automatically calculates a frequency
vector based on the system dynamics:
» bode(H),grid;
• Nyquist diagram of the system can similarly be generated, except that the transfer function is displayed on the
complex plain: Nyquist Diagrams
From: U(1)
» nyquist(H); 0.6

• Margin command is an important tool to check the stability 0.4


margins (GainMargin, PhaseMargin) of a system.
» margin(H); 0.2
Imaginary Axis

• Zeros, poles: 0
To: Y(1)

The rootsof the transfer function are the poles of the system. This is
-0.2
how to find them:
» [num,den]=tfdata(H,'v'); -0.4

» poles=roots(den);
-0.6
The system zeros are the roots of the numerator of the transfer
function: -0.8
-1 -0.5 0 0.5
» zeros=roots(num); Real Axis
The zeros and poles can be immediately gained from the zpk model:
» [z,p,k]=zpkdata(H,'v');
The zeros and poles can also be plotted on the complex plain by the pzmap command:
» subplot(111);
» pzmap(H);
The damp command lists all the poles and (in case of complex pole-pairs) the natural frequencies and the damping
factors:
» damp(H);
The DC (zero frequency) gain of the system can also be calculated:
» K=dcgain(H);

LTI Viewer:
A linear system can be analyzed in details by the LTI Viewer. The LTI Viewer is a graphical user interface for
analyzing the system response in time and frequency domain. The systems and the curves can be manipulated from
menus or by the right mouse button:
» ltiview % or

14
Introduction to Matlab Control System Toolbox .

» ltiview('bode',H);

Simulink:
SIMULINK is a graphical software package supporting block-oriented system analysis. SIMULINK has two phases,
model definition and model analysis. First a model has to be defined than it can be analyzed by running a simulation.
SIMULINK represents dynamic systems with block diagrams. Defining a system is much like drawing a block
diagram. Instead of drawing the individual blocks, blocks are copied from libraries of blocks. The standard block
library is organized into several subsystems, grouping blocks according to their behavior. Blocks can be copied from
these or any other libraries or models into your model.

SIMULINK block library can be opened from the MATLAB command window by entering command “ simulink ”.
This command displays a new window containing icons for the subsystem blocks.
Constructing your model select New from the File menu of SIMULINK to open a new empty window in which you
can build your model. Open one or more libraries and drag some blocks into your active window.

To build your model you can drag the appropriate blocks by the left mouse button from their libraries to your file to
the required position where you release the button. To connect two blocks use the left mouse button to click on either
the output or input port of one block, drag to the other block's input or output port to draw a connecting line, and then
release the button. By clicking on the block with the right button you can duplicate it. The blocks can be increased,
decreased, rotated. Open the blocks by double clicking to change some of their internal parameters. Save the system
by selecting Save from the File menu. Run a simulation by selecting Start from the Simulation menu. Simulation
parameters can also be changed. You can monitor the behavior of your system with a Scope or you can use the To
Workspace block to send data to the MATLAB workspace and perform MATLAB functions (e.g. plot) on the
results. Parameters of the blocks can be referred also by variables defined in MATLAB.

Simulation of SIMULINK models involves the numerical integration of sets of ordinary differential equations.
SIMULINK provides a number of integration algorithms for the simulation of such equations. The appropriate
choice of method and the careful selection of simulation parameters are important considerations for obtaining
accurate results. To get yourself familiarized with the flavour of the the options offered by SIMULINK consider the
following example:
Create a new file and copy various blocks. The block parameters should then be changed to the required value.
Change the Simulation–>Parameters–>Stop time parameter to 50 from the menu. SIMULINK uses the variables
defined in the MATLAB workspace.
H(s): Control System Toolbox –>LTI system : H
Difference: Simulink–>Math–>Sum: +-
Dead time, delay: Simulink–>Continuous–>Transport Delay: 1
Gain: Simulink–>Math–>Gain: 1
Step input: Simulink–>Sources–>Step
Scope: Simulink–>Sinks–>Scope
Clock: Simulink–>Sources–>ClockOutput, time: Simulink–>Sinks–>To Workspace: y,t

t
y
Clock time
output

1.5 H
Step
Gain LTI SystemPs Transport Scope1
Input
Delay

The result can be analyzed directly by the Scope block or it can be send back to the MATLAB workspace by the
To Workspace output block . The results there can be further processed and displayed graphically. Change the Gain
parameter between 0.5 and 2. Determine the parameter value for which the system produces constant oscillation.

15
The Frequency Function .

3. The Frequency Function


A basic property of a stable linear system is that for sinusoidal input it responds with a sinusoidal signal of the same
frequency in steady (quasi-stationary) state. Applying the input signal
u( t ) = Au sin(ωt + ϕ u ) , t ≥ 0
The output signal is
y (t ) = ysteady − state (t ) + ytransient (t )
In steady state:
ysteady − state (t ) = Ay sin(ωt + ϕ y )

u (t ) = Au sin(ωt + ϕu ) y (t ) = Ay sin(ωt + ϕ y ) + ytransient


H (s)

The frequency function defines the amplitude ratio Ay/Au and the phase shift (ϕy-ϕu) versus the frequency. Utilising
the amplitude ratio and the phase shift within one single function the frequency function has been derived as a
complex function. It can be proven that formally the frequency function can be obtained from the transfer function
by substituting s=jω . So the frequency function is
H ( jω ) = H ( s) s = jω = M (ω )e jϕ (ω )
where M(ω) is the amplitude-frequency function or magnitude function, and ϕ(ω) is the phase-frequency or phase
function:
Ay (ω )
M (ω ) = H ( jω ) = ϕ (ω ) = arg{H ( jω )} = ϕ y (ω ) − ϕ u (ω )
Au (ω )
The frequency function can be depicted in a given frequency range by plotting M(ω) and ϕ(ω) functions versus the
frequency. The frequency scale is logarithmic. This technique exhibits the Bode diagram. A second possibility is to
plot the points of the frequency function calculated for various ω values on the complex plain, while ω changes from
zero to infinity. Connecting these points results the contour of the so-called Nyquist diagram.

Example: The transfer function of a linear system is


10
H ( s) =
s + 2 s + 10
2

Lets calculate the output of the system, if the input is u(t)= 2*sin(3*t).
» num=10;
» den=[1, 2, 10];
» H=tf(num,den);
» t=0:0.05:10;
» u=2*sin(3*t);
» yl=lsim(H,u,t);
Plot both the input (red) and output (blue) in the same diagram:
» plot(t,u,'r',t,yl,'b'), grid;
In steady state the amplitude and the phase of the output signal is changing with the frequency applied. This change
can be calculated from the frequency function H ( s = jω ) . In MATLAB the bode command calculates these
variables for a range of frequencies or just at a given frequency, say ω = 3 :
» [gain,phase]=bode(H,3);
Investigate the system behavior for different frequencies: u(t)= sin( ω t), ω1 = 1, ω2 = 2, ω3 = 5, ω4 = 10 .
» w1=1; w2=2; w3=5; w4=10;
» u1=sin(w1*t); u2=sin(w2*t); u3=sin(w3*t); u4=sin(w4*t);
» y1=lsim(H,u1,t); y2=lsim(H,u2,t); y3=lsim(H,u3,t); y4=lsim(H,u4,t);
» plot(t,y1,'r',t,y2,'g',t,y3,'b',t,y4,'m'), grid;
Calculate the gain and phase values for the different frequencies.

16
The Frequency Function .

» [gain1,phase1]=bode(H,w1);
» [gain2,phase2]=bode(H,w2);
» [gain3,phase3]=bode(H,w3);
» [gain4,phase4]=bode(H,w4);
For comparison, create a table with the above gain and phase values.
The gain and phase values can be displayed simpler if vectors are used. Create a linear frequency vector.
» w=1:0.1:10;
» [gain,phase]=bode(num,den,w);
If both the amplitude and the phase diagrams are needed, the subplot command sets up the figure window for several
simultaneous graphs. (2 x 1 graph and first window is selected by 211)
» subplot(211), plot(w,gain)
» subplot(212), plot(w,phase)
This is the Bode diagram of the system.
» subplot(111) % setting again one figure window

The same data can be also displayed on the complex plain. That is the Nyquist diagram.
» clf % clears the figure window
» re=real(gain.*exp(j*phase*pi/180));
» im=imag(gain.*exp(j*phase*pi/180));
» plot(re,im)
This can be calculated directly by the nyquist command.
» [re,im]=nyquist(num,den,w)
» plot(re,im)

In practice, you may want to visualize or to calculate the frequency function.


Visualization: The bode command without output argument displays the curve immediately.
» clf % clears the figure window
» bode(H),grid
Observe that logarithmic scaling is used for both the frequency and the magnitude function. Similarly, the Nyquist
curve can be displayed:
» nyquist(H)

Calculation: The magnitude and the phase values can be calculated directly. For direct calculation the transfer
function (num,den) format should be used, since the LTI structure command produces a 3-dimensional array.
» [gain,phase,w]=bode(num,den)
Display the result in a log-log diagram. The MATLAB uses the loglog command to do this. This command is the
same as plot except it uses logarithmic scale for both the x and y axis.
» loglog(w,gain),grid
If both the amplitude and the phase diagrams are needed, use again subplot commands.
» subplot(211)
» loglog(w,gain)
» subplot(212)
» semilogx(w,phase)
The logarithmic frequency vector can be generated directly by the logspace command.
» w=logspace(-2,2,100)
This creates 100 logarithmically equidistant frequency points between 10-2=0.01 and 102=100. With this the Bode
diagram can be calculated again.
» [gain,phase]=bode(num,den,w)
» subplot(111)
» loglog(w,gain)
Notice that this time the frequency range has been changed.

Exercise 1:
The system transfer function is: H(s)=10/(s^2+2s+5)
The input is: u(t)=A*sin(2t), t ≥ 0
The output in steady-state is: y(t)=5*sin(2t+phase)

17
The Frequency Function .

Calculate the A and phase parameters (use the MATLAB bode command)

The shape of the Nyquist diagram is characteristic of the system. Analysing it one can characterize important
properties of the system.

The Bode diagram is advantageous when multiplying two transfer functions: because of the logarithmic scale the
Bode diagrams are just added. In most cases approximate Bode diagrams, given by asymptotes of the magnitude
curve provide a good approximation of the frequency characteristics. By sketching these approximate curves a quick
evaluation of the system behaviour can be given.

The next example illustrates that in a wide frequency range the approximate Bode amplitude diagram is really close
to the accurate one.

Bode and Approximated Bode Plots:


Compute the approximated and exact Bode plots for
10
H ( s) =
s (1 + s )(1 + 10s ) 2
Observe that H(s) is given in time-constant form. The system in zero-pole-gain form is
1 1 1
H ( s ) = 0.1
s (0.1 + s ) 1 + s
2

» s=zpk(‘s’)
» H=10/(s*(1+s)*(1+10*s)*(1+10*s))
» [num,den]=tfdata(H,’v’)
» p=roots(den)
The poles of the system are 0, - 0.1, - 0.1, -1. Also, these are the break frequencies of the Bode plot.
Introduce appropriate scaling for the following frequency ranges: w1:0.02-0.1; w2:0.1-1; w3:1-4
» w1= logspace (log10(0.02),-1,10);
» w2=logspace(-1,0,10);
» w3=logspace(0,log10(4),10);
The full frequency range:
» w= [w1 w2 w3];
1
1. Calculate the approximated amplitude values for
s
» gain1=1./w;
1
2. Calculate the approximated amplitude values for . Below the break frequency (low frequency
(0.1 + s ) 2
1
approximation) the term is approximated by constant value . Above the break frequency (high frequency
0.12
1
approximation) it is approximated by 2
s
» w2L=w1; w2H=[w2 w3];
» gain2L=1./((0.1+0*w2L).^2); gain2H=1./(w2H.^2); gain2=[gain2L gain2H];
1
3. Calculate the approximated amplitude values for similarly.
1+ s
» w3L=[w1 w2]; w3H=w3;
» gain3L=0*w3L+1; gain3H=1./w3H;
» gain3=[gain3L gain3H];
Calculate the gain value of the full system.
» gainappr=0.1*gain1.*gain2.*gain3;
Calculate the exact bode amplitude values.
» gainexact=bode(num,den,w);

18
The Frequency Function .

Plot the two curves to check the approximation:


» loglog(w, gainexact,’b’,w,gainappr,’r’),grid
It is seen that the approximation is acceptable, the biggest deviation is obtained at frequency ω = 0.1 , which is the
breakpoint of the double pole. Note that the deviation can be much larger in case of lightly damped poles ( ζ < 0.3 ).

4
10

2
10

0
10

-2
10

-4
10
-2 -1 0 1
10 10 10 10

19
Elements of a Linear System .

4. Elements of a Linear System


In general, a linear system can be written in the
c d

k ∏ (1 + sτ j )∏ (1 + 2ζ jτ 0 j s + s 2τ 02 j )
H (s) = 1 1
f
e − sTD
si e

∏ (1 + sT )∏ (1 + 2ζ T
1
j
1
j 0j s + s 2T02j )

form, where k is the gain, i is the number of integrators, TD is the dead time (time-delay). Note that the above form
is called as time-constant form, contrary to the zero-pole form.

In the sequel a set of fundamental system elements will be derived applying particular parameterization of the time-
constant form. Specifically, the following elements will be discussed: proportional, integrating, derivative, first order
lag, second order lag (or second order oscillating element), dead time (or time-delay).

Step responses and frequency responses will be analyzed.

1. Gain: H ( s ) = k
In terms of the frequency function the gain of the system is k and the phase is zero for all frequencies.

k
2. Integrator: H ( s ) =
s
k
Investigate the behavior of a simple integrator for k = 1 and k = 5 , respectively.
s
1 5
H1 ( s ) = , H 2 (s) =
s s
» clear % just to clear workspace (deletes all the previously defined variables)
» s=zpk(‘s’) % s is defined as a symbolic variable in zpk form
» H1=1/s
» H2=5/s
Step response:
» figure(1), step(H1,’r’,H2,’g’), grid
Bode diagram:
» figure(2), bode(H1,’r’,H2,’g’), grid
Nyquist diagram:
» figure(3), nyquist (H1,’r’,H2,’g’),grid
The steady state values are:
» y1inf=dcgain(H1)
» y2inf=dcgain(H2)

It is seen that the DC (zero frequency) gain of an integrator is infinitely large (this is why it is typically used in
closed-loop control systems), while its phase is -90º for all frequencies. As far as the time-domain behaviour of an
integrator is concerned, note that the output of an integrator remains constant if its input is zero (zero detection
property).

20
Elements of a Linear System .

k
3. Proportional element and lag: H ( s ) =
1 + Ts
Examine the Bode and Nyquist diagrams and the step response of the following first order lag term:
k 2
H1 (s) = =
1 + Ts 1 + 10 s
Define the system then display its step response.
Using the symbolic variable s as introduced earlier the transfer function gets created in zpk form, too:
» H1=2/(1+10*s)
or equivalently, a transfer function form can be obtained by
» H1=tf(2,[10, 1])
The step response
» t=0:0.1:50;
» y1=step(H1,t);
» plot(t,y1),grid
or simply
» step(H1)

Now discuss the meaning of k and T.


The steady-state value, y (t → ∞) of the system can be calculated by the final value theorem
y (t → ∞) = lim sY ( s ) = lim sH ( s )U ( s )
s →0 s →0

where U ( s ) is the Laplace transform of the input applied. Assuming a unit step input the above relation simplifies
to
1
y (t → ∞) = lim sH ( s )U ( s ) = lim sH ( s ) = lim H ( s )
s →0 s →0 s s →0
Specifically, for H ( s ) = H1 ( s )
2
y (t → ∞) = =2
1 + 10 s s =0
By MATLAB:
» y1inf=dcgain(H1)
To see the impact of the time constant T on the transient behaviour repeat the
» y1=step(2,[T 1],t);
MATLAB command for various T values.

Systems also can be described by the zero-pole form:


kp 2 0.2
H1 (s)= = = .
s− p 10(0.1 + s ) s + 0.1
where
1 k
p=− , kp =
T T
This form is more descriptive in the frequency domain. The absolute value of the pole can be interpreted as the
corner frequency of the element. Also, according to
y (t → ∞) = lim H ( s )
s →0
derived earlier for the step response you may conclude that the magnitude function value at low frequency is
identical to the value of the steady-state step response.
The transfer function H ( s ) , as well as the and the frequency function H ( jω ) are both complex functions. The
Bode diagram exhibits this complex function by the absolute value (magnitude function) and phase angle (phase
function). The Nyquist diagram is an alternative technique to visualize the complex frequency function on the
complex plane.

21
Elements of a Linear System .

» bode(H1);
» nyquist(H1);
Similarly, investigate the following systems:
2 2 2
H1 = ; H2 = ; H3 =
1 + 10 s (1 + 10 s )(1 + 2 s ) (1 + 10 s )(1 + 2 s )(1 + 0.1s )
Display the step response, Bode diagram and Nyquist diagrams of the three systems.
(1-red, 2-green, 3- blue)
» H2=2/((1+10*s)*(1+2*s))
» H3=2/((1+10*s)*(1+2*s)*(1+0.1*s))
Step responses:
» figure(1), step(H1,’r’,H2,’g’,H3,’b’),grid
Bode diagrams:
» figure(2), bode(H1,’r’,H2,’g’,H3,’b’), grid
Nyquist diagrams:
» figure(3), nyquist(H1,’r’,H2,’g’,H3,’b’),grid

Note that it is possible to magnify a portion of the curves by the zoom menu command of the figure window.

k
4. Integrator and lag: H ( s ) =
s (1 + Ts )
2 2 2
H1 ( s ) = , H 2 ( s ) = , H3 =
s s(1 + 10s ) s (1 + 10 s)(1 + 2 s)
» H1=2/s
» H2=2/(s*(1+10*s))
» H3=2/(s*(1+10*s)*(1+2*s))
Step responses:
» figure(1), step(H1,’r’,H2,’g’,H3,’b’),grid
Bode diagrams:
» figure(2), bode(H1,’r’,H2,’g’,H3,’b’),grid,
Nyquist diagrams:
» figure(3), nyquist (H1,’r’,H2,’g’,H3,’b’),grid

1
5. Second order element: H ( s ) =
s T + 2ζ T0 s + 1
2
0
2

Investigate the following system:


1 1
H ( s) = = 2 2
9s + 2 s + 1 s T0 + 2ζ T0 s + 1
2

1 1
where ω0 = is the natural frequency and ζ is the damping factor of the system ( T0 = = 3 , ζ =1/3).
T0 ω0
» num=1;
» den=[9, 2, 1]
» H=tf(num,den)
Calculate the poles of the system.
» roots(den)
or by
» damp(H)
The two poles are complex conjugates and can be given as p1 = a + jb, p2 = a − jb
The vt overshoot of the system is calculated by

22
Elements of a Linear System .

a
ω 02 = a 2 + b 2 , ζ = − ,
b
−ζπ
πa
1−ζ 2 −
vt = e =e b

The oscillation frequency is


ω p = b = ω0 1 − ς 2
and the time of the first maximum (peak time) is
π
Tp = .
ωp

» zeta=1/3
» vt=exp(-zeta *pi/sqrt(1- zeta * zeta))
The step response:
» [y,t]=step(H);
» plot(t,y), grid
Calculate the maximum value:
» ym=max(y)
The steady state value
» ys=dcgain(H)
The overshoot again
» yo=(ym-ys)/ys

Examine the Bode and Nyquist diagrams and the step response of the system for various damping factors
ζ =0.3, 0.7, 1 .
» zeta1=0.3, zeta2=0.7, zeta3=1
» T0=3
» H1=1/(s*s*T0*T0+2*zeta1*T0*s+1)
» H2=1/(s*s*T0*T0+2*zeta2*T0*s+1)
» H3=1/(s*s*T0*T0+2*zeta3*T0*s+1)
Step response:
» figure(1), step(H1,’r’,H2,’g’,H3,’b’),grid
Bode diagram:
» figure(2), bode(H1,’r’,H2,’g’,H3,’b’),grid
Nyquist diagram:
» figure(3), nyquist(H1,’r’,H2,’g’,H3,’b’),grid
The zero-pole map
» pzmap(H1)
» pzmap(H2)
» pzmap(H3)
It is seen that for damping factor 0.3 the step response is the most oscillating, the maximum amplification in the
Bode amplitude diagram is the highest, and the Nyquist diagram crossing the imaginary axis gives the biggest
magnitude for this case. The imaginary value of the complex conjugate poles providing the frequency of oscillation
in the time response is also the highest. High amplification in the Bode amplitude diagram indicates high overshoot
in the step response. Damping factor 0.7 provides a slight overshoot. Control systems can be designed for similar
behaviour.

6. Derivative element: H ( s ) = sTd


2s 2s 2s
H1 ( s ) = sTd = 2s, H 2 ( s ) = , H3 = , H4 =
1 + 10s (1 + 10 s )(1 + 2 s ) (1 + 10 s )(1 + 2s )(1 + 0.1s )
Investigate again the step response, the Bode and the Nyquist diagram of the system.

23
Elements of a Linear System .

» H1=2*s
» H2=(2*s)/(1+10*s)
» H3=(2*s )/((1+10*s)*(1+2*s))
» H4=(2*s )/((1+10*s)*(1+2*s)*(1+2*s))
Step response of H1 ( s ) :
» figure(1), step(H1)
The MATLAB can not evaluate this system, since the transfer function is not proper, the degree of the numerator is
higher than the degree of the denominator. (The answer should be a Dirac function).
Step responses:
» figure(1), step(H2,’r’,H3,’g’,H4,’b’),grid
Bode diagrams:
» figure(2), bode(H2,’r’,H3,’g’,H4,’b’),grid,
Nyquist diagrams:
» figure(3), nyquist(H2,’r’,H3,’g’,H4,’b’),grid

k ( s − z1 )( s − z2 )...( s − zM )
7. Zeros: H ( s ) =
D( s)
Analyse the effect of zeros to the step and frequency responses in case of the following transfer function:
1+τ s
H (s) =
(1 + s )(1 + 10 s )
τ , the time constant in the numerator (the zero is −1/ τ ) changes between values –4 and 4. In case of a positive
zero the transfer function describes a so-called non-minimum-phase element.
» s=tf('s')
» tau=[-4 -2 0 2 4];
» D=50*s*s+11*s+1;
» for i=1:5,
» H(i)=(s*tau(i)+1)/D
» end
» t=0:0.1:60;
» Y1=step(H(1),t); Y2=step(H(2),t); Y3=step(H(3),t);
» Y4=step(H(4),t); Y5=step(H(5),t);
» plot(t,[Y1,Y2,Y3,Y4,Y5]),grid,shg
Note that non-minimum-phase systems may show unexpected behaviour, e.g. the step response with one zero on the
right half plane takes an initial slope not in the direction of the steady state value. A positive zero, however,
accelerates the system.
Bode diagrams:
» figure(1),bode(H(1),’r’,H(2),’g’,H(3),’k’,H(4),’y’,H(5),’b’),grid
Nyquist diagrams:
» figure(2),nyquist(H(1),’r’,H(2),’g’,H(3),’k’,H(4),’y’,H(5),’b’),grid
Evaluate the effect of zeros in the Bode and Nyquist diagrams!

8. Delay (dead time): H ( s ) = e − sTD


The description of the delay term in the time and the Laplace operator domain:
y (t ) → y (t − TD )
Y ( s ) → Y ( s )e − sTD
The transfer function of a system with delay:
H D ( s ) = H ( s )e − sTD
In the frequency domain:
e − jωTD = 1, arg {e − jωTD } = −ωTD

24
Elements of a Linear System .

gain: H D = H
phase: arg( H D ) = arg( H ) − ωTD

Investigate the frequency functions of the following transfer functions:


1 1
H1 ( s ) = , H 2 (s) = e −2 s
1 + 10s (1 + 10 s )
The gain value and the phase of the delay term:
H1 = H 2 , gain2=gain1
arg( H 2 ) = arg( H1 ) − ωTD , phase2= phase1 −ωTD

» Td=2
» num1=1
» den1=[10, 1]
Now calculating the Bode diagram the num, den format should be used. Create a logarithmic frequency vector first.
» w=logspace(-2,0,100)
» [gain1,phase1]=bode(num1,den1,w)
» delay=180/pi*Td*w' % the phase angle caused by the dead time ( −ωTD ) is to be converted
from radians to degrees)
The magnitude and the phase with the delay:
» gain2=gain1
» phase2=phase1-delay
» subplot(211),loglog(w, gain1,’r’,w, gain2,’b’),grid;
» subplot(212),semilogx(w, phase1,’r’,w, phase2,’b’),grid
The linearity of the phase curve can be seen well if the linear plot command is used instead of semilogx.
» figure(2),subplot(111),plot(w, phase1,’r’,w, phase2,’b’),grid

Display the Nyquist curve: Calculate the complex values first.


» h1= gain1.*exp(j*phase1*pi/180)
» h2= gain2.*exp(j*phase2*pi/180)
» plot(real(h1),imag(h1),’r’, real(h2),imag(h2),’b’)
The high frequency behaviour can be investigated by selecting a higher frequency range with the logspace command.

The time domain behaviour can be investigated better in SIMULINK, because the time-delay is offered as a single
building block.

Nevertheless the transfer function of the dead time can be approximated by a non-minimum-phase rational fraction
where the first elements of its Taylor expansion are the same as of the exponential transfer function characterizing
the dead time. These rational fractions are called Pade functions. The higher is the degree of the Pade function, the
better is the approximation of the dead time element. It has to be mentioned that with this approximation the step
response starts with +1 or –1 instead of zero. In Matlab the pade command calculates the approximation.

Demonstrating the use of Pade approximation use 5-th order approximation.


H deadtime ( s ) = e − sTD
» H1=tf(num1,den1)
» [numpade,denpade]=pade(Td,5)
» Hdeadtime=tf(numpade,denpade)
» H2=H1* Hdeadtime

Step response:
» figure(1), step(H1,’r’,H2,’g’),grid
Bode diagram:
» figure(2), bode(H1,’r’,H2,’g’),grid

25
Elements of a Linear System .

Nyquist diagram:
» figure(3), nyquist(H1,’r’,H2,’g’),grid

Exercise: Analyse how good is the Pade approximation building up a SIMULINK diagram for the first order lag
element above with a transport delay as a block provided by SIMULINK, and considering the dead time
approximated by the Pade rational fraction.

k
9. Double Integrator: H ( s ) =
s2
4 4 4 4
H1 ( s ) = , H 2 (s) = , H3 = , H4 =
s 2
s (1 + 10 s )
2
s (1 + 10 s )(1 + 2 s )
2
s (1 + 10 s )(1 + 2 s )(1 + 0.1s )
2

Exercise: Calculate the step responses, the Nyquist and Bode diagrams of the given elements.

Summary:
Step responses in steady state ( t → ∞ ) and amplitude response of the frequency function for ω → 0 behave in a
similar way.
Nyquist diagrams of proportional elements at ω = 0 start from a point of the positive real axis, which characterizes
the gain of the element. The Nyquist diagram of an integrating element starts from the infinity direction of the
negative imaginary axis. That of the double integrating elements starts from infinity of the negative real axis. Nyquist
diagrams of derivative elements are initiated from the zero point of the complex plain and start in the direction of the
positive imaginary axis. In case of transfer function containing only lags (no zeros) the Nyquist diagram covers as
many quarters in the complex plain as many is the number of the time lags. Zeros deteriorate the monotonic change
of the phase angle. Bode amplitude diagram of a proportional element starts parallel to the frequency axis with zero
phase angle, Bode amplitude diagram of a system containing one integrator starts with −20dB / decade slope with
−900 phase angle, while that of a system with double integrators starts with −40dB / decade slope with
−1800 phase angle. Time lags break down the slope of the Bode amplitude diagram, while zeros break the slope up.

26
Feedback and Closed Loop .

5. Feedback and Closed Loop


Feedback is the most important structure in control systems. The overall transfer function from the reference input
(set point) to the process output (also called resulting transfer function) can be calculated by the well known
relationships. Also, the related frequency function is immediately obtained by substituting s=jω. MATLAB supports
calculation of the various transfer functions in the closed-loop: series connection, parallel connection or feedback.
Let us summarize the most important commands.

C - controller, P - plant, F – feedback element

r(t) e(t) u(t) y(t)


C ( s) P( s)
R(s) E(s) U(s) Y(s)
-

F (s)

The following transfer functions can be calculated:


Loop transfer function
L( s ) = C ( s ) P( s ) F ( s )
Closed-loop transfer functions (error, control input, output, respectively)
E ( s) 1 U ( s) C (s) Y ( s) C ( s) P( s)
We ( s ) = = , Wu ( s ) = = , T (s) = =
R(s) 1 + L( s ) R( s) 1 + L( s) R( s) 1 + L( s)
The forward path transfer function is E=CP:
» E=series(C,P); or » E=C*P;
The loop transfer function is L=CPF=EF
» L=series(E,F); or » L=C*P*F;
The output closed-loop transfer function:
» T=feedback(E,F,-1);
If the feedback is unity (F=1), the closed-loop transfer function can be calculated by the cloop command:
» T=cloop(E,-1);
The closed-loop transfer function can be calculated directly with the LTI structures:
» T=C*P/(1+C*P*F)
The minreal comand cancels the common zero-pole pairs.
» T=minreal(T)
For the cancellation a tolerance can also be specified. The default value for the tolerance is is sqrt(eps)=1.4901e-008.
» T=minreal(T,0.001)
In this case the zero-pole pair is cancelled if the difference is less than 0.001.
The frequency functions are then calculated by the bode or nyquist commands.

The frequency function:


Compare the frequency functions of the open-loop and closed-loop systems. Examine the step response of the
closed- loop and find out how it is related to the closed-loop frequency function.

Exercise1.
The open loop transfer function is
K
L( s ) =
s (1 + sT1 )
A unity negative feedback is applied. The closed-loop transfer function is

27
Feedback and Closed Loop .

K 1
T (s) = =
s T1 + s + K 1 + s 1 + s 2 T1
2

K K

1
It is a second order term. The general form of a second order element is:
1 + 2ς T0 s + T02 s 2
Matching the coefficients suggests
T1 1
T0 = , ς =
K 2 KT1
Show the open-loop and closed-loop Bode amplitude diagrams for T1=1 and K=0.1 and K= 4 in one figure.
Calculate the significant points of the open loop frequency function.

» s=tf('s')
» T1=1
» K1=0.1
» K2=4
» L1=K1/(s*(s+1))
» L2=K2/(s*(s+1))
The output closed-loop transfer function can be calculated by
num L( s ) num
L( s ) = , T ( s) = =
den 1 + L( s ) num + den
» T1=L1/(1+L1)
» T1=minreal(T1) %cancels the identical zero-pole pairs
or
» T1=feedback(L1,1)
» T2=feedback(L2,1)
Calculate the open-loop and closed-loop frequency functions.
» figure(1)
» step(T1,'r',T2,'y')
» figure(2)
» bode(L1,'b',L2,'c',T1,'r',T2,'y')

With unity feedback the amplitude-frequency function of the closed-loop in the low frequency domain is
approximately one, while in the high frequency domain it runs close to the open loop amplitude function. For large
values of K at the cut-off frequency the amplitude of the closed-loop can be high, indicating high overshoot in the
unit step response.
It is seen that the higher the closed-loop amplification is the higher is the overshoot in the step response.
Let us determine the damping factor of the closed-loop.
» damp(T1)
Eigenvalue Damping Freq. (rad/s)
-1.13e-001 1.00e+000 1.13e-001
-8.87e-001 1.00e+000 8.87e-001
» damp(T2)
Eigenvalue Damping Freq. (rad/s)
-5.00e-001 + 1.94e+000i 2.50e-001 2.00e+000
-5.00e-001 - 1.94e+000i 2.50e-001 2.00e+00
or with variables
» [w01,zeta1]=damp(T1)
» [w02,zeta2]=damp(T2)
The different gains result significantly different behaviour. In the first case the poles are real variables, in the second
example the poles are complex numbers (second order oscillating element).

28
Feedback and Closed Loop .

Exercise2.
Investigate the open-loop and closed-loop behaviour of the system
k 10
L( s ) = =
(1 + s )(1 + 5s ) (1 + s )(1 + 5s )
» s=zpk('s')
» L=10/((1+s)*(5*s+1))
» T=feedback(L,1)

Calculate the steady-state value of the step responses of the open-loop and closed-loop system.

The final value theorem for step response is


1
r (t ) = 1(t ), R( s ) =
s
1
y (t → ∞) = lim sR( s ) H ( s ) = lim s H ( s ) = lim H ( s )
s →0 s →0 s s →0

The steady-state value for the open-loop and closed-loop step responses is

yopen (t → ∞) = lim L( s ) = k = 10
s →0

yclosed (t → ∞) k 10
yclosed (t → ∞) = lim T ( s ) = = =
s →0 1 + yclosed (t → ∞) 1 + k 1 + 10

1 1
The steady-state error: e(t → ∞) = 1 − yclosed (t → ∞ ) = =
1 + k 11
Display the step response of the open- and closed-loop:
» step(L,’r’,T,’b’)
The steady state values can be read from the curves or can be calculated by
» yos=dcgain(L)
» ycs=dcgain(T)

Display the open-loop and closed-loop Bode diagrams.


» bode(L,’r’,T,’b’)

Calculate the steady state error for k=1, 20, 100.

29
Stability .

6. Stability
Consider the following typical closed-loop control configuration with unity feedback gain.

r(t) e(t)
E(s)
L( s)
u(t)
U(s)
y(t)
Y(s)
≡ r(t)
T (s)
y(t)
Y(s)
-

where L( s ) denotes the loop (or open-loop) transfer function. The closed-loop transfer function is calculated by
L( s )
T (s) =
1 + L( s )
The stability of closed-loop systems: The system is stable (in BIBO=Bounded Input Bounded Output sense) if it
produces bounded output for any bounded input.

1. Stability analysis based on the location of the closed loop poles:


The stability of the closed-loop system can easily be determined by analyzing the location of the poles of the closed-
loop transfer function. A system is stable if the real parts of the closed-loop poles are negative, i.e. all the poles are in
the left-hand complex s-plane.

Exercise 1.
The closed-loop (overall) transfer function of a system is
s+5
T (s) = .
s − 3s + 4 s 3 + 10s 2 + 5s − 10
5 4

Is the system stable?


» num=[1, 5]
» den=[1, -3, 4, 10, 5, -10]
» T=tf(num,den)
» poles=roots(den)
poles =
2.1150 + 2.1652i
2.1150 - 2.1652i
-0.9824 + 0.7214i
-0.9824 - 0.7214i
0.7348
Note that the same result is obtained using the LTI structure
» [z,p,k]=zpkdata(T,'v')
The system is unstable, since there are poles with positive real part.
Plot the pole locations in the complex s-plane:
» pzmap(T)
Again, the system is unstable, since there are poles on the right-hand side of the complex plane.

2. Apply the Nyquist stability criterion:


The stability of a feedback system can also be determined by the behaviour of the open-loop:
a. The open-loop does not have unstable poles; all poles are in the left-hand s-plane.
The closed-loop system is stable if the Nyquist curve of the open-loop system does not encircle the (–1+0j) point.
b. The open-loop does have unstable poles:
The closed-loop system is stable if for the Nyquist curve of the open-loop system, the number of counterclockwise
encirclements of the (–1+0j) point is equal to the number of unstable open-loop poles.

30
Stability .

Exercise 2.
The loop transfer function of a system is
10
L( s ) = .
(1 + 10 s )(1 + s )
The system is in a negative unity feedback control loop. Determine the stability of the closed-loop based on the
Nyquist criterion.
» s=tf('s')
» L=10/((1+10*s)*(1+s))
» [z,p,k]=zpkdata(L,'v')
Does the open-loop have unstable poles? Does the Nyquist diagram ecircle the (–1+0j) point? Is the closed-loop
system stable? What happens with stability if the gain 10 in the numerator of the above transfer funcion is increased?
» nyquist(L),grid
Verify the result by calculating the poles of the closed-loop system. In the feedback command 1 means unity
feedback and -1 indicates the negative feedback. (The -1 can be ignored since that is the default value)
» T=feedback(L,1,-1)
or
» T=L/(1+L); T=minreal(T)
The minreal command cancels the zero-pole pairs:
» [z,p,k]=zpkdata(T,'v')
Plot the pole locations:
» pzmap(T)
As seen the system is structurally stable. The poles are left-side poles, the Nyquist diagram does not encircle the
(–1+0j) point even with increased gains.

Exercise 3.
The loop transfer function of a system is
−5
L( s ) = .
(1 − 10s )(1 + 0.1s )
The system is in a negative unity feedback control loop. Determine the stability of the closed-loop based on the
Nyquist criterion.
» L=-5/((1-10*s)*(1+0.1*s))
» [z,p,k]=zpkdata(L,'v')
Does the open-loop have unstable poles? (Yes, p2 = 0.1 )
» nyquist(L)
In which direction the curve encircles (–1+0j) point? (CCW)
Is the system stable? (Yes)
Verify the result by calculating the poles of the closed-loop system.
» T=feedback(L,1)
» step(T)
» [z,p,k]=zpkdata(T,'v')
» pzmap(T)

Examine the system if the polarity of the poles are changed to

−5
L( s ) =
(1 + 10s )(1 − 0.1s )

In which direction the Nyquist curve circles the -1 point? Is the system stable?

31
Stability .

Exercise 4.
The loop transfer function of a system is
1− s
L( s ) = k .
(1 + s )(1 + 0.5s )
The system is in a negative unity feedback control loop. Determine the range for the k parameter for which the open-
loop is stable.
a. k=1 is assumed:
» L=(1-s)/((1+s)*(1+0.5*s))
» [z,p,k]=zpkdata(L,'v')
Does the open-loop have unstable poles?
» nyquist(L),grid
Determine the point where the Nyquist curve intersects the real axis (0.666). The system can be multiplied by the
gain k=1/0.666 to be marginally stable. The system is stable if 0< k<1.5 (for k>0).
» zoom
b. k=-1 is assumed:
» nyquist(-L), grid
Determine again the point where the Nyquist curve intersects the real axis (-1); find k for stability (closed-loop
system is stable if k>-1>0).
Putting together the two intervals: The system is stable if –1<k<1.5

The stability can also be determined by the rlocus command, which plots the locus of the roots [gyökhely-görbe]
supposing a parameter (generally the gain) changing its value from zero to infinity.
» help rlocus
» rlocus(L)
» rlocfind(L)
(We can use the mouse to find the gain value at a given point).
Phase margin, Gain margin:
If the system is stable the degree of stability is an important system property. The phase margin and gain margin tells
how far is the system from being marginally stable.
The phase margin can be calculated from the phase at the cut-off frequency ω c .
ϕ m = ϕ (ωc ) + 180°
At the cut-off frequency the absolute value of the open-loop frequncy function is 1:
ωc : L(ω ) ω =ω = 1
c

If the phase margin is positive, the system is stable. For example, if the phase is ϕ = −120° , then the phase margin
is ϕ m = ϕ + 180° = −120° + 180° = 60° , that is the system is stable.
By multiplying the gain of the system with the gain margin the system becomes marginally stable.
1
gm = , where ϕ (ωπ ) = −180°
L (ωπ )
where
ωπ : ϕ (ω )ω =ωπ = −180
The phase margin can be demonstrated both on the Nyquist plot and the Bode diagram. The MATLAB margin
command helps to directly calculate the phase margin.

Exercise 5.
The transfer function of a system is
1
L( s ) = .
(0.5 + s )( s 2 + 2 s + 1)
The system is in a negative unity feedback control loop. Calculate the phase margin, the gain margin and the cut-off
frequency of the system:
» L=1/((0.5+s)*(s^2+2*s+1))

32
Stability .

» [gm,pm,wg,wc]=margin(L)
gm is the gain margin, pm is the phase margin, wc is the cut-off frequency and wg is the frequency where the phase is
-180º. These values can be displayed graphically too.
» margin(L);
Note that in case of graphic display the Gm value is obtained in decibels: Gm=20*log10(gm)

The amplitude, phase and frequency values can be looked at directly by arranging them into table format:
» w=logspace(-1,1,100);
» [num,den]=tfdata(L,'v')
» [mag,phase]=bode(num,den,w);
» Tabl=[mag, phase, w’]

mag phase w
…….
1.1123 -99.5242 0.5094
1.0643 -103.0406 0.5337
1.0158 -106.6104 0.5591≈wc
0.9669 -110.2286 0.5857
0.9178 -113.8900 0.6136
…….
0.2706 -173.3384 1.2915
0.2449 -176.7848 1.3530
0.2211 -180.1658 1.4175≈wg
0.1991 -183.4774 1.4850
0.1789 -186.7160 1.5557
…….

The phase margin can be read from this table by finding the phase value belonging to the 1.0 magnitude value:
pm=180-106.6=73.4. The cut-off frequency: w=wc=0.77. The gain margin can be calculated from the gain
belonging to the −180° phase value: gm=1/0.221=4.52.
Bode Diagrams
Gm=13.064 dB (at 1.4142 rad/sec), Pm=72.227 deg. (at 0.56754 rad/sec)
20

0
Phase (deg); Magnitude (dB)

-20

-40

-60

-80

-100

-200

-300
10 -1 10 0 10 1

Frequency (rad/sec)

33
Series PID Compensation .

7. Series PID Compensation


Consider the following closed-loop control system formed by a series compensator C ( s ) and a process P ( s ) :

r(t) e(t) u(t) y(t)


C (s) P(s )
R(s) E(s) U(s) Y(s)
-

Related to the above configuration a fundamental problem set up is to design a series compensator C ( s ) for which
the closed-loop system is stable and the design specifications are met. First discuss some practical design and quality
specifications, then a couple of design procedures will be performed.

Design and quality specifications: The aim of a control system is to ensure good reference signal tracking
and disturbance rejection properties, meeting given quality specifications. Specifications - what is considered as
"good" - have to be formulated.
First of all a control system has to be stable. Stability has different formulations. Bounded input -bounded output
(BIBO) stability means that every bounded input results in bounded output. Asymptotic stability means that the
transients of the system are decreasing, tending to zero. Stability has to be ensured even in cases when system
parameters may change within a given range. In other words a good design should be robust with respect to the
parameter variations exhibited by the process.
Another important requirement is static (or steady-state) accuracy. In steady-state the output signal has to
approximate its prescribed reference value within a given accuracy. The control system also has to be able to reject
disturbances within the specified static accuracy. Static accuracy depends both on the system structure and the input
/disturbance signals.
Transient response of the control system exhibits an important quality of the system. It can be formulated in the
time domain with properties of the unit step responses of the control system for the reference input signal or for the
disturbances. Such properties are the overshoot and the settling time. (The settling time is defined as the time
necessary to reach the steady value within 1-2 %). Generally a small overshoot can be tolerated, but there are
applications where aperiodic transients are required.
A good approximation of the transient properties can also be obtained from system characteristics in the frequency
domain. The overshoot observed in the time domain relates to the maximum amplitude of the closed-loop frequency
function or to the phase margin calculated from the open-loop frequency response. The settling time can be estimated
from the cut-off frequency of the open-loop.

Introducing L( s ) = C ( s ) P ( s ) as the loop transfer function the closed-loop system can be characterized by the
following transfer functions:
E (s) 1 U ( s) C ( s) Y ( s) C (s) P(s)
We ( s ) = = , Wu ( s ) = = , T (s) = =
R( s) 1 + C ( s) P(s) R( s) 1 + C ( s) P( s) R( s) 1 + C (s) P(s)
where r(t) is the reference (input) signal, u(t) is the control signal (the input of the process) and y(t) is the output
signal.

In order to meet the design specifications the parameters of the controller have to be adjusted accordingly. As
mentioned earlier, the time domain specifications have corresponding specifications in the frequency domain.
- The output overshoot depends on the phase margin. For typical systems, if the phase margin is approximately
60°, then the overshoot of the step response is around 5-10%.
3 10
- The settling time depends on the cut-off frequency and it can be approximated by ≤ ts ≤ .
ωc ωc
- The maximum of the control signal depends on the controller gain and PD pole replacement ratio (see the
examples later on).
As far as the structure of the series compensators are concerned, the following controllers will be considered:

34
Series PID Compensation .

C P ( s ) = A = kc
1 1 + Ti s
CPI ( s ) = A(1 + ) = kc
sTi s
sτ 1 + Td s
C PD ( s ) = kc (1 + ) = kc
1 + T1s 1 + T1s
1 sτ 1 + Ti s 1 + Td s
C PID ( s ) = CPI ( s )C PD ( s ) = A(1 + + ) ≈ kc , (only if Ti > τ > T1 )
sTi 1 + T1s s 1 + T1s

PI controller is used, when zero steady-state error is required in the closed-loop step response, therefore an integrator
has to be included in the control loop. PD controller is used if the system needs to be accelarated. In case of an ideal
PD controller the value of the T1 parameter is zero, however an ideal PD controller can not be realized. Clearly, a
PID structure is to be used if both steady-state and transient performances are to be met.
It is seen that in a PID controller there are four design parameters. We can choose these parameters in the case of
pole cancellation in the following way. The Ti parameter is chosen for the largest time constant of the process and
the Td parameter is equal to the second largest time constant. This way the terms in the numerator of the controller
Td
will cancel the poles of the plant. The T1 parameter is calculated in the T1 = form, where n p is the pole
np
replacement ratio. It is a good practical rule that n p is between 5 and 10. If it is selected higher then the system
becomes faster, but the maximum of the control signal increases. Since the kc parameter does not influence the
phase of the open loop, it can be used to set the phase margin.

The steps of the series P, PI, PD, PID controller design will be shown for the following process:
1
P(s) =
(1 + 10 s )(1 + s )(1 + 0.2 s )
Design series P,PI,PD,PID type controllers for 60° phase margin. Calculate the quality parameters. Display the step
response and the control signal.
P type controller design:
The controller is in the C ( s ) = kc form. Only the kc parameter has to be calculated.
Define the transfer function
» s=tf('s')
» P=1/((1+10*s)*(1+s)*(1+0.2*s))
» P=zpk(P)
First select the gain kc=1.
» kc=1
» C=kc
» L=C*P
Display the loop Bode diagram and calculate the important parameters (phase margin, gain margin, cut-off
frequency) with the margin command.
» margin(L)
The system has significant phase and gain margin. Since the phase decreases from 0° to -270°, the phase margin can
be set by changing the gain. The gain parameter of the controller can be calculated from the amplitude parameter
belonging to the ϕ = ϕ m − 180° phase. This value can be read from a table or can be calculated by the margin
command.
The margin command can immediately calculate the required gain (gm) value. If we substract the phase margin
value from the phase of the system, then the margin command calculates the required gain as follows:
» [mag,phase,w]=bode(L);
» gm=margin(mag,phase-60,w)

35
Series PID Compensation .

» kc=gm
The controller is: CP ( s ) = kc = 7.51
Equivalently, the kc parameter can be calculated from a table as follows:
» w=logspace(-1,1,100);
» [num,den]=tfdata(L,’v’)
» [mag,phase]=bode(num,den,w);
» Table=[mag, phase, w’]

mag phase w
0.1527 -115.4479 0.5591
0.1442 -117.3498 0.5857
0.1361 -119.2727 0.6136
0.1283 -121.2166 0.6428

kc will be the reciprocal of the magnitude belonging to phase angle -120°. The frequency belonging to this value
will be the cut-off frequency:
kc=1/0.1361=7.3475, wc=0.6136
Refining the resolution in the frequency vector w, it can be seen that both solutions will lead to the same result.
As always, it is important to verify the behaviour of the closed-loop system.
» C=kc
» L=kc*L
Verify the stability-related parameters (phase and gain margin):
» margin(L)
» [gm,pm,wg,wc]=margin(L)
The phase margin is really 60°. The cut-off frequency is ω c =0.6245. Calculate the closed-loop transfer function:
» T=feedback(L,1)
Compare the Bode diagram of the open-loop and closed-loop:
» bode(L,'r',T,'b')
Display the step response of the closed-loop system:
» step(T)
Also, calculate the step response:
» t=0:0.05:10;
» y=step(T,t);
The maximum value can be calculated.
» ym=max(y)
The steady state value of the step response is equal to the DC (zero frequency) gain.
» ys=dcgain(T)
From these values the percentage overshoot can be calculated.
» yt=(ym-ys)/ys
The steady state error
» es=1-ys
The behaviour of the u(t) control signal also has to be examined. This is important because this is the input of the
process and generally physical limitations exist in practice for this signal. Calculate the transfer function between the
reference signal and control signal.
» U=feedback(C,P)
or
» U=C/(1+C*P)
In case of step reference:
» u=step(U,t);
» plot(t,u)
The most common limitations are given for the maximum of the control signal. Let’s calculate it:
» um=max(u)

36
Series PID Compensation .

P,PI,PD,PID type controller design:


Create an m file to calculate all the performance related parameters. Herewith along the results of the design are
summarized in a table format:

C ( s) L( s ) = C ( S ) P ( s ) kc ωc yt es um ~ ts

Plant 1 0 0 0.5 2 12
(1 + 10s )(1 + s )(1 + 0.2s )
P kc kc 7.51 0.62 0.153 0.117 7.51 8
(1 + 10s )(1 + s )(1 + 0.2s )
PI (1 + 10s ) kc 0.504 0.46 0.078 0 5.16 9
kc
s s (1 + s )(1 + 0.2 s )
PD (1 + s ) kc 21.69 1.98 0.103 0.044 217 2
kc
(1 + 0.1s ) (1 + 10 s )(1 + 0.1s )(1 + 0.2s )
PID (1 + 10 s )(1 + s ) kc 19.34 1.79 0.076 0 193 2
kc
s (1 + 0.1s ) s (1 + 0.1s )(1 + 0.2 s )

Control signal, u(t)


Step response 8
1.4
PI
6
1.2
PI P
4
PID
1
PD 2
P
0.8 0

0.6 -2
0 2 4 6 8 10

0.4 Control signal,


250

0.2 200

150
0
0 1 2 3 4 5 6 7 8 9 10 100

50
PID
0
PD
-50
0 2 4 6 8 10

The primary requirements for the control system were good reference signal tracking and fast transient response. It is
seen that P compensation does not meet any of them. The settling process is quite slow, and the output signal does
not reach the required steady value y=1. With PI compensation the steady state error is zero, but the system is still
slow. With PD compensation the control system has been accelerated, but it tracks the step reference signal with
steady state error. This acceleration effect is reached by a significantly increased value of the control signal
(manipulated variable) u(t). With the PID controller the behaviour of the control system became faster accompanied
by zero steady state error.
Write a MATLAB program (create an m file, a text file with extension m). Open a new m-file from MATLAB file
menu. Write the required MATLAB commands into the empty file. Save the file: Save As, C:/Matlab/work/myfile.m
To activate your program just type the file name with no extension in the MATLAB Command Window:

37
Series PID compensation .

» myfile

The following MATLAB commands calculate the parameters associated with the controller design:
clear figure(1),step(Tp,'r',Tpi,'b',Tp
s=tf('s'); d,'g',Tpid,'y')
P=1/((1+10*s)*(1+s)*(1+0.2*s)) figure(2),step(Up,'r',Upi,'b')
P=zpk(P) figure(3),step(Upd,'g',Upid,'y')

Cp=1 t=0:0.05:10;
Cpi=(1+10*s)/(10*s) yp=step(Tp,t);
Cpd=(1+s)/(1+0.1*s) ypi=step(Tpi,t);
Cpid=Cpi*Cpd ypd=step(Tpd,t);
Cpid=zpk(Cpid) ypid=step(Tpid,t);

[mag,phase,w]=bode(Cp*P); ysp=dcgain(Tp)
kp=margin(mag,phase-60,w) yspi=dcgain(Tpi)
Cp=kp*Cp; yspd=dcgain(Tpd)
yspid=dcgain(Tpid)
[mag,phase,w]=bode(Cpi*P);
kpi=margin(mag,phase-60,w); ep=1-ysp
Cpi=kpi*Cpi; epi=1-yspi
epd=1-yspd
[mag,phase,w]=bode(Cpd*P); epid=1-yspid
kpd=margin(mag,phase-60,w);
Cpd=kpd*Cpd; ytp=(max(yp)-ysp)/ysp
ytpi=(max(ypi)-yspi)/yspi
[mag,phase,w]=bode(Cpid*P); ytpd=(max(ypd)-yspd)/yspd
kpid=margin(mag,phase-60,w); ytpid=(max(ypid)-yspid)/yspid
Cpid=kpid*Cpid;
up=step(Up,t);
Tp=feedback(Cp*P,1); upi=step(Upi,t);
Tpi=feedback(Cpi*P,1); upd=step(Upd,t);
Tpd=feedback(Cpd*P,1); upid=step(Upid,t);
Tpid=feedback(Cpid*P,1); upim=max(upi)
updm=max(upd)
Up=feedback(Cp,P); upidm=max(upid)
Upi=feedback(Cpi,P);
Upd=feedback(Cpd,P);
Upid=feedback(Cpid,P);

The control performance can be analysed also by building a SIMULINK block-diagram and running it with the given
plant and the designed controllers.

Compensating complex poles (second order oscillating element):


If the plant has a complex pole pair,
A A
P( s) = = 2 2 , where p1,2 = a ± jb
( s − p1 )( s − p2 ) s T0 + 2ζ T0 s + 1
1
its Bode diagram has a break frequency at frequency, where the slope of the asymptotic Bode amplitude diagram
T0
changes from 0 to –40dB/decade. In this case a possible pole-cancellation PID controller design strategy can be

38
Series PID compensation .

setting the time constants in the numerator of the PI and PD terms of the controller to the natural frequency of the
1 + T0 s 1 + T0 s
plant, that is Ti = T0 and Td = T0 , C ( s ) = kc .
s 1 + T1s
Another possibility is just to use a pure integrator as a controller setting the phase margin for about 60º.

Sensitivity
The parameters of a plant are not exact values, generally they are measured quantities. For example measuring the
step response we can characterize the type of the element (e.g. first order lag with dead time or second order
oscillating element). Supposing this type for the element structure the parameters can be estimated also from the
measured input-output data (system identification). The parameters of the model of the element obtained in this way
are not accurate values, their values can be given around a nominal value between a minimum and a maximum.
Analysis of sensitivity of the control system to plant parameter uncertainties is an important question.

Designing a controller we have to consider these parameter uncertainties. The control system has to behave
according to the required performance for the nominal parameter values and still in an acceptable way if the real
parameters differ from their nominal values in the given band. Different rubust control design techniques address the
solution of this problem.

To demonstrate controller design for a second-order oscillating element and the effect of parameter uncertainties let
us design a PID controller for the following plant:
1
P( s) = , and the damping factor ς changes between 0.2 and 1.
4 s + 4ς s + 1
2

Design the controller for phase margin 60º.

The MATLAB commands:

» clear
» s=tf('s');
Define the plant:
» P1=1/(4*s*s+0.8*s+1) %zeta=0.2
» P2=1/(4*s*s+2.8*s+1) %zeta=0.7, nominal
» P3=1/(4*s*s+4*s+1) %zeta=1
» P=zpk(P); P1=zpk(P1); P2=zpk(P2); P3=zpk(P3);
The controller with gain kp=1:
» Cpid=(1+2*s)*(1+2*s)/((2*s)*(1+0.2*s))
» Cpid=zpk(Cpid)
The Bode diagram for the nominal plant P2:
» [mag,phase,w]=bode(Cpid*P2);
Set the controller gain ensuring 60º phase margin:
» kp=margin(mag,phase-60,w)
» Cpid=kp*Cpid;
» figure(1); bode(Cpid*P2) % show the Bode diagram
» Tpid=feedback(Cpid*P2,1);
» Upid=feedback(Cpid,P2);
Calculate the resulting transfer functions
» t=0:0.05:10;
» ypid=step(Tpid,t)
» figure(2); plot(t,ypid),grid,shg
» upid=step(Upid,t);
» figure(3); plot(t,upid),grid,shg
Calculate and plot the output and the control signal
» Tpid1=feedback(Cpid*P1,1);
» Tpid3=feedback(Cpid*P3,1);
Calculate and show the output signal with the nominal plant and with the modified damping factors

39
Series PID compensation .

» ypid1=step(Tpid1,t);
» ypid3=step(Tpid3,t);
» figure(4); plot(t,[ypid,ypid1,ypid3]),grid

The figure below shows the output of the control system with the controller designed for the nominal system
( ς = 0.7 ), and applying the controller also for the plant with changed damping factors. It is seen that the overshoot
is much higher for the smaller damping factor. The control system is sensitive for the uncertainties in this parameter.

1.4

1.2
0.7 0.2

1
1

0.8

0.6

0.4

0.2

0
0 1 2 3 4 5 6 7 8 9 10

40
Series Compensation in Case of Dead Time .

8. Series Compensation in Case of Dead Time


Consider the following closed-loop system:

r(t) e(t) u(t) y(t)


C ( s) Pd ( s )
R(s) E(s) U(s) Y(s)
-

where P( s ) is the transfer function of the process without dead time, C ( s ) is the controller and
L( s ) = C ( s ) Pd ( s ) is the open loop transfer function with
e− s
Pd (s) = P(s)e− sTd = .
1 + 20s

The C ( s ) controller has to be designed such that the performance of the whole system satisfy the design
specifications. The closed-loop system should have zero steady state error.
Select a PI compensator.
1 + 20 s
C ( s ) = kc
s
The closed-loop transfer function:
1 + 20 s e − s e− s
L ( s ) = C ( s ) P ( s ) = kc = kc
s 1 + 20 s s
kc has to be calculated to achieve 60° phase margin.
The magnitude and phase values of the open-loop can be calculated from the system with no delay, since
− jωTd − jωTd
Pd ( jω ) = P( jω )e = P( jω ) , since e = 1;

Arg { Pd ( jω )} = Arg { P( jω )} + Arg {e } = Arg {P( jω )} − ωTd


− jωTd

» s=zpk(‘s’)
» P=1/(1+20*s)
» Td=1
» kc=1
» C=kc*(1+20*s)/s
The loop transfer function
» L=C*P
» L=minreal(L)
The magnitude and phase values of the loop can be calculated for the system without the delay:
» [num,den]= tfdata(L,’v’)
» [mag,phase,w]=bode(num,den);
» magd=mag;
» phased=phase-w*Td*180/pi;
The kc gain can be calculated in several ways:
Method 1. Use the margin command:
The gain margin for -120° phase angle is
» gm=margin(magd,phased-60,w)
This is the value for kc
» kc=gm
0.5229

41
Series Compensation in Case of Dead Time .

Method 2. Use a table:


» w=logspace(-2,1,100);
» [mag,phase]=bode(num,den,w);
» magd=mag
» phased=phase-w’*Td*180/pi
Now create a table
» T=[ phased, magd, w’]

-116.5943 2.1544 0.4642


-118.5162 2.0092 0.4977
-120.5770 1.8738 0.5337 <=
-122.7868 1.7475 0.5722
-125.1562 1.6298 0.6136

The mag value for phased = −120° is mag ( phased = −120° )=1.8738 .
From here kc=1/1.8738
» kc=1/1.8738
0.5337

Calculate again the loop by


» C=kc*(1+20*s)/s
Verify that the phase margin is 60°.

The system can be simulated by the following SIMULINK model (recall that SIMULINK offers the option to easily
add dead time to the process):

Scope1

C P

Step LTI System LTI System1 Transport Scope2


Delay

The scope blocks can be used to transfer the results of the simulation to the Matlab workspace. Change the
parameters in the Scope graphic window ‘properties’ menu.
Data history: Save data to workspace
Variable name: ty (tu for the control signal)
Matrix format
The time and output vectors can be gained easily after the 1.4
simulation. 1.2
» t=ty(:,1)
» y=ty(:,2) 1
» plot(t,y),grid 0.8
Similarly, the control signal u can be generated.
The u, y vectors can be used to verify further design 0.6

requirements (overshoot, settling time, maximum control effort, 0.4


etc.).
0.2
(To workspace blocks from the sinks library can alternatively
be connected to signals to be saved.) 0
0 5 10 15 20 25 30

42
Series Compensation in Case of Dead Time .

Pade approximation: The design can be done also with the help of the Pade approximation. The delay term is
− sT
approximated by a rational fraction: PPADE ( s) ≈ e d

r(t) e(t) u(t) y(t)


C (s) P( s) PPADE ( s )
R(s) E(s) U(s) Y(s)
-

In Matlab the pade command calculates the approximation. Use 5-th order approximation,
» [numpade,denpade]=pade(Td,5)
» Ppade=tf(numpade,denpade)
» Pd=P*Ppade
With this transfer function the kc gain then can be calculated the same way as for a system with no delay.
» [mag,phase,w]=bode(Pd);
» kc= margin(magd,phased-60,w)
0.5229
It has to be emphasized that the previous solution and simulation is the more accurate and therefore it is preferred.

43
Controlling Unstable Processes

9. Controlling Unstable Processes


Exercise 1. Consider the following unstable process: Bode Diagrams
From: U(1)
1
P(s) = -20
( s + 2)( s − 5)

Phase (deg); Magnitude (dB)


-40

Question: Can the system be stabilized with a -60

proportional (P) controller, C ( s ) = kc ? -80

-100
» s=zpk('s')
180
» P=1/((s+2)*(s-5))
» figure(1); nyquist(P); 170

To: Y(1)
» figure(2); 160
» figure(3);rlocus(P);
It is seen that stability is hopeless. The generalized 150
10 10 10 -1
10 0 1 2

Nyquist plot will never encircle the (-1+j0) point


Frequency (rad/sec)
counter-clockwise [óramutató járásával ellenkezõ
irányban]. Just to check this: the poles of the closed-loop control system are determined by the roots of s2-3s-
10+kc=0. It is well known that the necessary condition for closed-loop stability is that all the coefficients in the
above characteristic equation must have identical sign. This condition can not be met since kc does not have an
influence on the coefficient (-3). This can also be verified by looking at the root locus (location of the closed-loop
poles while 0 ≤ kc < ∞).

Exercise 2. As a second example consider the following variation of the above process:
1
P(s) =
( s − 2)( s + 5)
Is there a stabilizing proportional controller, C ( s ) = kc ?
» clear
» s=zpk('s')
» P=1/((s-2)*(s+5))
» figure(1); nyquist(P);
» figure(2); bode(P);
» figure(3);rlocus(P);
This time the Nyquist plot is rather promising. It is seen that increasing the kc gain will result in satisfying the
generalized Nyquist criterion for stability. Specifically, kc >10 will stabilize the closed loop system. Select a gain
value to achieve maximum phase margin.
First calculate the magnitude and phase values of the open-loop for kc=1.
» C=1; Bode Diagrams
» L=C*P; From: U(1)
-20
» [num,den]=tfdata(L,’v’)
Phase (deg); Magnitude (dB)

» [mag,phase,w]=bode(num,den); -40

Method 1. Read the values from a table. -60


» T=[ phase, mag, w] -80
-155.9825 0.0613 2.2122 -100
-154.7797 0.0506 2.8072 -150
-154.6231 0.0452 3.1623
-160
To: Y(1)

-154.7797 0.0399 3.5622


-155.9825 0.0300 4.5204 -170

-180
The Bode plot phase curve has a maximum value at 10 10 10 10 -1 0 1 2

ϕ = -154.62 mag=0.045 ω =3.16 . The maximum Frequency (rad/sec)


phase margin that can be achieved with proportional compensation is pm=180-154.6=25.8°. For the maximum phase
margin the gain should be selected to kc=1/0.045=22.22
» kc=22.22
Method 2. The maximum can be found also with the max command.

44
Controlling Unstable Processes

» [maxphase,index]=max(phase)
» kc=1/mag(index)
Recalculate the system with the new gain.
» C=kc;
» L=C*P;
The margin command calculates the phase margin.
» margin(L);
The system has quite low phase margin ( Pm = 25.4° ).
Look at the closed loop performance:
» T=feedback(L,1)
» step(T)
Stability is demonstrated, however, the steady-state error is close to 100%. A PID compensator is necessary to
improve the performance.

Exercise 3. Design a PID compensator to reduce the steady state error.


( s + 2) ( s + 5)
C ( s ) = kc .
s s + 50
The unstable pole p1 = 2 can not be compensated directly with an unstable zero, because in case of parameter
change the system becomes unstable. The p1 = 2 unstable pole, instead is compensated by a stable PI term. The
p1 = −5 pole is moved to higher frequency by the PD term (p=-50, the pole replacement ratio is 10).
The kc gain can be calculated again by finding the maximum phase value. Close all the figure windows.
» clear Bode Diagrams
» s=zpk('s') Gm=-17.25 dB (at 2.0851 rad/sec), Pm=58.122 deg. (at 14.596 rad/sec)
100
» P=1/((s-2)*(s+5))
50
» C=((s+2)*(s+5))/(s*(s+50))
Phase (deg); Magnitude (dB)

» L=C*P 0

» L=minreal(L) -50
» bode(L) -100
» [mag,phase,w]=bode(L); -100
Calculate the gain for the maximum phase. -150
(kc=760, pm=180-121=59) -200
» [maxphase,index]=max(phase)
-250
» kc=1/mag(index)
The gain is kc=760.26 and the phase margin is -300
10 10 10 10 -2
10 10-1 0 1 2 3

pm=180+maxphase =59.
Frequency (rad/sec)
Verify the system behaviour.
» kc=760.26 Step Response
From: U(1)
» C=kc*((s+2)*(s+5))/(s*(s+50)) 1.5
» L=C*P, L=minreal(L)
» margin(L)
Look at the closed loop performance: 1
Amplitude

» T=feedback(L,1)
To: Y(1)

The steady state error is reduced to zero.


» es=1-dcgain(T) 0.5
Verify also the u control signal.
» U=feedback(C,P);
» step(U) 0
0 0.2 0.4 0.6 0.8 1 1.2
» u=step(U)
» um=max(u) Time (sec )
The maximum of the control signal, um is quite high. It can be reduced by reducing the pole replacement ratio

45
Discrete-time Systems .

10. Discrete-time Systems


z-transforms
In computer controlled systems signals in the digital controller are stored and processed in digital form. Also, due to
the nature of the digital computers the processing takes place at discrete time events. Consequently, analog
continuous-time signals involved in a closed-loop control system have to be sampled and converted to digital form.
The digital form of the y (t ) analog signal can be derived by applying a train of shifted unit pulses:
yd (t ) = { y (nTs )} = y (0)δ (t ) + y (Ts )δ (t − Ts ) + y (2Ts )δ (t − 2Ts ) + y (3Ts )δ (t − 3Ts ) + ...
The Laplace-transform of yd (t ) is
yd ( s ) = y (0) + y (Ts )e− sTs + y (2Ts )e −2 sTs + y (3Ts )e−3 sTs + ...
Introducing
z = e sTs , z −1 = e − sTs

yd ( z ) = y (0) + y (Ts ) z −1 + y (2Ts ) z −2 + y (3Ts ) z −3 + ... = ∑ y (kTs ) z − k
k =0
−1
This is the well-known z-transform of the sampled signal, where z is frequently interpreted as a shift operator.

Exercise 1.
The z-transform of a signal is
2z2 − z
yd = , Ts = 0.5
z 2 − z + 0.24
Calculate the first N s = 5 samples of the inverse z-transform of the signal.
a/ Matlab commands:
» num=[2, -1, 0]
» den=[1, -1, 0.24]
» Ns=5;
» yd=dimpulse(num,den,Ns);
» plot(1:Ns,yd,'*')
Alternatively, symbolic input and LTI structure can also be used. In this case, similarly to the s variable, the z
variable should be defined, however, also the Ts sampling time has to be specified for the definition:
» Ts=0.5;
» z=tf(‘z’,Ts);
» Y=(2*z^2-z)/(z*z-z+0.24);

One advantage of using the LTI structure is that the very same MATLAB command impulse can be used both for
continuous-time and discrete-time systems. If the sampling time is zero, then the system is considered as a
continuous time system. Citing the MATLAB Help we have

IMPULSE(SYS,TFINAL) simulates the impulse response from t=0 to the


final time t=TFINAL.

To obtain the same output samples we calculated earlier tfinal = 2 is to be assigned for the final time:
» tfinal=2;
» Yd=impulse(Y,tfinal);
» plot(Yd,’*’);
(The first value belongs to the first sample at t=0.)

b/ Partial-fraction expansion
The z-transform of an exponential component is

46
Discrete-time Systems .

z
y (t ) = e− at , yd (nTs ) = e− anTs 
Z

z − e − aTs
The signal can be converted to the
num num1  r r 
yd ( z ) = =z = z  k0 + 1 + 2 
den den  z − p1 z − p2 
partial fraction expanded form by the MATLAB residue command.

» num1=[2, -1]
» den=[1, -1, 0.24]
» [r,p,k0]=residue(num1,den)
r =
1
1
p =
0.6000
0.4000

k0 =
[]
» Ts=0.5
 r r   1 1  z z
yd ( z ) = z  1 + 2  = z  + = +
 z − p1 z − p2   z − 0.6 z − 0.4  z − 0.6 z − 0.4
{ }
yd (t ) = { y (nTs )} = eanTs + ebnTs , where e aTs = 0.6, ebTs = 0.4
» a=log(0.6)/Ts
» b=log(0.4)/Ts
ln(0.6) ln(0.4)
a= = −1.02, b = = −1.83
Ts Ts
y (t ) = e−1.02 t + e−1.83 t , t≥0

» t=[0:Ts:2]’
» yd=exp(a*t)+exp(b*t)
» plot(t,yd,’*’);

Pulse transfer function


A continuous-time system together with a zero-order hold on the input can be characterized by its pulse transfer
function, which is the ratio of the z-transform of the sampled output and the discrete-time input signal, where
u(kTs)=u[k].

u[k]
U(z)
D / A u(t)
ZOH U(s)
P(s )
y(t)
Y(s)
y[k]
Y(z) ≡ U(z)
Pd (z) =
Y(z)
U(z)
Y(z)

Exercise 2.
Calculate the pulse transfer function of the continuous-time system with zero order hold on the input.

1 + 5s
P( s) = , Ts = 1
(1 + 10 s)(1 + 8s)(1 + 4 s )(1 + 2s )
» s=zpk(‘s’)
» P=(1+5*s)/((1+10*s)*(1+8*s)*(1+4*s)*(1+2*s))

47
Discrete-time Systems .

The continuous-time system is obtained in zero-pole form:


s + 0.2
P( s ) = 0.0078
( s + 0.5)( s + 0.25)( s + 0.125)( s + 0.1)
» [z,p,k]=zpkdata(P,’v’)
The poles of the continuous-time system: -0.5, -0.25, -0.125, -0.1
The zero of the continuous-time system: -0.2

The pulse transfer function of the system with zero order hold can be calculated by the c2d command.
» Ts=1;
» Pd=c2d(P,Ts)
( z + 3.088)( z − 0.8187)( z + 0.2198)
Pd ( z ) = 0.0011
( z − 0.9048)( z − 0.8825)( z − 0.7788)( z − 0.6065)
The discrete zeros and poles:
» [zd,pd,kd]= zpkdata(Pd,’v’)
The poles of the discrete system: 0.9048, 0.8825, 0.7788, 0.6065
The zeros of the discrete system: -3.088, 0.8187, -0.2198
Notice that the discrete poles are resulted from the z = e s transformation.
sT

» exp(-0.5)
0.6065
Similarly
» exp(p)
The relationships between the continuous and discrete zeros are more complicated.

Final value theorems


The initial value of a discrete signal is obtained by
lim y (nTs ) = lim y ( z )
n →0 z →∞
The final value is obtained by relationship
lim y (nTs ) = lim(1 − z −1 ) y ( z )
n →∞ z →1

Exercise 3.
The input of the plant in exercise 2. is a unit step. In the discrete case after sampling we get a pulse train with unity
z
amplitude whose z-transform is .
z −1
Calculate the initial and final value of the output signal of the discrete system.
z
y( z) = Pd ( z )
z −1
The initial value is 0, as the degree of the denominator is higher than the degree of the numerator.
The final value is obtained substituting z=1 in the expression of Pd ( z ) . The final value of the step response is the
gain of the pulse transfer function, which can be calculated by the dcgain command the same way as for continuous
systems (the ddcgain(numd,dend) command can also be used). The commands of the Control System Toolbox
determine that the system is continuous or discrete based on the sampling time parameter. If the sampling time
parameter of an LTI system is set to zero than the system is considered as continuous otherwise it is assumed
discrete.
» A=dcgain(P)
» Ad=dcgain(Pd)
Both A (continuous) and Ad (discrete) give values of 1, as expected.

48
Discrete-time Systems .

Stability
The discrete closed-loop control system is stable, if the roots of its characteristic equation (the poles of the
denominator of the closed-loop pulse transfer function are inside of the unit circle.

Exercise 4.
Determine if the following discrete transfer function is stable.
z 2 - 0.3 z - 0.1
Pd (z) = , Ts = 1
z 3 + 3 z 2 + 2.5 z + 1
Pole-zero map
Define the system in matlab: 1
» Ts=1 0.8
» z=tf(’z’,Ts) 0.6
» Pd=(z*z-0.3*z-1)/(z^3+3*z^2+2.5*z+1) 0.4

Calculate its poles: 0.2

Imag Axis
» [z,p,k]=zpkdata(Pd,’v’) 0

The absolute value of the poles: -0.2

» abs(p) -0.4

ans = -0.6

2.0000 -0.8

-1
0.7071 -2.5 -2 -1.5 -1 -0.5 0 0.5 1

0.7071 Real Axis

The system is unstable, since one of the values is larger than 1.


The stability can also be determined by visualizing the locations of the Step Response
From: U(1)
discrete poles. 20

» pzmap(Pd) 15

» zgrid Amplitude
10

The system is unstable again because the -2 pole falls outside of the unity To: Y(1) 5
circle. The zgrid command displays the unity circle and the constant 0
damping lines for second complex pole pairs. -5
The stability can be determined in the time domain by looking at the step
-10
response. 0 1 2 3 4 5

» step(Pd) Time (sec )

The output is not bounded therefore the system is unstable.

49
Discrete PID Control .

11. Discrete PID Control


This demo shows how to use MATLAB to design and simulate sampled-data control systems. The design uses
frequency domain considerations leading to a pole-cancellation PID control method.

r[k] e[k] u[k] u(t) y(t)


C (z ) D/ A P(s )
- U(z) Y(s)

y[k]
A/ D Y ( z)
Y(z) P( z ) =
U ( z)
The continuous-time process is given by
e− s
P (s) = = P1 ( s )e − s
(1 + 10s )(1 + 5s )
Observe that the system has a delay term of 1 sec. The sampling time is 1 sec.
Design a series PID compensator to meet the following design specifications:
- Phase Margin≅60°
- Settling time should be minimal
- The closed-loop system should follow unit step reference signal without steady error (type one system).
−s
Define the continuous-time process without the e time delay.
» s=zpk(’s’);
» P1s=1/((1+10*s)*(1+5*s));
Find the discrete-time process model assuming zero-order hold (without delay):
 P ( s) 
P1 ( z ) = (1 − z −1 ) Z  1 
 s 
By MATLAB:
» Ts=1;
» P1z=c2d(P1s,Ts,’zoh’);
Note that it is not necessary to include the default ‘zoh’ string.
Add the time-delay to the process by multiplying P1 ( z ) with z , since
−1
Z {e− s } = z −1 .
The z variable can be defined similarly to the z variable.
» z=zpk(‘z’,Ts)
» Pz=P1z/z
1
P ( z ) = z −1 ⋅ P1 ( z ) = ⋅ P1 ( z ) = 0.00905
( z + 0.9048)
z ( z − 0.9048)( z − 0.8187 ) z
The zeros and poles of the discrete system are
» [zd,pd,kd]=zpkdata(Pz,’v’)

PI term is necessary to achieve the steady state zero error requirement. PD term is used to accelerate the system
response. The PI and PD break frequencies can be calculated similarly to the continuous system. Ti is the largest
time constant of the system and Td is the second largest time constant.
Ts 1
− −
PI : break frequency at 0.1 ⇒ e
Ti
=e 10
= e −0.1 = 0.9048
Ts 1
− −
PD : break frequency at 0.2 ⇒ e
Td
=e 0.5
= e −0.2 = 0.8187
The discrete controller is

50
Discrete PID Control .

z − 0.9048 z − 0.8187
C ( z ) = kc
z −1 z
The kc parameter is calculated to set the 60° phase margin.
First assume kc =1 :
» kc=1;
» Cz=((z-0.9048)*(z-0.8187))/((z-1)*z);
or with the poles of the discrete plant transfer function
» Cz=((z-pd(1))*(z- pd(2)))/((z-1)*z)
Calculate the discrete-time loop transfer function L ( z ) = C ( z ) ⋅ P ( z ) .
» Lz=Cz*Pz;
» Lz=minreal(Lz, 0.001); % cancel the zero-pole pairs
The kc parameter can be calculated by the margin command or read from a table.
a/ Use the margin command. The bode command tests the Ts sampling time to see if the system continuous or
discrete. If Ts = 0 the system is considered to be a continuous-time system, if Ts > 0 positive than the system is
considered to be a discrete-time system.
» [mag,phase,w]=bode(Lz);
» kc=margin(mag,phase-60,w);
b/ Use a table
» [numz,denz]=tfdata(Lz,’v’);
» [mag,phase,w]=dbode(numz,denz,Ts);
» T=[mag, phase, w] % setting up a table
T=
0.0781 -114.8936 0.2200
0.0697 -117.8611 0.2462
0.0622 -121.1822 0.2756
0.0555 -124.8991 0.3084
0.0495 -129.0589 0.3452
It is seen that the phase shift of about -120° (required by the specification of PM=60°) is achieved at a magnitude of
0.062. Consequently, gain by 1/0.0622 defines the proper gain kc as follows:
» kc=1/0.0622
The gain of the discrete-time controller is kc = 16 . Notice that the margin has to forms; its input can be a transfer
function or the bode amplitude and phase values.

Verify now the system behaviour. Check the phase margin by the margin command.
» Cz=kc*((z-pd(1))*(z- pd(2)))/((z-1)*z);
» Lz=Cz*Pz;
» Lz=minreal(Lz);
» margin(Lz);
The closed loop performance can be investigated by a Simulink model.
» simulink % starts SIMULINK

Create a new file and copy the various blocks. The parameters of the block should be set to the required value.
SIMULINK uses the variables defined in the MATLAB workspace.
C(z), P1(s): Control System Toolbox –>LTI system :Cz, P1s
Sum: Simulink–>Math–>Sum: +-
Dead time, delay: Simulink–>Continuous–>Transport Delay: 1
Step input: Simulink–>Sources–>Step, Change the Step time parameter to zero
Zero-Order-Hold: Simulink–>Discrete–> Zero-Order-Hold, Sampling time: Ts
Scope: Simulink–>Sinks–>Scope
The scope blocks can also be used to transfer the results of the simulation to the Matlab workspace. Change the
parameters in the Scope graphic window ‘properties’ menu.

51
Discrete PID Control .

Data history: Save data to workspace


Variable name: My for ScopeY and Mu for ScopeU
Matrix format
From the Menu change the Simulation–>Parameters–>Stop Time parameter to 20

ScopeU

Cz P1s

Step LTI System Zero-Order LTI System1 Transport ScopeY


Hold Delay

After the simulation the time, the output and control vectors can be gained from the transferred matrix form.
» ty=My(:,1), y=My(:,2)
» tu=Mu(:,1), u=Mu(:,2)
Plot the output, y(t)
» subplot(211), plot(ty,y), grid
and the control signal u(t). The u(t) control signal is the output of the zero order hold.
» subplot(212), stairs(tu,u) , grid
The discrete u[k] signal can also be ploted.
» hold on, plot(tu,u,’*’)
The performance parameters (settling time, overshoot) of the system can be calculated from the y and u vectors.

1.5

0.5

0
0 2 4 6 8 10 12 14 16 18 20

20

15

10

-5
0 2 4 6 8 10 12 14 16 18 20

52
State Space Representation, Observability, Controllability .

12. State Space Representation, Observability, Controllability


Using State Models an { A, b, c, d } set exhibits a Single Input/Single Output system representation with input u ,
output y and state vector x in the State Model:
x = Ax + bu
y = cx + du
It is well known that there is an infinitely large number of Input/Output equivalent State Models associated with a
given transfer function. MATLAB offers one possible way to get a State Model. Start our discussion with a transfer
function of
1
H ( s) =
s + s +1
2

and transform it to a State Model:


» num=1;
» den=[1 1 1];
» [A,b,c,d]=tf2ss(num,den) % transform to state model
» [num1,den1]=ss2tf(A,b,c,d) % just to check, transfer back to transfer function form
To verify the equivalence plot the step responses of the two Input/Output equivalent representations:
» step(num,den);
» step(A,b,c,d);
A different representation can be generated by a coordinate transformation in the state-space. The state variables in
the new coordinate-system can be calculated by a linear transformation (called similarity transformation). In details,
assuming P as a non-singular quadratic transformation matrix and x as the state variable in the new (transformed)
system the similarity transformation can be summarized in the following set of equations:
x = Ax + bu
y = cx + du
where
x = Px => x = P -1 x
A = PAP -1 , b = Pb, c = cP −1 , d = d
−1
Note that if P is a matrix constructed by the eigenvectors of A as its column vectors, the new form becomes a
diagonal form (also called as parallel realization).

Example:
» A=[-1 –0.5 0.5; 2 –3 0; 2 –1 –2]
» b=[2;3;1]
» c=[0 0 1]
» d=0
» H=ss(A,b,c,d)
Calculate the eigenvectors (V) and eigenvalues (ev).
» [V,ev]=eig(A)
Transfer the system to diagonal form by the eigenvectors of the system. The inverse of the transformation matrix is V.
» Pi=V; P=inv(V)
» Ap=P*a*Pi
» bp=P*b
» cp=c*Pi
» dp=d
Verify that V is diagonal.
The same calculation can be done easier by
» [Ap,bp,cp,dp]=ss2ss(A,b,c,d,inv(V))
or just leaving the transformation matrix hidden:
» [Ap,bp,cp,dp]=canon(A,b,c,d,’modal’)

53
State Space Representation, Observability, Controllability .

 x1   −1 0 0   x1   1.73 
 x  =  0 −3 0  ⋅  x  +  0  ⋅ u
 2    2  
 x3   0 0 −2   x3   −2.23
 x1 
y = [ 0.57 −0.707 0] ⋅  x2  + 0 ⋅ u
 
 x3 
Draw the block diagram of the system. This is a parallel representation of the system. Notice that the gain by
b1 ⋅ c1 = 1.73 ⋅ 0.57 = 1 . It is clear that any b1 ⋅ c1 = 1 gives identical I/O equivalent state-space representations.
x1 x1
1.73
∫ 0.57

-1

u x2 x2 y
∫ -0.707

-3

x3 x3
-2.23

-2

The above parallel structure allows us to directly read the observability and controllability conditions. Namely x3 is
not observable and x2 is not controllable.
The observability and controllability of the system can be determined without converting a system to a diagonal
representation. The controllability matrix of the system is Co = b Ab ... An −1 b  and can be obtained by
» Co=ctrb(a,b)
or using an LTI structure:
» Co =ctrb(H)
Then controllability can be judged by checking if Co has full rank:
» rank(Co)
The system is controllable if the rank of the controllability matrix is equal to the number of state variables (n). In our
case the system is not controllable since rank (Co ) = 2 < n .
 c 
 cA 
The observability matrix of a system is Ob =  
 ... 
 n −1 
 cA 
by MATLAB:
» Ob=obsv(A,c)
» Ob =obsv(H)
» rank(Ob)
The system is observable if the rank of the observability matrix is equal to n. The system is not observable since
rank (Ob ) = 2 < n .

54
State Space Representation, Observability, Controllability .

The output controllability matrix of the system is coy = c * Co . The system is output controllable if the rank (coy )
equals to the number of outputs.
» rank(c*Co)
Calculate the transfer function of the system:
» [num,den]=ss2tf(A,b,c,d)
Calculate the transfer function in zero-pole form.:
» Hzpk=zpk(H)
Observe, that two zeros cancel two poles:
» Hzpk=minreal(Hzpk)
Information on system states is lost in the transfer function form. The fact of zero-pole cancellation explains why
controllability and/or observability are lost..
The parallel representation can be created by partial fraction expansion, too:
» [num,den]=tfdata (H,’v’)
» [r,p,k]=residue(num,den)
0 0 1
H ( s) = + +
s + 3 s + 2 s +1
1
Remark: it is seen that the transfer function describes only a subsystem of the complete system represented by
s +1
the State Model. This particular subsystem, however, is controllable and observable.

55
State Feedback Control .

13. State Feedback Control


A linear time invariant system can be described by its state matrices: A,B,C,D.
x = Ax + Bu
y = Cx + Du
State feedback modifies the dynamic and static behaviour of the system in terms of the relation between the
reference signal and the output signal:

r u x x y
G B
∫ C
-
A

K
Using the K matrix in the state feedback path and the G matrix in the feed-forward path the state equation of the
closed-loop system is
x = (A − BK ) x + GBr
.
y = (C − DK ) x + DGr
Introducing A F = A − BK , B F = GB, C F = C − DK , D F = DG we have
x = A F x + B F r
y = CF x + DFr
for the closed-loop system. The characteristic equation of the closed-loop system is:
det( sI − A F ) = det(sI - A + BK ) = 0 .
The state feedback control system can be designed in the following steps:
- assign the poles for the closed-loop system
- the feedback gain K is calculated with the Ackermann formula (use Matlab acker command)
- calculate the static gain G to achieve the required steady-state behaviour.

Example. Consider the following system, given by its transfer function:


6
H (s) =
( s + 1)( s + 2)( s + 3)
Give the poles of the system and transform it to state space representation.
» po=[-1;-2;-3];
» [A,b,c,d]=tf2ss(6,poly(po))
 −6 − 11 − 6  1 
A =  1 0 0  , B =  0  , C = [ 0
 0 6], D = 0
 0 1 0   0 
Assign the poles for the closed-loop system
» pc=[-6;-3+i*4;-3-i*4];
and find the feedback gain vector to realize the required pole-placement from p o = − 1 [ −2 − 3 ] to
pc = [ − 6 −3 + 4 j −3 − 4 j ] :

56
State Feedback Control .

» k=acker(A,b,pc)
k = [6 50 144 ]
» t=0:0.1:6;
» subplot(211),step(A,b,c,d,1,t);
» subplot(212),step(A-b*k,b,c,d,1,t);

It is seen that the transient response has considerably been improved, though the steady-state behaviour of the
closed-loop system is not acceptable. One way to achieve zero steady-state error is to apply a correction gain for the
reference signal. The value of that gain is equal to the ratio of the open-loop DC gain over the gain of the closed-loop
DC gain:
» GainOL=dcgain(A,b,c,d)
GainOL = 1
» GainCL=dcgain(A-b*k,b,c,d)
GainCL = 0.04
» G= GainOL/ GainCL
» subplot(212),step(A-b*k,G*b,c,d)
The figure below shows the plant output and the outputs of the control system without and with gain compensation.

0.8
plant output
0.6

0.4

0.2

0
0 1 2 3 4 5 6

1.5

output with gain compensation


1

0.5

output without gain compensation


0
0 1 2 3 4 5 6

Due to the change in the location of the poles the closed-loop system became faster, as expected .
Insert an integrator into the loop
The state feedback itself adds a similar effect to the closed-loop as a serially connected PD compensator designed in
the frequency domain. The static gain of the system, however, is set by the G parameter, which is out of the loop,
i.e. this setting is sensitive with respect to the changes or uncertainties in the process parameters. Also, in case of
disturbances the static gain will not change. Consequently, to eliminate the static error an integrator should be
inserted into the loop. The modified control structure is presented in the figure below.

57
State Feedback Control .

r xi xi u x x y
∫ ki B
∫ C
- -
A

The number of the state variables has been increased by a new state variable.
Assuming a single-input single-output system with D = 0 the state model for the closed-loop system can be written
as
 x   A - bk bki   x   0 
x e =   =  + r = A cxe + bcr ,
 xi   -c 0   xi   1 
where A c can further be decomposed with elements of extended dimensions:
 A - bk b ki   A 0  b 
Ac =  =
0   -c

0   0 
[k − ki ] = A e − b ek e
 -c
The poles of the closed-loop system then are determined by
sI − A e + b e k e = 0
Describing the poles for the closed-loop system (i.e. for the characteristic equation above) the Ackermann’s formula
can be applied to find the proper gain k e .

Continuing the example enhance the system by the state variable of the integrating element.
Create the state equation of the enhanced system:

» z=[0;0;0];
» Ae=[A z;c 0]
Ae =
-6 -11 -6 0
1 0 0 0
0 1 0 0
0 0 6 0
» be=[b;0]
be =
1
0
0
0
Prescribing the closed-loop poles one more pole is to be given:
» pce=[-9 -6 -3+i*4 -3-i*4];
» ke=acker(Ae,be,pce)
ke = [15 158 693 225 ]
The first 3 components of the state feedback vector belong to the original state variables, while the forth is the state
feedback coefficient for the integrator.
» k=ke(1:3)

58
State Feedback Control .

k =
15 158 693
» ki=ke(4)
ki =
225
» Ac=[A-b*k b*ki;-c 0]
Ac =
-21 -169 -699 225
1 0 0 0
0 1 0 0
0 0 -6 0
» bc=[z;1]
bc =
0
0
0
1
» cc=[c 0]
cc =
0 0 6 0
» dc=0;
» subplot(111),step(Ac,bc,cc,dc,1,t)

1.4

1.2

0.8

0.6

0.4

0.2

0
0 1 2 3 4 5 6

It is seen that both the static and dynamic behaviour is good.

State estimation
In most cases only the output of the system is measurable and the state variables are not available. In this case the
state variables have to be estimated. For state estimation the Kalman filter can be used. Its structure is shown in the
figure below. The model of the system is built and connected parallel to the system. The input for the system and its
model is the same. The difference of the outputs of the system and the model is fed back to the state derivatives of
the model via the gain vector L. The estimation circuit can be built easily in SIMULINK.

The poles of the estimation circuit can be prescribed and the gain L ensuring these closed-loop poles can be
calculated using the Ackermann formula. The prescribed poles of the estimation circuit have to ensure a faster
behaviour for the estimation circuit than the transient behaviour of the system itself. With this estimation circuit the
estimated state variables approximate the time course of the system state variables. State feedback control can be
realised then from the estimated state variables.

59
State Feedback Control .

System
D

u x x y
B
∫ C

Model

D
x x y
B ∫ 
C -
e

A

60

You might also like