Matlab Computer Modules ECE a FallDocument Transcript
UNIVERSITY OF CALIFORNIA, SANTA BARBARA
Department of Electrical and Computer Engineering
Matlab Computer Modules
ECE 147a (Fall 95)
Copyright c 1995, Roy Smith
This material is provided solely for the use of students enrolled in ECE 147a at
the University of California, Santa Barbara, in Fall 1995.
4 ECE 147a: (Fall 95) Matlab Computer Modules
This booklet contains the four Matlab teaching modules which accompany
ECE 147a. They cover most of the topics taught in the course and are intended
to be worked on at the same time the material is being presented in lectures.
Each consists of three parts: a discussion of the ideas and topics; one or more
worked out examples (complete with the required code); and a series of problems
for you to work out. The code and graphics for the worked out examples are
presented in this handout. Each Mac in the lab also has the example code on
it. Make a copy of these for yourself and use them as templates for the work to
be done on the problems. Do not modify the originals.
The modules are due to be handed in at approximately two week intervals and
will alternate with the in-class homework assignments. You will hand in print-
outs of the code, appropriate plots, and where applicable, written answers to
questions. It's very easy to generate lots of paper doing this | try to avoid
that temptation and submit only that which is necesary. I'm impressed by clear
and concise work and hate wading through verbiage. Note that it takes a longer
time to produce a shorter document covering the same material. The gurus
amongst will probably be able to generate typeset documents which include the
graphs and print-outs. Don't waste too much time on this.
Start work on these early. You will run into plenty of unforseen obstacles,
especially in the early modules, as you get up to speed.
Remember that you can use Matlab and the material in these modules to help
you in any other part of the course (except for the fact that you won't have
access to computers during the exams). I encourage you to play around with
these problems | if there is something that you don't understand or are unsure
about, then code it up in Matlab and try it out.
All references in the modules to the text refer to Feedback Control of Dynamic
Systems," 2nd Edition, by G.F. Franklin, J.D. Powell and A. Emami-Naeini
(Addison Wesley, 1991).
These modules and notes are an evolving thing. If you have any suggestions as
to how they might be improved I would be glad to hear them. For example,
there may be parts of the module that you do not understand, or commands
that seem obscure. Please write these things down and send me a note. The
students next year will de nitely appreciate it.
2 Using Matlab
Most students enrolled in ECE147a have already used Matlab for at least one
other class. If you haven't there is a very good introduction, the Matlab
Using Matlab 5
Primer," by Kermit Sigmon, which is available in the Library Reserve Book
Room. I suggest that you get up to speed on Matlab as soon as possible | it
will do a lot of tedious calculations that might come up in labs and homeworks.
You can use Matlab for everything in the course except the closed book exams.
It is very helpful for homeworks and labs. I encourage you to play around with
it on simple control problems. It's a very quick way of getting some intuition
about what is going on.
2.1 Computer Facilities
This year is the rst year that the ECI workstations are available for Matlab
use. The instructions here discuss the use of Matlab on IBM PCs and Macs.
The Controls Lab (3120, Eng. I.) is equipped with PCs so you can use Matlab
while you are doing your lab experiments. The Mac instructions will probably
only be relevant if you use a Mac at home or use Matlab in the MCL. I'm sure
that you will be able to work out how to translate these instructions to the new
The Controls Lab has about ve working IBMs which are also used for running
the digital control experiments in ECE147b. Matlab is available on these
machines, however the Lab is open only during the scheduled lab times.
Some students have Macs or PCs at home and may want to buy Matlab for
them. There is book available entitled The Student Edition of Matlab," which
comes with a version of Matlab software. The book store may a copy. If not
they can order it | the publisher is Prentice-Hall and the ISBN is 0-13-855982-1
(IBM 5.25 inch disk) or 0-13-855974-0 (IBM 3.5 inch disk). The software is more
limited than that available in the Maclab. For example, matrices are limited
to 1024 elements. The controls and toolboxes are not included | however I
may be able to loan the toolbox to any ECE147 student who buys the student
version. If you buy a student version you can get a $50.00 discount on the real
thing later. Matlab is readily available in the Maclab and most engineering
employers have Matlab | which means that there may not be a good reason
for you to have a version at home too.
2.2 Starting Matlab and -Tools
On the IBM machines starting Matlab is simply a matter of entering the com-
6 ECE 147a: (Fall 95) Matlab Computer Modules
The machines in the Controls Lab should also have -Tools installed. These
machines are old and have accumulated super uous les on their disks. The
quality of Matlab, and -Tools is not guaranteed on these machines.
The rudimentary aspects of running the Mac can be learned in a couple of
minutes. If you have never used one before ask either the TA or someone in
the Lab to show you how to start. I will assume that you know how to move
around the folders, drag & copy, delete, open disks and select menu items.
Matlab is located in a folder, labeled MATLAB on the hard disk. To run
Matlab you need only open that folder and double click on the Mac II-MatLab
application. This alone does not give you access to -Tools functions or the
assigned modules. These les are stored on the Unix server and you must rst
establish a link to that server. This is done in the following way.
Pull down the menu from the colored apple and select the Chooser.
Highlight the AppleShare icon
Highlight the Maclab File Server line in the resulting window. Click on the
You will now need to ll in your username and password. After doing this
The material that can be accessed over the network will be displayed.
Highlight the server.mutools line and click OK.
This procedure will give you access to all of the -Tools functions. The proce-
dure for setting up access to the printer is almost identical except that the line
summit.fusernameg has to be highlighted. You should set up both things before
Before shutting o the machines drag both of these icons into the
trash. This is very important | if you do not do this your login priviledges are
available to the next person to turn on the machine. Similarly, do not set up
any automated server connection procedure using your account and password.
2.2.3 Macintosh Path Problems
If, after setting up the server connection, you still cannot use Matlab, you
may have an incorrect path. Unfortunately the path is easily corrupted. To
Using Matlab 7
check the Matlab path pull down the M- le menu and select Set Path. It must
contain at least the following lines.
If these are not present add them to the list and click on Save. Do not hit
the Auto Set button. This performs a very crude search for subfolders in the
MATLAB folder and sets them up in the path. All references to the server will
be deleted. This was probably how the path became corrupted in the rst place.
2.2.4 Disk Usage
Do not leave any of your script les or special purpose functions on the ma-
chines. Move all such items to a personal disk. The machines are regularly, and
brutally, purged of all unknown les. Anything left behind will be lost. This is
particularly true in the MacLab as it receives heavy use and the computers are
monitored on a daily basis.
2.3 Automating Matlab and Taking Notes
2.3.1 m- les
To drive Matlab in a batch le fashion, M- les are created. Suppose there
is some long calculation that depends on only a few variables; with an M- le,
only the variables need to be re-entered, and not the set of Matlab equations,
to get the new result. M- les are discussed in the Matlab Primer. If you are
using M- les on an IBM PC, then you will need to set up the correct path for
the les use. If you are using a Mac, then the M- le is set up by placing a copy
of it in the MATLAB folder.
To get a copy of all of the Matlab text output, the diary function is used.
>> help diary
at the Matlab command line for an explanation of use. If you are on an IBM
PC, then the diary le will be stored in the current working directory. If you
8 ECE 147a: (Fall 95) Matlab Computer Modules
are using a Macintosh, then the diary output le will be in the same folder that
was active when Matlab is started.
2.4 Setting up Matrices and Vectors in Matlab
Since these are the basic data structures of Matlab, we must know how to set
them up correctly so that the dimensions of the equation are followed. Once we
know which way the columns and rows are built, and the method used to access
elements, we can explore ways to build matrices in some automated way. The
Matlab Primer discusses matrices on pages 1 through 3, and then shows ways
to automate the building of matrices through M- les.
Note: Some functions operate di erently on matrices than they do
on scalars. For example, if A is an n n matrix, the Matlab function
>> B = exp(A);
only applies element wise to the matrix and not globally matrix wise. In other
words, for each element, bij ,
bij = ea ;ij
B = I + A + A + . . .:
A list of some other functions to take notice of is on page 6 in the primer. It is
always a good idea to check the online help or do an example calculation to see
if the function you are using is behaving.
2.5 Special Functions for Control Problems
2.5.1 Control Toolbox
Although we will primarily use -Tools, the control toolbox is a possible alter-
native for most calculations. The control toolbox contains functions that solve
speci c control problems. For example, root locus plots can be generated using
the functions rlocus, and plot. Consult pages C-8, and C-22 through C-25 in
the Control Toolbox manual for the short descriptions of most of the functions
you will be interested in. Once you know the name of a function, there is always
online help on the computers. The command line is
Using Matlab 9
>> help function-name
It is a good idea to read up on the excerpts of the -Tools manual. We have
selected the material we feel would be most important, there may be other
portions of the -Tools manual that you may want to reference. The Data
Structures" to More Sophisticated System Functions" sections (pages 1-3 to
1-29) should be read. Don't worry about understanding everything the rst
time, just get an idea of where you can nd the information that you may need
to check later. Also, skim over pages 4-1 to 4-9 for the quick summaries of the
-Tools functions and where to nd them.
There is also a root locus function, rloc, written to handle systems in the -
Tools format. A manual page for this function can be found in Section 5.4.
Abbreviated information can be obtained with the command,
>> help rloc
Between the Controls Toolbox and -Tools, you shouldn't have to work to hard
to get complex calculations done easily.
2.6 Graphics and Printouts
Both the Macs and the PCs generate graphics in about the same way. The main
functions of interest are plot and vplot. These are referenced on line and in
the manuals. It is easy to set up titles and axis on the graphs within Matlab.
There are many examples in the manuals, and in this handout.
In order to get hard copies from the Mac you have to initialize a username
and password. The procedure for doing this is described above in Section 2.2.2.
Now printing simply involves making the graphics screen the active window and
selecting the print option from the le pull down menu.
Problems often occur when printing complicated plots. In particular Bode plots,
generated with some command like,
often will not print. One work around is to specify and print the magnitude
and phase plots separately. For example,
10 ECE 147a: (Fall 95) Matlab Computer Modules
>> sys_g = frsp(sys,omega);
The magnitude plot can be printed at this point. The command,
plots the phase response which can also be printed.
2.7 Memory Problems
When doing complicated calculations Matlab may run out of memory. This
problem often occurs when calculating frequency or time responses with a lot
of data points. The obvious solution is to select fewer points.
There are some other alternatives too. Before doing any memory intensive
calculations, clear the unwanted variables out of your workspace. For example,
>> clear trash1 trash4
will delete the variables trash1 and trash4. This type of operation creates
a fragmented workspace and Matlab can only store variables in the largest
contiguous block of memory. To do a garbage collection", the command
is used. This saves all data on the hard disk, cleans out the memory, and reloads
the data from disk.
If you are using a Mac which runs Multi nder then other options are also avail-
able. The default memory for the Matlab application could be set to too
small a value. The typical default is 1024K bytes. If more memory is installed
(some machines have up to 8M) try asking for more memory. On an 8M ma-
chine, setting the memory to 5000K would not be unreasonable. We haven't
con rmed this yet, but it is possible that doing this will allow some of the more
complicated plots to print properly.
2.8 Example Control Problems
If you are reading through this at the beginning of the course, don't worry too
much about understanding the example. The rst few modules will help you
Using Matlab 11
out there. This section is provided as a reference that you may wish to refer
back to later | the command sequences used here can provide a template for
In the following example, we will take a second order stable plant, perform some
analysis, create controllers and again analyze the new closed loop system. Our
plant for this example will be,
P (s) = s2 + 4s + 16
We create the state space system matrix with the nd2sys (numerator denomi-
nator to system matrix) function.
>> sys = nd2sys( 0,0,16], 1,4,16])
Matlab returns with,
-4 -16 1 2
1 0 0 0
0 16 0 0
0 0 0 -Inf
First, we want to know where are the poles and zeros. We will use the functions
spoles (system spoles) and szeros (system zeros).
-2.0000 + 3.4641i
-2.0000 - 3.4641i
As you can see, there are two complex poles, and no nite zeros. Next we want
to perform frequency analysis of the plant. But rst, we want to establish a
de ned frequency base for doing our calculations. The command,
12 ECE 147a: (Fall 95) Matlab Computer Modules
>> omega1 = logspace(-1,2,200);
will create frequency vectors of 200 logarithmically spaced points from 10 1 to
102 . The frsp function accepts the frequency vector as rads/sec, so that then
becomes the units of our omega1 variable.
Now to perform the frequency analysis, we type,
>> sysfrsp1 = frsp(sys,omega1);
to create the frequency response. To plot this data we type,
to create the following Bode and Nyquist plots respectively. Figures 1 and 2
show each of these plots.
Next we wish to create a step response of the system. The following commands,
>>u = step_tr( 0], 1],.1,10);
>>y1 = trsp(sys,u);
will set up a step occurring at t = 0 with magnitude one and duration of 10
seconds, perform the time response to the input signal, and plot the data with
as shown in Figure 3.
To perform root locus analysis, the input,
>> k = 0.5:0.5:10];
>> r1 = rloc(sys,k);
Using Matlab 13
10 -1 10 0 10 1 10 2
10 -1 10 0 10 1 10 2
Figure 1: Bode plot of Open Loop system
sets up a vector of gains 0:5 to 10 for the root locus to be analyzed at, analyzes
the roots, and plots them on a real vs imaginary plot. There is a bug in Matlab
which ips the real axis if all of the data has the same imaginary part. The
above vplot command also plots points at -3 and -1. This will make sure that
the real axis is plotted correctly. Figure 4 shows the resulting root locus. If
we have selected a wider range of gains in k then more of the locus would have
Now that we have analyzed the open loop system, a constant gain of K = 2
looks like a good value. The following commands,
>> K = 2;
>> cont = K;
>> clsys = starp(mmult( 1;1],sys,cont, 1,-1]),1);
set up a constant gain controller, and then create a unity feedback closed loop
system with that controller. You may want to look at the starp function in
the manual, but if you use this function in the manner given here, then you will
always create a closed loop system of the form illustrated in Figure 5.
Again we perform frequency analysis,
>> clsysfrsp1 = frsp(clsys,omega1);
14 ECE 147a: (Fall 95) Matlab Computer Modules
-0.4 -0.2 0 0.2 0.4 0.6 0.8 1
Figure 2: Nyquist plot of Open Loop system
>> xlabel('real magnitude')
>> ylabel('imaginary magnitude')
to create the Bode and Nyquist plots shown in Figures 6 and 7 respectively.
To create the new step response, we don't need to retype the u de nition,
because it is already set up as a variable in the workspace. The step response
analysis is done with the commands,
>> y2 = trsp(clsys,u);
>> text(1,1,'input -.')
>> text(2,0.7,'output --')
>> xlabel('time: seconds')
to create the new graph. Note that we plotted a point at 1.2 to force the Matlab
automatic axis scaling to print this on the same scale as before. Another way
of doing this is with the Matlab command axis. Figure 8 shows the resulting
Now this system has too much overshoot, so lets put in some derivative control.
C (s) = 0:d s s + 1
Using Matlab 15
input -. output --
0 1 2 3 4 5 6 7 8 9 10
Figure 3: Step response of Open Loop system
This controller is set up with the same nd2sys call and then the closed loop
system, PDsys, is formed as,
>> Td = 0.5;
>> PDcont = nd2sys( Td K], 0.01 1]);
>> PDsys = starp(mmult( 1;1],sys,PDcont, 1,-1]),1);
The rest of these gures are set up nearly identically to the last set, so this will
be left as an exercise to try to duplicate these graphs.
16 ECE 147a: (Fall 95) Matlab Computer Modules
-3 -2.8 -2.6 -2.4 -2.2 -2 -1.8 -1.6 -1.4 -1.2 -1
Figure 4: Root Locus of Constant Gain Closed Loop System 0:5 < K < 10
y r Sys Cont j
Figure 5: Generic result of the command:
clsys = starp(mmult( 1;1],sys,cont, 1,-1]),1);
Using Matlab 17
10 -1 10 0 10 1 10 2
10 -1 10 0 10 1 10 2
Figure 6: Bode plot of Constant Gain K = 2 Closed Loop system
-0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
Figure 7: Nyquist plot of Constant Gain K = 2 Closed Loop system