You are on page 1of 106

OPTIMAL CONTROL

LINEAR QUADRATIC CONTROL (LQR)

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 1
04
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.
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 2
04
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.

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 3
04
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.
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 4
04
OPTIMAL REGULATOR

Step 1: Find the solution P of the Riccatti equation from


A, B, Q, and R

Step 2: Find the feedback gain matrix K.

% MATLAB command
[K,P,E]= lqr(A,B,Q,R) % E are the eigenvalues of (A-
BK)

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 5
04
For example, consider a LTI system

Find the optimal regulator to mi


nimize J

From J
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 6
04
and substituting this to the Riccati equation gives the following
equation:

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 7
04
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 8
04
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))

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 9
04
INTRODUCTION TO
COMPUTING WITH MATLAB

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 10
04
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?
example=Introduction&section=ControlStateSpace

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 11
04
INSTALLATION
Select the tool boxes that you need
• e.g. Matlab, curve fitting, optimization, statistics,
symbolic math, control systems, etc.

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 12
04
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.

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 13
04
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.

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 14
04
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.

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 15
04
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   
 

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 16
04
• 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.
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 17
04
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

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 18
04
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
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 19
04
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 20
04
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 21
04
• 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.

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 22
04
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 23
04
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:


20 23 >> clear Variable_Name
/ 24
/ 06 P R E PA R E D B Y H AW I . A
04
MAIN MATLAB WINDOW

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 25
04
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 26
04
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 27
04
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 28
04
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 29
04
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).

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 30
04
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

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 31
04
• 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.

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 32
04
• 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’ >>help functionname

/ 20
23 >> lookfor ‘something’ >>lookfor keyword
/0
6 P R E PA R E D B Y H AW I . A 33
04
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.

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 34
04
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.

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 35
04
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.

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 36
04
• 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;

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 37
04
For example, after entering:
>> height = 5
height =
5
>> width = 4
width =
4
the Workspace window will contain both height and width as
scalars:

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 38
04
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 / 20
23 are part of MATLAB language);
6 39
P R E PA R E D B Y H AW I . A
5. Must not contain punctuation characters;
04/0
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
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 40
04
• 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:

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 41
04
If you want it back! Press the up-arrow key until the
earlier command:
>> / 20
23 height = 5
6 P R E PA R E D B Y H AW I . A 42
0appears in the Command Window. Now press enter
4/0
• 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:

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 43
04
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.

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 44
04
 Use of M-File

Click to create a
new M-File

• Extension “.m”
• A text file containing script or function or program to run
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 45
04
• 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
/ 20
23 underscore) or use a reserved MATLAB command.
6
/0
P R E PA R E D B Y H AW I . A 46
04
at the command prompt enter:
>> area_calcul
You will see the height, width and area variables appear
with their values.

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 47
04
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

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 48
04
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.

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 49
04
Editor

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 50
04
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

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 51
04
• 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;

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 52
04
• 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

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 53
04
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
function out1=functionname(in1)
function out1=functionname(in1,in2,in3)
function [out1,out2]=functionname(in1,in2)

• You should write this command at the beginning of


the m-file and you should save the m-file with a file
23

4/
06
/ 20 name same as the function name 54
P R E PA R E D B Y H AW I . A
0
• 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

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 55
04
A B
A TL
M
I T H
L W
R O
N T
CO
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

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 57
04
* Material taken from http://techteach.no/publications/control_system_toolbox/#c1
CONTINUOUS TIME TRANSFER
FUNCTION(1)
Function: Use tf function create transfer
function of following form:

Example
2s  1
H (s) 
s 2  3s  2

Matlab Output
>>num = [2 1];
>>den = [1 3 2]; Transfer function:
2 s + 1
>>H=tf(num,den) -------------
s^2 + 3 s + 2
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 58
04
CONTINUOUS TIME TRANSFER
FUNCTION(2)
Include delay to continuous time Transfer
Function
ExampleH ( s)  e 2 s 2 s  1
s 2  3s  2
>>num = [2 1];
>>den = [1 3 2];
>>H=tf(num,den,’inputdelay’,2)

Matlab Output
Transfer function:
2 s + 1
exp(-2*s) * -------------
23 s^2 + 3 s + 2P R E P A R E D
/ 20
/0
6 B Y H AW I . A 59
04
CONTINUOUS TIME TRANSFER
FUNCTION(3)
Function: Use zpk function to create transfer
function of following form:
2s  1 s  0.5
Example H (s)  2 2
s  3s  2 s  1s  2
Matlab Output
>>num = [-0.5];
>>den = [-1 -2]; Zero/pole/gain:
2 (s+0.5)
>>k = 2; -----------
>>H=zpk(num,den,k) (s+1) (s+2)
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 60
04
CONTINUOUS TIME STATE SPACE
MODELS(1)
State Space Model for dynamic system
x  Ax  Bu
y  Cx  Du

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
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 61
04
Note: Only apply to system that is linear and time invariant
CONTINUOUS TIME STATE SPACE
MODELS(2)
Function: Use ss function creates state space
models. For example:
 x1  0 1 0 
x  A  B    C  0 1 D  0
 x2    5  2  3

Matlab Output
>>A = [0 1;-5 -2];
a = b =
>>B = [0;3]; x1 x2 u1
x1 0 1 x1 0
>>C = [0 1]; x2 -5 -2 x2 3
>>D = [0];
c = d =
>>sys=ss(A,B,C,D) x1 x2 u1
23
/ 20 y1 0 P1 Hy1
A W I . A 0 62
6 R E PA R E D BY
4/0
0
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)

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 63
04
DISCRETE TIME TRANSFER FUNCTION(1)

Function: Use tf function create transfer function


of following form:
2z 1
Example: H ( z )  2 with sampling time 0.4
z  3z  2

Matlab Output
>>num = [2 1];
Transfer function:
>>den = [1 3 2]; 2 z + 1
>>Ts=0.4; -------------
z^2 + 3 z + 2
>>H=tf(num,den,Ts)
23
Sampling time: 0.4
/ 20
/0
6 P R E PA R E D B Y H AW I . A 64
04
DISCRETE TIME TRANSFER FUNCTION(2)

Function: Use zpk function to create transfer


function of following form:
z  0.5
Example: H ( z)  2
z with
1z sampling
2 time 0.4

Matlab Output
>>num = [-0.5];
>>den = [-1 -2]; Zero/pole/gain:
2 (z+0.5)
>>k = 2; -----------
>>Ts=0.4; (z+1) (z+2)
>>H=zpk(num,den,k,Ts) Sampling time: 0.4
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 65
04
DISCRETE TIME STATE SPACE MODELS(1)

State Space Model for dynamic system


x[n  1]  Ax[n]  Bu[n]
y[n]  Cx[n]  Du[n]

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
23
/ 20
6 P R E PA R E D B Y H AW I . A 66
Note:
04/0 Only apply to system that is linear and time invariant
DISCRETE TIME STATE SPACE MODELS(2)

Function: Use ss function creates state space


models. For example:
 x1[n]  0 1 0 
x[n]    A  B    C  0 1 D  0
 x2 [n]   5  2  3

MatlabOutput
Matlab Output
>>A = [0 1;-5 -2];
a = b =
>>B = [0;3]; Transfer function:
x1 x2 u1
x1 20 z 1
+ 1 x1 0
>>C = [0 1]; x2-------------
-5 -2 x2 3
>>D = [0]; z^2 + 3 z + 2
c = d =
>>Ts= [0.4]; x1 x2 time: 0.4
Sampling u1
y1 0 1
>>sys=ss(A,B,C,D,Ts)
/2
02
3 y1 0
/0
6 P R E PA R E D B Y H AW I . A
Sampling time: 0.4 67
04
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
Input Function Output
G(s)
Elements of a Block Diagram
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 68
04
COMBINING MODELS

The Following Matlab functions can be used to


perform basic block diagram manipulation
Combination Matlab Command

G1(s) G2(s) sys = series(G1,G2)


+
G1(s)
+ sys = parallel(G1,G2)
G2(s)

+
G1(s)
- sys = feedback(G1,G2)
23 G2(s)
/ 20
/0
6 P R E PA R E D B Y H AW I . A 69
04
BASIC ARITHMETIC OPERATIONS OF MODELS

Arithmetic Operations Matlab Code

Addition sys = G1+G2;


sys = G1*G2;
Multiplication
sys = inv(G1);
Inversion

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 70
04
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

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 71
04
TRANSIENT RESPONSE ANALYSIS

Unit step response of the transfer function system


Consider the system: 25
H s   2
s  4 s  25
%*****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
06
/ 20
23
Step Response of H(s)’)
P R E PA R E D B Y H AW I . A 72
/
04
TRANSIENT RESPONSE ANALYSIS

Unit step response of H(s)

Unit Step Response of H(s)


1.4

1.2

0.8
Am plitude

0.6

0.4

0.2

0
0 1 2 3 4 5 6 7

23
Time (sec)

/ 20
/0
6 P R E PA R E D B Y H AW I . A 73
04
TRANSIENT RESPONSE ANALYSIS
Alternative way to generate Unit step response of the
transfer function, H(s)
%*****Numerator & Denominator of H(s)
>>num = [0 0 25];den = [1 4 25];
%*****Create Model
>>H=tf(num,den);
>>step(H)

10u (t )
If step input is , then step response is generated with
the following command:

>>step(10*H)
/2
02
3

/0
6 P R E PA R E D B Y H AW I . A 74
04
TRANSIENT RESPONSE ANALYSIS

Impulse response of the transfer function system


25
Consider the system: H s   2
s  4 s  25
%*****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
06
/ 20
23
Response of H(s)’)
P R E PA R E D B Y H AW I . A 75
/
04
TRANSIENT RESPONSE ANALYSIS

Impulse response of H(s)


Impulse Response of H(s)
3

2.5

1.5
Amplitude

0.5

-0.5

-1
0 1 2 3 4 5 6 7
Time (sec)

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 76
04
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
25
Consider the system: H s   2
s  4 s  25

For unit-ramp input, . Hence


1
U ( s) 
s2 NEW H(s)
1  25  1 25
Y s   2   2   2
/ 20
23

s  s  4s  25  s s s  4 s  25 
/0
6 P R E PA R E D B Y H AW I . A 77
04 Indicate Step response
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)’)
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 78
04
TRANSIENT RESPONSE ANALYSIS

Unit Ramp response of H(s)

Unit Ramp Response Curve of H(s)


7

0
23 0 1 2 3 4 5 6 7
/ 20
/0
6 P R E PA R E D B Y H AW I . A 79
04
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

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 80
04
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)’)
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 81
04
FREQUENCY RESPONSE ANALYSIS
Example: Bode Diagram for

25
H s   2
s  4 s  25
Bode plot of H(s)
20

10

0
Bode magnitude plot
Magnitude (dB)

-10

-20

-30

-40

-50

-60

-45

Bode phase plot


Phase (deg)

-90

-135

3
2-180
/ 20 100
82
1 2
6 10 10 P R E PA R E D B Y H AW I . A
4/0 Frequency (rad/sec)
0
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

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 83
04
STABILITY ANALYSIS BASED ON
FREQUENCY RESPONSE
Example: Matlab code to draw a Nyquist Plot
Consider the system 1
H s  
s 2  0.8s  1
%*****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
06
/ 20
23
Plot of H(s)’) P R E PA R E D B Y H AW I . A 84
/
04
STABILITY ANALYSIS BASED ON
FREQUENCY RESPONSE
The Nyquist Plot for 1
H s   2
s  0.8s  1

Nyquist plot of H(s)

0 dB -2 dB
2 dB
-4 dB
1
4 dB

-6 dB
6 dB
0.5
10 dB -10 dB
Imaginary Axis

20 dB -20 dB

-0.5

-1

23
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1

20
Real Axis

6/ P R E PA R E D B Y H AW I . A 85
4/0
0
Extra: partial fraction expansion
2 s 2  3s  2
Given: G (s)  2
s  3s  2
num=[2 3 2];
den=[1 3 2];
[r,p,k] = residue(num,den)

r= Answer:
-4
1 r (1) r ( n)
p= G( s)  k (s)  
-2
-1  0
s  p(1)
4 1
s  p ( n)
k=  2s  
2 s  (2) s  (1)

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 86
04
M-File Example
%Define the transfer function of a plant %find the bandwidth of the new
G=tf([4 3],[1 6 5]) system
wb=bandwidth(T)
%Get data from the transfer function %plot the step response
[n,d]=tfdata(G,'v') step(T)
[p,z,k]=zpkdata(G,'v') %plot the rootlocus
[a,b,c,d]=ssdata(G) rlocus(T)
%Check the controllability and observability of the %obtain the bode plots
system
ro=rank(obsv(a,c)) bode(T)
rc=rank(ctrb(a,b)) margin(T)
%find the eigenvalues of the system %use the LTI viewer
damp(a) ltiview({'step';'bode';'nyquist'},T)
%multiply the transfer function with another transfer %start the SISO tool
function
sisotool(T)
T=series(G,zpk([-1],[-10 -2j +2j],5))
%plot the poles and zeros of the new system
iopzmap(T)

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 87
04
C S
H I
A P
GR
GRAPHICS
Basic Plotting
 Linetypes, markers, colours, etc
Subplots – creating multiple axes on a single figure
Annotating figures – titles, labels, legends
Editing figure properties

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 89
04
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.

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 90
04
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’

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 91
04
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

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 92
04
 ‘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,..)

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 93
04
P R E PA R E D B Y H AW I . A 94
P R E PA R E D B Y H AW I . A 95
PROPERTY EDITOR — AXIS
Edited objects

Tree of objects

Help
for object

P R E PA R E D B Y H AW I . A 96
SUBPLOTS
subplot(m,n,p) : create a subplot in an array of axes

>> subplot(2,3,1); P=1 P=2 P=3

>> subplot(2,3,4);
m
P=4

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 97
04 n
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

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 98
04
% 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])

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 99
04
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 100
04
% 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]=
2meshgrid(x,y)
3
02
6/ P R E PA R E D B Y H AW I . A 101
/0
04
>> Z= X.^2+Y.^2
IT H
L W
R O
T
N B
O
C TLA
R N A
E M
OD
M
SYSTEM PERFORMANCE:

For example, consider a TF

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
20
23 >>Peak: 1.1630
6/ P R E PA R E D B Y H AW I .A 103
04/0 >>PeakTime: 1.8100
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

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 104
04
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'

23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 105
04
23
/ 20
/0
6 P R E PA R E D B Y H AW I . A 106
04

You might also like