The document discusses optimal control and linear quadratic control (LQR). It introduces LQR as a method for optimal control where the system dynamics are linear and the cost is quadratic. It explains that LQR provides an algorithm to optimize the controller by finding the feedback gain matrix K. The document provides steps to solve LQR problems using MATLAB and finds the optimal regulator to minimize a cost function for a sample linear time-invariant system.
2. INTRODUCTION
• The theory of optimal control is concerned with operating
a dynamic system at a minimum cost.
• The case where the system dynamics are described by a set of linear
differential equations and the cost is described by a quadratic
function is called the LQ problem.
• The Lqr algorithm reduces the amount of work done by the control
systems engineer to optimize the controller.
• However, the engineer still needs to specify the cost function
parameters and compare the results with the specified design goals.
P R E P A R E D B Y H A W I . A 2
3. OPTIMAL REGULATOR
SISO system
Quadratic function
Find a control law u= -Kx for a dynamical system in terms of
minimizing the objective function.
where Q and R are user-defined weighting matrix and
coefficient, respectively.
P R E P A R E D B Y H A W I . A 3
4. OPTIMAL REGULATOR
Feedback control to minimize J
where K is given by
P is the solution that satisfies the following Riccatti
equation:
If A is a real matrix and Q is a real symmetric matrix,
then P is a real symmetric matrix and PD.
P R E P A R E D B Y H A W I . A 4
5. OPTIMAL REGULATOR
Step 2: Find the feedback gain matrix K.
Step 1: Find the solution P of the Riccatti equation
from
A, B, Q, and R
% MATLAB command
[K,P,E]= lqr(A,B,Q,R) % E are the eigenvalues of
(A-BK)
P R E P A R E D B Y H A W I . A 5
6. For example, consider a LTI system
Find the optimal regulator to mi
nimize J
From J
P R E P A R E D B Y H A W I . A 6
7. and substituting this to the Riccati equation gives the
following equation:
P R E P A R E D B Y H A W I . A 7
9. MATLAB PROGRAM
A= [-1 1;0 -1]; B= [0;1]; C= [1 0]; D= 0;
Q= [4 0;0 1]; R= 1;
[K,P,E]= lqr(A,B,Q,R)
AA= A-B*K; BB= [0;0]; CC= eye(2); DD= D;
t= 0:0.02:5;
u= zeros(size(t));
x0=[1;1];
y= lsim(AA,BB,CC,DD,u,t,x0);
plot(t,y(:,1),t,y(:,2))
P R E P A R E D B Y H A W I . A 9
11. TEXTBOOK(S) AND/OR OTHER
REQUIRED MATERIAL:
1. C.B Moler, Numerical Computing with MATLAB, 2nd Ed, SIM,
2008
2. D.B O’Leary, Scientific computing with Case Studies, 1st Ed,
SIM, 2008
3. A.Quarteroni, F. Saleri, Scientific Computing with Matlab and
Octave, 2nd Ed, Springer, 2006
4. https://ctms.engin.umich.edu/CTMS/index.php?exampl
e=Introduction§ion=ControlStateSpace
P R E P A R E D B Y H A W I . A 11
12. INSTALLATION
Select the tool boxes that you need
• e.g. Matlab, curve fitting, optimization, statistics,
symbolic math, control systems, etc.
P R E P A R E D B Y H A W I . A 12
13. INTRODUCTION TO MATLAB
• The emphasis here is “learning by doing". Therefore, the
best way to learn is by trying it yourself. Working through
the examples will give you a feel for the way that
MATLAB operates.
• MATLAB was written originally to provide easy access
to matrix software developed by the LINPACK (linear
system package) and EISPACK (Eigen system package
projects.
P R E P A R E D B Y H A W I . A 13
14. History: A numerical analyst called Cleve Moler
wrote the first version of Matlab in the 1970s. It has
since changed into a successful commercial
software package. The software package has been
commercially available since 1984 and is now
considered as a standard tool at most universities and
industries worldwide.
• A MathWorks Web site, MATLAB Tutorials and
Learning Resources, offers a number of
introductory videos and a PDF manual entitled
Getting Started with MATLAB.
P R E P A R E D B Y H A W I . A 14
15. INTRODUCTION TO MATLAB
• The name MATLAB stands for MATrix LABoratory.
• MATLAB is a high-performance language for
technical computing.
• It integrates computation, visualization, and
programming environment.
• It allows you to solve many technical computing
problems, especially those with matrix and vector
formulas, in a fraction of the time it would take to
write a program in a scalar non-interactive language
such as C or Fortran.
P R E P A R E D B Y H A W I . A 15
16. MATLAB is matrix-oriented, so what would take several
statements in C or Fortran can usually be accomplished in just a
few lines using MATLAB's built-in matrix and vector operations
FORTRAN:
real*8 A(10,10), B(10,10), C(10,10)
do i=1,10
do j=1,10
C(i,j) = A(i,j) + B(i,j)
10 continue
20 continue
MATLAB:
C = A + B
P R E P A R E D B Y H A W I . A 16
17. • It has powerful built-in routines that enable a
very wide variety of computations. It also has
easy to use graphics commands that make the
visualization of results immediately available.
This allows you to spend more time thinking,
and encourages you to experiment.
• Matlab makes use of highly respected
algorithms and hence you can be confident
about your results. Powerful operations can be
performed using just one or two commands.
• You can build up your own set of functions for
a particular application.
P R E P A R E D B Y H A W I . A 17
18. MATLAB has many toolboxes:
Control toolbox is one of the important toolbox in
MATLAB.
• RLC Circuit Response,
• Gain and Phase Margins,
• PID and ...
System Identification
Fuzzy logic
P R E P A R E D B Y H A W I . A 18
19. Starting MATLAB
You can enter MATLAB by double-clicking on the
MATLAB shortcut icon on your Windows desktop. When
you start MATLAB, a special window called the
MATLAB desktop appears. The desktop is a window that
contains other windows. The major tools within or
accessible from the desktop are:
• The Command Window
• The Command History
• The Workspace
• The Current Directory
• The Help Browser
• The Start button
P R E P A R E D B Y H A W I . A 19
22. • Command Prompt : MATLAB commands
are entered here.
• Workspace : Displays any variables created
(Matrices, Vectors, Singles, etc.)
• Command History : Lists all commands
previously entered.
Double clicking on a variable in the
Workspace will open an Array Editor. This
will give you an Excel-like view of your
data.
P R E P A R E D B Y H A W I . A 22
24. WORKSPACE
• Matlab remembers old commands
• And variables as well
• Each Function maintains its own scope
• The keyword clear removes all variables from
workspace
• The keyword who lists the variables
• You can use the command clear all to delete all
the variables present in the workspace
• You can also clear specific variables using:
>> clear Variable_Name
P R E P A R E D B Y H A W I . A 24
30. HELP
• If you want a more comprehensive introduction, there
are many resources available. You can select the Help
tab in the toolstrip atop the Matlab command window,
then select Getting Started.
• Extensive documentation is available, either via the
command line by using the 'help topic‘ command.
• We recommend starting with the command demo
(a link may also be provided on the top line of the
command window).
P R E P A R E D B Y H A W I . A 30
31. This brings up a separate window which gives
access to a short video entitled ”getting Started"
that describes the purpose of the various panes
in the main Matlab window.
Help is available from the command line prompt. Type
help help for help" (which gives a brief synopsis of the
help system), help for a list of topics.
Then to obtain help on “Elementary math functions",
for instance, type
>> help elfun
P R E P A R E D B Y H A W I . A 31
32. • Clicking on a key word, for example sin will provide
further information together with a link to doc sin
which provides the most extensive documentation on
a keyword along with examples of its use.
• Another way to get help is to use the lookfor
command. The lookfor command divers from the
help command. The help command searches for an
exact function name match, while the lookfor
command searches the quick summary information in
each function for a match.
P R E P A R E D B Y H A W I . A 32
33. • For example, suppose that we were
looking for a function to take the inverse of
a matrix. Since MATLAB does not have a
function named inverse, the command help
inverse will produce nothing.
• On the other hand, the command lookfor
inverse will produce detailed information,
which includes the function of interest, inv.
>> lookfor inverse
>> help ‘what’
>> lookfor ‘something’
>>help functionname
>>lookfor keyword
P R E P A R E D B Y H A W I . A 33
34. Some facts for a first impression:
• Everything in MATLAB is a matrix !
• MATLAB is an interpreted language, no compilation needed
(but possible)
• MATLAB does not need any variable declarations, no
dimension statements, has no packaging, no storage allocation,
no pointers
• Programs can be run step by step, with full access to all
variables, functions etc.
P R E P A R E D B Y H A W I . A 34
35. VARIABLES, SCRIPTS AND OPERATIONS
Creating MATLAB variables:
MATLAB variables are created with an assignment
statement. The syntax of variable assignment is
variable name = a value (or an expression)
For example,
>> x = expression
where expression is a combination of numerical values,
mathematical operators, variables, and function calls.
P R E P A R E D B Y H A W I . A 35
36. Variables
• As we discussed, MATLAB stands for 'MATrix
LABoratory'. This title is appropriate because the
structure for the storage of all data in MATLAB is a
matrix.
• MATLAB stores these as matrix variables with 1 row
and 1 column. These variables show in your workspace
is shown as 1x1 matrices.
P R E P A R E D B Y H A W I . A 36
37. • To create a (scalar) variable in MATLAB simply
enter a valid variable name at the command line
and assign it a value using =.
• Once a variable has been assigned a value, it is
added to the MATLAB Workspace and will be
displayed in the Workspace window.
• No need for types. i.e., int a;
double b;
float c;
P R E P A R E D B Y H A W I . A 37
38. For example, after entering:
>> height = 5
height =
5
>> width = 4
width =
4
the Workspace window will contain both height and width as
scalars:
P R E P A R E D B Y H A W I . A 38
39. We should NOT use length as a variable name as this is a
built-in MATLAB function.
Rules on Variable Names
There are a number of rules as to the variable names
you can use:
1. Must start with a letter, followed by letters, digits, or
underscores. X12, rate_const, Flow_rate are all
acceptable variable names but not vector-A (since - is a
reserved character);
2. Are case sensitive, e.g., FLOW, flow, Flow, FlOw
are all different variables;
3. Must not be longer than 31 characters;
4. Must not be a reserved word (i.e., special names
which are part of MATLAB language);
5. Must not contain punctuation characters;
P R E P A R E D B Y H A W I . A 39
40. Some MATLAB Workspace Functions
• You can see the variables in your workspace by looking at the
Workspace window or by using the whos command:
>> whos
Name Size Bytes Class Attributes
area 1x1 8 double
height 1x1 8 double
Width 1x1 8 double
• If you want to remove a variable from the MATLAB you can use
the clear command, e.g.,
>> clear width
removes width from the workspace.
• If you want to get rid of all the variables in your workspace just
type:
>> clear
P R E P A R E D B Y H A W I . A 40
41. • Suppose you want to repeat or edit an earlier
command. If you dislike typing it all back in, then
there is good news for you: MATLAB lets you search
through your previous commands using the up-arrow
and down-arrow keys. This is a very convenient
facility, which can save a considerable amount of time.
Example. Clear the height of the rectangle in the
earlier example:
>> clear height
and look at the Workspace window:
P R E P A R E D B Y H A W I . A 41
42. If you want it back! Press the up-arrow key until the
earlier command:
>> height = 5
appears in the Command Window. Now press enter
P R E P A R E D B Y H A W I . A 42
43. • MATLAB suppresses the output of a command if you
finish the command with a semi-colon - ;.
Example:
>> height = 7;
If you look at height in your Workspace window you
will see its value has changed to 7:
P R E P A R E D B Y H A W I . A 43
44. SAVING YOUR WORK - SCRIPT FILES
• So far all your work has been at the command line. This
is useful for quick calculations. For calculations that are
more complex, or that you are likely to perform often it
is much more useful to save your work in script files.
• To create a new script file (also called an M-file)
choose File---New----M-File from the MATLAB
menu bar.
P R E P A R E D B Y H A W I . A 44
45. Click to create a
new M-File
• Extension “.m”
• A text file containing script or function or program to run
Use of M-File
P R E P A R E D B Y H A W I . A 45
46. • In the script file window enter:
height = 5
width = 4
area = height * width
• Save your script file by choosing File----Save
from the menu in the script file editor.
• Give your script file a name, such as
area_calcul.m. The .m extension is used to
denote MATLAB script files. The file will be
saved in the working directory
Filenames cannot include spaces. They also cannot start
with a number, include punctuation characters (other than
the underscore) or use a reserved MATLAB command.
P R E P A R E D B Y H A W I . A 46
47. at the command prompt enter:
>> area_calcul
You will see the height, width and area variables appear
with their values.
P R E P A R E D B Y H A W I . A 47
48. Mathematical Operators:
Add: +
Subtract: -
Divide: ./
Multiply: .*
Power: .^ (e.g. .^2 means squared)
You can use round brackets to specify the order in which
operations will be performed
P R E P A R E D B Y H A W I . A 48
49. Scripts and Functions
• There are two kinds of M-files:
- Scripts, which do not accept input arguments or
return output arguments. They operate on data in
the workspace. Any variables that they create
remain in the workspace, to be used in subsequent
computations
- Functions, which can accept input arguments and
return output arguments. Internal variables are local
to the function.
P R E P A R E D B Y H A W I . A 49
51. Script or function
• Scripts are m-files containing MATLAB
statements
• Functions are like any other m-file, but they
accept arguments
• It is always recommended to name function file
the same as the function name
P R E P A R E D B Y H A W I . A 51
52. • Global Variables
• If you want more than one function to share a
single copy of a variable, simply declare the variable
as global in all the functions. The global declaration
must occur before the variable is actually used in a
function.
Example: function h = falling(t)
global GRAVITY
h = 1/2*GRAVITY*t.^2;
P R E P A R E D B Y H A W I . A 52
53. • M-Files
You can create your own programs using M-files,
which are plain text files containing MATLAB code.
Use the MATLAB Editor or another text editor to
create a file containing the same statements you
would type at the MATLAB command line. Save the
file under a name that ends in .m
P R E P A R E D B Y H A W I . A 53
54. Writing User Defined Functions
• Functions are m-files which can be executed by
specifying some inputs and supply some desired
outputs.
• The code telling the Matlab that an m-file is actually a
function is
• You should write this command at the beginning of
the m-file and you should save the m-file with a file
name same as the function name
function out1=functionname(in1)
function out1=functionname(in1,in2,in3)
function [out1,out2]=functionname(in1,in2)
P R E P A R E D B Y H A W I . A 54
55. • Examples
– Write a function : out=squarer (A, ind)
• Which takes the square of the input matrix if the
input indicator is equal to 1
• And takes the element by element square of the
input matrix if the input indicator is equal to 2
Same Name
P R E P A R E D B Y H A W I . A 55
56.
57. BUILDING MODELS FOR LTI SYSTEM
Control System Toolbox supports continuous time
models and discrete time models of the following
types*:
Transfer Function
Zero-pole-gain
State Space
* Material taken from http://techteach.no/publications/control_system_toolbox/#c1
57
P R E P A R E D B Y H A W I . A
58. CONTINUOUS TIME TRANSFER
FUNCTION(1)
Function: Use tf function create transfer
function of following form:
Example
2
3
1
2
)
( 2
s
s
s
s
H
>>num = [2 1];
>>den = [1 3 2];
>>H=tf(num,den)
Transfer function:
2 s + 1
-------------
s^2 + 3 s + 2
Matlab Output
58
P R E P A R E D B Y H A W I . A
59. CONTINUOUS TIME TRANSFER
FUNCTION(2)
Include delay to continuous time Transfer
Function
Example
2
3
1
2
)
( 2
2
s
s
s
e
s
H s
Transfer function:
2 s + 1
exp(-2*s) * -------------
s^2 + 3 s + 2
>>num = [2 1];
>>den = [1 3 2];
>>H=tf(num,den,’inputdelay’,2)
Matlab Output
59
P R E P A R E D B Y H A W I . A
60. CONTINUOUS TIME TRANSFER
FUNCTION(3)
Function: Use zpk function to create transfer
function of following form:
Example
2
1
5
.
0
2
2
3
1
2
)
( 2
s
s
s
s
s
s
s
H
>>num = [-0.5];
>>den = [-1 -2];
>>k = 2;
>>H=zpk(num,den,k)
Zero/pole/gain:
2 (s+0.5)
-----------
(s+1) (s+2)
Matlab Output
60
P R E P A R E D B Y H A W I . A
61. CONTINUOUS TIME STATE SPACE
MODELS(1)
State Space Model for dynamic system
Du
Cx
y
Bu
Ax
x
Matrices: A is state matrix; B is input matrix; C is
output matrix; and D is direct
transmission matrix
Vectors: x is state vector; u is input vector; and y is
output vector
Note: Only apply to system that is linear and time invariant
61
P R E P A R E D B Y H A W I . A
62. CONTINUOUS TIME STATE SPACE
MODELS(2)
Function: Use ss function creates state
space models. For example:
0
1
0
3
0
2
5
1
0
2
1
D
C
B
A
x
x
x
>>A = [0 1;-5 -2];
>>B = [0;3];
>>C = [0 1];
>>D = [0];
>>sys=ss(A,B,C,D)
a =
x1 x2
x1 0 1
x2 -5 -2
Matlab Output
b =
u1
x1 0
x2 3
c =
x1 x2
y1 0 1
d =
u1
y1 0 62
P R E P A R E D B Y H A W I . A
63. CONVERSION BETWEEN DIFFERENT
MODELS
Converting From Converting to Matlab function
Transfer Function Zero-pole-gain [z,p,k]=tf2zp(num,den)
Transfer Function State Space [A,B,C,D]=tf2ss(num,den)
Zero-pole-gain Transfer Function [num,den]=zp2tf(z,p,k)
Zero-pole-gain State Space [A,B,C,D]=zp2ss(z,p,k)
State Space Transfer Function [num,den]=ss2tf(A,B,C,D)
State Space Zero-pole-gain [z,p,k]=ss2zp(A,B,C,D)
63
P R E P A R E D B Y H A W I . A
64. DISCRETE TIME TRANSFER FUNCTION(1)
Function: Use tf function create transfer
function of following form:
Example: with sampling time
0.4
2
3
1
2
)
( 2
z
z
z
z
H
>>num = [2 1];
>>den = [1 3 2];
>>Ts=0.4;
>>H=tf(num,den,Ts)
Transfer function:
2 z + 1
-------------
z^2 + 3 z + 2
Sampling time: 0.4
Matlab Output
64
P R E P A R E D B Y H A W I . A
65. DISCRETE TIME TRANSFER FUNCTION(2)
Function: Use zpk function to create transfer
function of following form:
Example: with sampling time 0.4
2
1
5
.
0
2
)
(
z
z
z
z
H
>>num = [-0.5];
>>den = [-1 -2];
>>k = 2;
>>Ts=0.4;
>>H=zpk(num,den,k,Ts)
Zero/pole/gain:
2 (z+0.5)
-----------
(z+1) (z+2)
Sampling time: 0.4
Matlab Output
65
P R E P A R E D B Y H A W I . A
66. DISCRETE TIME STATE SPACE MODELS(1)
State Space Model for dynamic system
]
[
]
[
]
[
]
[
]
[
]
1
[
n
n
n
n
n
n
Du
Cx
y
Bu
Ax
x
Matrices: A is state matrix; B is input matrix; C is
output matrix; and D is direct
transmission matrix
Vectors: x is state vector; u is input vector; and y is
output vector
n is the discrete-time or time-index
Note: Only apply to system that is linear and time invariant 66
P R E P A R E D B Y H A W I . A
67. DISCRETE TIME STATE SPACE MODELS(2)
Function: Use ss function creates state space
models. For example:
0
1
0
3
0
2
5
1
0
]
[
]
[
]
[
2
1
D
C
B
A
x
n
x
n
x
n
>>A = [0 1;-5 -2];
>>B = [0;3];
>>C = [0 1];
>>D = [0];
>>Ts= [0.4];
>>sys=ss(A,B,C,D,Ts)
Transfer function:
2 z + 1
-------------
z^2 + 3 z + 2
Sampling time: 0.4
Matlab Output
a =
x1 x2
x1 0 1
x2 -5 -2
Matlab Output
b =
u1
x1 0
x2 3
c =
x1 x2
y1 0 1
d =
u1
y1 0
Sampling time: 0.4 67
P R E P A R E D B Y H A W I . A
68. COMBINING MODELS
A model can be thought of as a block with
inputs and outputs (block diagram) and
containing a transfer function or a state-
space model inside it
A symbol for the mathematical operations on
the input signal to the block that produces
the output
Transfer
Function
G(s)
Input Output
Elements of a Block Diagram
68
P R E P A R E D B Y H A W I . A
69. COMBINING MODELS
The Following Matlab functions can be used
to perform basic block diagram
manipulation
Combination Matlab Command
sys = series(G1,G2)
sys = parallel(G1,G2)
sys = feedback(G1,G2)
G1(s) G2(s)
+
G1(s)
G2(s)
+
+
G1(s)
-
G2(s)
69
P R E P A R E D B Y H A W I . A
70. BASIC ARITHMETIC OPERATIONS OF MODELS
Arithmetic Operations Matlab Code
Addition sys = G1+G2;
Multiplication
sys = G1*G2;
Inversion
sys = inv(G1);
70
P R E P A R E D B Y H A W I . A
71. TRANSIENT RESPONSE ANALYSIS
• Transient response refers to the process generated in
going from the initial state to the final state
• Transient responses are used to investigate the time
domain characteristics of dynamic systems
• Common responses: step response, impulse response,
and ramp response
71
P R E P A R E D B Y H A W I . A
72. TRANSIENT RESPONSE ANALYSIS
Unit step response of the transfer function
system
Consider the system:
25
4
25
2
s
s
s
H
%*****Numerator & Denominator of H(s)
>>num = [0 0 25];den = [1 4 25];
%*****Specify the computing time
>>t=0:0.1:7;
>>step(num,den,t)
%*****Add grid & title of plot
>>grid
>>title(‘Unit Step Response of H(s)’) 72
P R E P A R E D B Y H A W I . A
73. TRANSIENT RESPONSE ANALYSIS
Unit step response of H(s)
Unit Step Response of H(s)
Time (sec)
Amplitude
0 1 2 3 4 5 6 7
0
0.2
0.4
0.6
0.8
1
1.2
1.4
73
P R E P A R E D B Y H A W I . A
74. TRANSIENT RESPONSE ANALYSIS
Alternative way to generate Unit step response of the
transfer function, H(s)
If step input is , then step response is
generated with the following command:
%*****Numerator & Denominator of H(s)
>>num = [0 0 25];den = [1 4 25];
%*****Create Model
>>H=tf(num,den);
>>step(H)
>>step(10*H)
)
(
10 t
u
74
P R E P A R E D B Y H A W I . A
75. TRANSIENT RESPONSE ANALYSIS
Impulse response of the transfer function
system
Consider the system:
25
4
25
2
s
s
s
H
%*****Numerator & Denominator of H(s)
>>num = [0 0 25];den = [1 4 25];
%*****Specify the computing time
>>t=0:0.1:7;
>>impulse(num,den,t)
%*****Add grid & title of plot
>>grid
>>title(‘Impulse Response of H(s)’) 75
P R E P A R E D B Y H A W I . A
76. TRANSIENT RESPONSE ANALYSIS
Impulse response of H(s)
Impulse Response of H(s)
Time (sec)
Amplitude
0 1 2 3 4 5 6 7
-1
-0.5
0
0.5
1
1.5
2
2.5
3
76
P R E P A R E D B Y H A W I . A
77. TRANSIENT RESPONSE ANALYSIS
Ramp response of the transfer function
system
There’s no ramp function in Matlab
To obtain ramp response of H(s), divide H(s)
by “s” and use step function
Consider the system:
For unit-ramp input, . Hence
25
4
25
2
s
s
s
H
2
1
)
(
s
s
U
25
4
25
1
25
4
25
1
2
2
2
s
s
s
s
s
s
s
s
Y
Indicate Step response
NEW H(s)
77
P R E P A R E D B Y H A W I . A
78. TRANSIENT RESPONSE ANALYSIS
Example: Matlab code for Unit Ramp Response
%*****Numerator & Denominator of NEW H(s)
>>num = [0 0 0 25];den = [1 4 25 0];
%*****Specify the computing time
>>t=0:0.1:7;
>>y=step(num,den,t);
%*****Plot input & the ramp response curve
>>plot(t,y,’.’,t,t,’b-’)
%*****Add grid & title of plot
>>grid
>>title(‘Unit Ramp Response Curve of H(s)’)
78
P R E P A R E D B Y H A W I . A
79. TRANSIENT RESPONSE ANALYSIS
Unit Ramp response of H(s)
0 1 2 3 4 5 6 7
0
1
2
3
4
5
6
7
Unit Ramp Response Curve of H(s)
79
P R E P A R E D B Y H A W I . A
80. FREQUENCY RESPONSE ANALYSIS
• For Transient response analysis - hard to
determine accurate model (due to noise or
limited input signal size)
• Alternative: Use frequency response approach
to characterize how the system behaves in the
frequency domain
• Can adjust the frequency response
characteristic of the system by tuning relevant
parameters (design criteria) to obtain
acceptable transient response characteristics of
the system
80
P R E P A R E D B Y H A W I . A
81. FREQUENCY RESPONSE ANALYSIS
Bode Diagram Representation of Frequency
Response
Consists of two graphs:
Log-magnitude plot of the transfer function
Phase-angle plot (degree) of the transfer function
Matlab function is known as ‘bode’
%*****Numerator & Denominator of H(s)
>>num = [0 0 25];den = [1 4 25];
%*****Use ‘bode’ function
>>bode(num,den)
%*****Add title of plot
>>title(‘Bode plot of H(s)’)
81
P R E P A R E D B Y H A W I . A
82. FREQUENCY RESPONSE ANALYSIS
Example: Bode Diagram for
Bode plot of H(s)
Frequency (rad/sec)
Phase
(deg)
Magnitude
(dB)
-60
-50
-40
-30
-20
-10
0
10
20
10
0
10
1
10
2
-180
-135
-90
-45
0
25
4
25
2
s
s
s
H
Bode magnitude plot
Bode phase plot
82
P R E P A R E D B Y H A W I . A
83. STABILITY ANALYSIS BASED ON
FREQUENCY RESPONSE
• Stability analysis can also be performed using a
Nyquist plot
• From Nyquist plot – determine if system is stable and
also the degree of stability of a system
• Using the information to determine how stability may
be improved
• Stability is determined based on the Nyquist Stability
Criterion
83
P R E P A R E D B Y H A W I . A
84. STABILITY ANALYSIS BASED ON
FREQUENCY RESPONSE
Example: Matlab code to draw a Nyquist
Plot
Consider the system
1
8
.
0
1
2
s
s
s
H
%*****Numerator & Denominator of H(s)
>>num = [0 0 1];
>>den = [1 0.8 1];
%*****Draw Nyquist Plot
>>nyquist(num,den)
%*****Add grid & title of plot
>>grid
>>title(‘Nyquist Plot of H(s)’) 84
P R E P A R E D B Y H A W I . A
85. STABILITY ANALYSIS BASED ON
FREQUENCY RESPONSE
The Nyquist Plot for
Nyquist plot of H(s)
Real Axis
Imaginary
Axis
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
-1
-0.5
0
0.5
1
0 dB
-20 dB
-10 dB
-6 dB
-4 dB
-2 dB
20 dB
10 dB
6 dB
4 dB
2 dB
1
8
.
0
1
2
s
s
s
H
85
P R E P A R E D B Y H A W I . A
86. 86
Extra: partial fraction expansion
num=[2 3 2];
den=[1 3 2];
[r,p,k] = residue(num,den)
r =
-4
1
p =
-2
-1
k =
2
Answer:
)
1
(
1
)
2
(
4
2
)
(
)
(
)
1
(
)
1
(
)
(
)
(
0
s
s
s
n
p
s
n
r
p
s
r
s
k
s
G
2
3
2
3
2
)
( 2
2
s
s
s
s
s
G
Given:
P R E P A R E D B Y H A W I . A
87. %Define the transfer function of a plant
G=tf([4 3],[1 6 5])
%Get data from the transfer function
[n,d]=tfdata(G,'v')
[p,z,k]=zpkdata(G,'v')
[a,b,c,d]=ssdata(G)
%Check the controllability and observability of the
system
ro=rank(obsv(a,c))
rc=rank(ctrb(a,b))
%find the eigenvalues of the system
damp(a)
%multiply the transfer function with another
transfer function
T=series(G,zpk([-1],[-10 -2j +2j],5))
%plot the poles and zeros of the new system
iopzmap(T)
%find the bandwidth of the new
system
wb=bandwidth(T)
%plot the step response
step(T)
%plot the rootlocus
rlocus(T)
%obtain the bode plots
bode(T)
margin(T)
%use the LTI viewer
ltiview({'step';'bode';'nyquist'},T)
%start the SISO tool
sisotool(T)
87
M-File Example
P R E P A R E D B Y H A W I . A
88.
89. GRAPHICS
Basic Plotting
Linetypes, markers, colours, etc
Subplots – creating multiple axes on a single
figure
Annotating figures – titles, labels, legends
Editing figure properties
P R E P A R E D B Y H A W I . A 89
90. BASIC PLOTTING COMMANDS
figure : creates a new figure window
plot(x) : plots line graph of x vs index
number of array
plot(x,y) : plots line graph of x vs y
plot(x,y,'r--')
: plots x vs y with linetype specified in string
: 'r' = red, 'g'=green, etc for a limited set
of basic colours.
P R E P A R E D B Y H A W I . A 90
91. PLOT(X,Y,…PROPERTIES)
Plot(x,y,cml) – plot with a basic colour-
marker-line combination (cml)
‘ro-’, ‘g.-’, ‘cv--‘
Basic colours : r, g, b, k, y, c, m
Basic markers : ‘o’, ‘v’, ‘^’, ’<‘, ’>’, ‘.’, ‘p’, ‘+’,
‘*’, ‘x’, ‘s’, ‘h’, ‘none’
Linetypes : ‘-’, ’--‘, ‘-.’, ‘:’, ‘none’
P R E P A R E D B Y H A W I . A 91
92. More detailed plot options can be specified
through the use of property:value pairs
Plot(x,y,’o-’,’property’,value)
‘property’ is always a string naming the propery, value may
be a number, array, or string, depending on the property
type:
‘color’ : [R G B] - color (american spelling!) is specified with a
[red green blue] value array, each in range 0-1.
[1 0 0] – pure red
[1 0. 0.5 0] – orange
P R E P A R E D B Y H A W I . A 92
93. ‘linewidth’ : specified as a single number. Default = 0.5
‘linestyle’ : any of line type strings - ‘-’,’:’,etc
‘marker’ : any of marker strings – ‘v’, ’o’, etc
‘markersize’ : number, default = 6
‘markeredgecolor’ : color string ‘r’, ‘g’, or [R G B] value for
the line defining edge of marker
‘markerfacecolor’: color string or [R G B] for inside of
marker (can be different from edge)
Can add any number of property:value pairs to a
plot command:
>> Plot(x,y,’prop1’,value1,’prop2’,value2,..)
P R E P A R E D B Y H A W I . A 93
96. PROPERTY EDITOR — AXIS
Tree of objects
Edited objects
Help
for object
P R E P A R E D B Y H A W I . A 96
97. SUBPLOTS
subplot(m,n,p) : create a subplot in an array of axes
>> subplot(2,3,1);
>> subplot(2,3,4);
m
n
P=1 P=2 P=3
P=4
P R E P A R E D B Y H A W I . A 97
98. ADDING TEXT TO FIGURES
Basic axis labels and title can be added via convenient
functions:
>> xlabel('x-axis label text')
>> ylabel('y-axis label text')
>> title('title text')
Legends for line or symbol types are added via the legend
function:
>> legend('line 1 caption','line 2
caption',…)
Legend labels are given in the order lines were plotted
plot(x,y,'LineWidth',2) % plot with line width of 2 points
P R E P A R E D B Y H A W I . A 98
99. % For example
t= 0:0.02:8;
y1= exp(-t).*sin(t); y2= exp(-0.5*t).*cos(t);
plot(t,y1,'k',t,y2,'r:','linewidth',2)
xlabel('t'), ylabel('y1 & y2')
legend('y1','y2','Location','NorthEast')
axis([0 8 -0.5 1])
P R E P A R E D B Y H A W I . A 99
101. % For example
t= linspace(-pi,pi,200)+eps;
x= sin(t); y= cos(t);
z= zeros(size(x));
plot3(x,y,z,'r','linewidth',2)
hold on
z= abs(sin(10*t)./(10*t));
plot3(x,y,z,'b','linewidth',3)
axis([-1.2 1.2 -1.2 1.2 -0.5 2])
axis off, hold off
>> x= 1:3; y= -1:1;
>> [X,Y]=
meshgrid(x,y)
>> Z= X.^2+Y.^2
P R E P A R E D B Y H A W I . A 101
102.
103. SYSTEM PERFORMANCE:
ns= 4;
ds= [1 2 4];
t= 0:0.01:10;
y= step(ns,ds,t);
plot(t,y,'linewidth',
2)
stepinfo(y,t,1)
>>RiseTime: 0.8188
>>SettlingTime: 4.0382
>>SettlingMin: 0.9054
>>SettlingMax: 1.1630
>>Overshoot: 16.3029
>>Undershoot: 0
>>Peak: 1.1630
>>PeakTime: 1.8100
For example, consider a TF
P R E P A R E D B Y H A W I . A 103
104. Controllability and observability
>> A= [0 1;0 0]; B= [1;0]; C= [1 0];
>> U= ctrb(A,B); % Controllability
matrix
>> ru= rank(U) % Rank
>> V= obsv(A,C); % Observability
matrix
>> rv= rank(V) % Rank
P R E P A R E D B Y H A W I . A 104
105. ESTIMATOR/OBSERVER DESIGN
A= [0 1 0;0 -1 1;0 0 0]; B= [0;0;1]; C=[1 0
0];
AA= [A;C];
AA= [AA zeros(4,1)];
BB= [B;0];
PC= [-2+2j, -2-2j, -1, -2];% pole
K= acker(AA,BB,PC)
PO= [-3+2j, -3-2j, -4]; pole of observer
AT= A'; CT= C'; L= acker(AT, CT, PO);
L= L'
P R E P A R E D B Y H A W I . A 105