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

DK2781 AppC

Uploaded by

Nahome Nigussie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

DK2781 AppC

Uploaded by

Nahome Nigussie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Appendix C

Introduction to MATLAB and Simulink

C.1 INTRODUCTION
MATLABÕ is a technical computing environment (program) designed for
numerical computation and visualization. Over the years it has become a
standard tool in many universities and research organizations for performing
mathematical calculations. Originally written to provide access to matrix
manipulation software, MATLAB has developed into a powerful tool to
simplify mathematical analysis.
In order to use MATLAB successfully, the student should under-
stand how to enter and manipulate variables, how to model control
systems in MATLAB, and how to produce plots and graphs depicting
the performance of control systems. This appendix is intended to guide
the student through the MATLAB functions which are useful in under-
standing and accomplishing these tasks. Students are encouraged to use
the documentation provided with MATLAB to explore more advanced
problems.
The commands shown here are valid for MATLAB version 6.1, Release
12.1, for Windows 98. There may be variations in other versions, or releases
for other platforms. Many of the commands shown here are in the Control
Systems Toolbox, which is available as an add-on to the basic MATLAB
program.

Copyright © 2003 Marcel Dekker, Inc.


694 Appendix C

C.2 BASICS
MATLAB is a window-based workspace environment in which variables are
entered and manipulated. The MATLAB program is started by double click-
ing the MATLAB icon in the Windows desktop. This opens a window similar
to that shown in Fig. C.1. Commands are entered in the command window,
which is the inner window as shown on the right side of Fig. C.1. Two right
arrows (>>) is the command prompt, indicating that the program is ready
for the next command. Variables are created as they are entered, and are
stored in the workspace. Variable names must begin with a letter. A variable
to represent  = 1.7 may be entered as follows:
alpha = 1.7;
Placing the variable name‘‘alpha’’to the left of the equal sign defines alpha as
whatever is on the right side of the equal sign.
MATLAB displays the variable upon entry unless a semicolon is placed
at the end of the line, that is,

beta = alpha*3.7 %Optional comment


beta =
6.2900

FIGURE C.1 MATLAB window.

Copyright © 2003 Marcel Dekker, Inc.


Introduction to MATLAB and Simulink 695

A percent sign provides a mean to include a comment. Everything to the right


of a percent sign is ignored by MATLAB.
In MATLAB version 6, a workspace window lists the variables that are
defined. The ‘‘who’’ command also allows the user to see what variables are
defined in the workspace.

Who
Your variables are:
alpha beta

C.2.1 Matrices
Matrices are entered a row at a time as follows:
A = [11 12 13
21 22 23
31 32 33]
The carriage return is implied at the end of each line and is not
explicitly shown in this text. MATLAB displays the variables upon entry
unless a semicolon is placed at the end of the line. Semicolons are also used
to simplify and conserve space. The same 3  3 matrix may be entered as
follows:
A = [11 12 13; 21 22 23; 31 32 33]; % Optional Comment
Matrices are indexed in (row, column) format. For example, a single element
of a matrix can be referenced as follows:
A(2,3)
This gives the result
ans =
23

C.2.2 Matrix Transpose


Matrices are transposed using a prime notation.
B = A’
produces
B=
11 21 31
12 22 32
13 23 33

Copyright © 2003 Marcel Dekker, Inc.


696 Appendix C

C.2.3 Range of Values


A colon is used to denote a range of values, e.g., A(1:2,:) is a
matrix consisting of the first two rows of A. A colon notation also can
be used to generate a vector of equally spaced elements. The format for
defining a vector x whose elements range from an initial value to a final
value is

x = ½0 : 2* pi; %½Initial value : Final valueaf

The step size is assumed to be 1 unless an optional step size is specified


between the initial and final values. A vector, used to plot data, may be
defined as follows:

x = ½0 : :25 : 2* pi %½Initial value : Step size : Final value

MATLAB generates the vector:

x=
Columns 1 through 7

0 0:2500 0:5000 0:7500 1:0000 1:2500 1:5000

Columns 8 through 14

1:7500 2:0000 2:2500 2:5000 2:7500 3:0000 3:2500

Columns 15 through 21

3:5000 3:7500 4:0000 4:2500 4:5000 4:7500 5:0000

Columns 22 through 26

5:2500 5:5000 5:7500 6:0000 6:2500

Numerous common mathematical functions are available, such as


y = sin(x)

which generate a vector corresponding to the sine of each element of the


vector x. Some other trigonometric functions available in MATLAB are
listed in Table C.1.
The data are plotted with the command
plot (x, y)

This command opens a plot window as shown in Fig. C.2. The window has
menu buttons across the top which allows the user to rescale, annotate, edit,
save, and print the plot.

Copyright © 2003 Marcel Dekker, Inc.


Introduction to MATLAB and Simulink 697

TABLE C.1 MATLAB Trigonometric Functions


sin Sine. sec Secant.
sinh Hyperbolic sine. sech Hyperbolic secant.
asin Inverse sine. asec Inverse secant.
asinh Inverse hyperbolic sine. asech Inverse hyperbolic secant.
cos Cosine. csc Cosecant.
cosh Hyperbolic cosine. csch Hyperbolic cosecant.
acos Inverse cosine. acsc Inverse cosecant.
acosh Inverse hyperbolic cosine. acsch Inverse hyperbolic cosecant.
tan Tangent. cot Cotangent.
tanh Hyperbolic tangent. coth Hyperbolic cotangent.
atan Inverse tangent. acot Inverse cotangent.
atan2 Four quadrant inverse tangent. acoth Inverse hyperbolic cotangent.
atanh Inverse hyperbolic tangent.

FIGURE C.2 MATLAB plot window.

Copyright © 2003 Marcel Dekker, Inc.


698 Appendix C

Multiple traces can be shown on one plot as illustrated by the following


commands:
z = cos(x);
subplot (2, 1, 1), plot (x, y) % Plot 2 rows 1 column and
% select the first subplot
title (‘Multiple Plots’) % Set title above first plot
grid % Turn grid on in first plot
ylabel (‘sin(x)’) % Set Y axis label in first plot
subplot (2, 1, 2), plot (x, z) % Select second subplot
grid % Turn grid on in second plot
xlabel (‘x’) % Set X axis label in second
% plot
ylabel (‘cos (x)’) % Set Y axis label in
% second plot

These commands generate the plot shown in Fig. C.3.

FIGURE C.3 Multiple plots in one window: sine plot (top figure); cosine plot
(bottom figure).

Copyright © 2003 Marcel Dekker, Inc.


Introduction to MATLAB and Simulink 699

Note that the titles and labels can be set from the command line or within
the plot window using the buttons across the top of the window. A number of
line styles and colors may be specified as described by the command:
help plot options
Typing ‘‘help’’ at the prompt will provide an outline of the help available in
the command window. More extensive help features are accessed through the
help menu button near the top of the MATLAB window.

C.2.4 m-Files and MAT-Files


MATLAB allows the user to define scripts containing several commands in
m-Files. m-Files are stored in the current directory as ‘‘filename.m’’ and
are executed from the command line by typing the file name without the
.m extension. m-Files may be created and edited from the MATLAB window
by using the ‘‘New m-File’’ button on the toolbar.
Workspace variables also may be saved for latter use by typing ‘‘save
filename’’. This creates a MAT-File in the current directory named
‘‘filename.mat’’. The variables are loaded back into the workspace by typing
‘‘load filename’’.

C.3 DEFINING SYSTEMS


Polynomials are represented by a vector of coefficients. For example, the
polynomial s3 + 2s2 + 5 is represented by the vector [1 2 0 5]. All coeffi-
cients including the zero must be included in the vector. Polynomials are
multiplied by convolving the representative vectors; hence the transfer
function
poly ¼ sðs þ 3Þðs þ 10Þ ¼ s3 þ 13s2 þ 30s ðC:1Þ
is formed as follows:
poly = conv([1 0], conv([1 3], [1 10]))
poly =
1 13 30 0
The roots of the polynomial are calculated using the ‘‘roots’’command.
roots(poly)
ans =
0
- 10
-3

Copyright © 2003 Marcel Dekker, Inc.


700 Appendix C

C.3.1 Transfer Function Model


A transfer function is a ratio of polynomials in s; hence the transfer function

num sþ5 sþ5


¼ ¼ 3 ðC:2Þ
den sðs þ 3Þðs þ 10Þ s þ 13s 2 þ 30s

is formed as follows:
num = [1 5];
den = conv([1 0], conv([1 3], [1 10]))
den =
1 13 30 0

The ‘‘TF’’ command is used to generate a linear time-variant (LTI) transfer


function system model object, ‘‘sys’’ from the numerator and denominator
polynomials. This transfer function system model object is subsequently used
for analysis as shown in Sec. C.4.
sys = tf(num, den)
Transfer function:
sþ5
-----------
s^3 + 13 s^2 + 30 s

C.3.2 State Space Model


State space models are entered as matrices. In Sec. 2.13, a DC servomotor is
modeled by Eq. (2.135). Augmenting the system with y_ m ¼ o yields the
third-order state space equation:
2 3
0 1 0 2 3 2 0 3
2 3 6 7 y
y_ m
m
6 Bm Kt 76 7 6 7
6 7 60 76 7 6 0 7
x_ ¼ Ax þ Bu ¼ 4 o_ m 5 ¼ 66 Jm 7
Jm 76 om 7 þ 6 6 7ea
4 5 4 7
_i m 6 7 1 5
4 Kb Rm 5 _i
0 m Lm
Lm Lm
ðC:3Þ

2 3
ym
6 7
y ¼ Cx þ Du ¼ ym ¼ ½ 1 0 0 4 om 5 þ ½0ea ðC:4Þ
i_m

Copyright © 2003 Marcel Dekker, Inc.


Introduction to MATLAB and Simulink 701

A very small 12 volt DC servomotor has the characteristics provided


below, which are converted to SI units to develop the A and B matrices. The
units of the variables are: y is in radians, o is in rad/s, i is in amperes, and e is
in volts.

oz-in:
Bm ¼ 0:013 oz:-in: Kt ¼ 1:223
A
mV
Lm ¼ 0:75 mH Kb ¼ 0:905
rpm
J ¼ 0:085  104 oz:-in:-s2 Rm ¼ 24 

2 3
0 1 0
A ¼ 40 1530 144000 5 ðC:5Þ
0 11:5 32000
2 3
0
B¼4 0 5 ðC:6Þ
1330

The following commands are used to enter the state space model, to
generate a linear time-invariant system model object, ‘‘sys2.’’ This state
space model object may be used for analysis like the transfer function system
model object. These commands have been saved in an m-file named ‘‘dcser-
vo.m’’ which is on the accompanying CDROM and can be executed by typing
‘‘dcservo’’at the command prompt.
A = [0 1 0 % Define the plant matrix
0 -1530 144000
0 -11.5 -32000];
B = [0; 0; 1330]; % Define the input matrix
C = [1 0 0]; % Define the output matrix
D = [0]; % Define the direct
% feedthrough matrix
sys2 = ss(A,B,C,D) % Generates a state space
% model object
which produces the state and output equations:
a=
x1 x2 x3
x1 0 1 0
x2 0 1530 1:44eþ005
x3 0 11:5 32000

Copyright © 2003 Marcel Dekker, Inc.


702 Appendix C

b=
u1
x1 0
x2 0
x3 1330
c=
x1 x2 x3
y1 1 0 0
d=
u1
y1 0
continuous-time model.

C.4 ANALYSIS
C.4.1 Root Locus
The LTI system model object is used for analysis. The root locus for a unity
feedback system is presented in Chap. 7. The root locus is plotted in
MATLAB using the ‘‘rlocus’’command.
rlocus(sys) % Plots a root locus for sys
or
rlocus(sys, K) % Plots a root locus with
% user-specified vector
% of gains K
A more advanced interface for root locus analysis and design is the
‘‘SISOTOOL’’ interface. The ‘‘SISOTOOL’’ is a graphical user interface that
facilitates compensator design and is started with the command:
sisotool(’rlocus’,sys) % Startroot locus design tool
This command opens a new window, plots the root locus as shown in Fig. C.4,
and allows the user to specify a gain or add compensator components by
placing poles and zeros on the plot. Design constraints such as desired damp-
ing ratio or peak overshoot can be set within the tool and appear as bounds on
the plot.
A root-locus plot for a digital control system (see Chap. 15) is shown
in Fig. C.5.

Copyright © 2003 Marcel Dekker, Inc.


Introduction to MATLAB and Simulink 703

FIGURE C.4 Root locus SISOTOOL window.

C.4.2 Frequency Response


Frequency response is presented in Chaps. 8 and 9. Frequency response plots
are generated for LTI system models using the command
bode(SYS)
which generates a Bode diagram as shown in Fig. C.6 for Eq. (C.2). Another
example is shown in Sec. 9.9. Nichols and Nyquist plots are generated with the
commands:
nichols(SYS)
or
nyquist(SYS)
The ‘‘SISOTOOL’’ described above also provides options for designing on a
Bode or Nichols chart. The command
sisotool(‘bode’,sys) % Start bode plot design tool

Copyright © 2003 Marcel Dekker, Inc.


704 Appendix C

FIGURE C.5 Digital root locus SISOTOOL window.

opens the‘‘SISOTOOL’’ with the Bode plots as shown in Fig.C.7.The command


sisotool(‘nichols’,sys) % Start nichols chart
design tool
opens the ‘‘SISOTOOL’’ with the Nichols chart as shown in Fig. C.8. With
either view, the user can adjust gain or add compensator poles and zeros and
immediately see the resulting loop characteristics.
Another ‘‘SISOTOOL’’ example is the Nichols chart shown in Fig. C.9
that depicts the plot of Lm G vs fG being tangent to an M-contour.

C.5 SIMULATION
Control systems are commonly evaluated by simulating the response to a step
and an impulse inputs.The‘‘step’’and ‘‘impulse’’commands generate plots
of the step and impulse response respectively.When invoked with arguments
to the left of the equal sign as shown,
[y, t] = step(sys)

Copyright © 2003 Marcel Dekker, Inc.


Introduction to MATLAB and Simulink 705

FIGURE C.6 Bode diagram of an example transfer function.

or
[y, t] = impulse(sys)
the output vector ‘‘y’’and time vector ‘‘t’’ that are generated automatically for
the plot, are defined in the workspace.These vectors are available then for use
elsewhere.
It is often desirable to specify the input as a function of time to generate
simulations that are more complex than the step and impulse inputs. This is
done by defining an input vector ‘‘u’’ and the corresponding time vector ‘‘t’’.
These vectors are used as inputs to the ‘‘lsim’’ command that simulates the
response of a linear system as follows:
t = [0:.1:10]; % Define a simulation time
% vector
u = [ones(1,51) zeros(1,50)]; % Define vector representing
% a pulse input
[y, t] = lsim(sys,u,t); % Simulate the response
plot(t, [y, u]) % Plot the response

Copyright © 2003 Marcel Dekker, Inc.


706 Appendix C

FIGURE C.7 Bode diagram SISOTOOL window.

This example also illustrates the ones and zeros commands. A vector
or matrix whose elements are all ones or zeros is defined with these
commands, using arguments which are the size of the vector or matrix
desired. A related function is ‘‘eye (rows, columns)’’ which defines an
identity matrix or a nonsquare matrix with ones on the principal diagonal
and zeros elsewhere.
eye(3)
ans=

1 0 0
0 1 0
0 0 1

eye(3,5)
ans=

1 0 0 0 0
0 1 0 0 0
0 0 1 0 0

Copyright © 2003 Marcel Dekker, Inc.


Introduction to MATLAB and Simulink 707

FIGURE C.8 Nichols chart SISOTOOL window.

C.5.1 Simulink
For simulations of more advanced system models, including nonlinear
models, it is useful to run Simulink. Simulink is a graphical simulation tool
which is started from the MATLAB command window by typing ‘‘simu-
link’’ at the prompt. This opens a window with the Simulink block library
and provides an option for creating a new Simulink model. Blocks are selected
and placed on the model workspace to create a system block diagram as shown
in Fig. C.10.

C.6 IMPLEMENTATION OF RESULTS


Compensators are typically designed as continuous time transfer functions.
They may be implemented with analog components, but it is more common
to implement them in a digital control system. MATLAB provides commands
for transforming analog designs into the discrete domain. To implement the

Copyright © 2003 Marcel Dekker, Inc.


708 Appendix C

FIGURE C.9 Nichols chart diagram SISOTOOL window with grid and bounds.

FIGURE C.10 Simulink block diagram.

Copyright © 2003 Marcel Dekker, Inc.


Introduction to MATLAB and Simulink 709

lead filter Gc ¼ ðs þ 3Þ=ðs þ 30Þ in a digital system with a sampling rate of


40 Hz, the ‘‘c2d’’ function is used.
num = [1 3];
den = [1 30];
ts = 1/40;
sys = tf(num,den)
Transfer function:
sþ3
----
s þ 30
sysd = c2d(sys,ts,’zoh’)
Transfer function:
z  0.9472
------
z  0.4724
Sampling time: 0.025
The discrete transfer function is used as described in Chap. 15. The ‘‘c2d’’
function provides several transformation methods including the zero order
hold method shown here and the Tustin transformation. See the MATLAB
documentation for more information.

Copyright © 2003 Marcel Dekker, Inc.

You might also like