Lab 1 Sample Report: Getting Acquainted With Matlab/Simulink
Lab 1 Sample Report: Getting Acquainted With Matlab/Simulink
MATLAB/SIMULINK
K.S. Tsakalis
8/24/01
ABSTRACT Moving beyond this general introduction of the tool and its
history (from a user’s perspective), the more immediate
This report provides an introduction to MATLAB/SIMULINK objective of this report is to demonstrate the use of MATLAB
and its applications to the solution of problems that arise in the in solving common problems in feedback systems. A set of
analysis and design of feedback systems. such problems has been defined in the EEE480 Lab 1
Assignment [1]. Their solution requires a variety of
1. INTRODUCTION computations with the following common themes:
vector and matrix manipulations
MATLAB, by MathWorks, is a high level computation and
simulation language that allows easy and reliable manipulation solutions of differential equations
of vectors and matrices. The initial core of MATLAB was
based on standard linear algebra libraries and, in the 80’s, dynamical system simulations with SIMULINK
became a popular tool for working with linear systems. Since display of results
then, the software expanded in both capabilities and
acceptance. The solutions presented in the following sections show that
these problems can be handled with relative ease. Moreover,
The high level coding allowed for the quick development of through the use of script and SIMULINK model files, the
new “macros” and functions, adding new capabilities. These effects of changes in the problem data or parameters can be
codes (“m-files,” “scripts”) are simply text (ASCII) with a investigated with minimal additional effort.
sequence of commands for the MATLAB interpreter. They are
virtually machine independent and can be shared or transferred The rest of this document is organized as follows: Section 2
easily. Sets of frequently encountered functions were bundled contains the theoretical background necessary to formulate and
together in “Toolboxes” focusing on specific applications compute the solutions. The use of suitable MATLAB
(Signal processing, Control, Identification, Robust control etc.). commands for each item of the assignment is discussed in
The later addition of SIMULINK, a block diagram-based GUI, Section 3. In Section 4, the results for each question are
increased the ease of use of the software. presented. Finally, Section 5 provides concluding comments
and suggestions for further work.
Further developments were made to improve the speed of
execution of repetitive tasks. The MATLAB engine is 2. THEORETICAL BACKGROUND
extremely fast and efficient in handling arrays. However, the
use of the interpreter introduces delays that become significant Most of the problems in this assignment can be handled
in the execution of long “FOR” loops. The always- directly with an appropriate MATLAB command or function.
recommended vectorization of the code provided a partial For the problems requiring the computation of a step response
remedy but a more complete answer was found in the creation of Linear Time Invariant (LTI), analytical solutions could be
of pre-compiled portions of code (mex- and now dll- files). The obtained using Laplace transforms, e.g., see [2]. Such an
ability to use such code was always available but its usefulness approach, however is quite tedious and is not used here.
increased with the creation of auto-coding programs Instead, the solution is computed numerically by integrating the
(translators of MATLAB language to C and then compiled associated ordinary differential equation (ODE). This is also
code). Auto-coding from SIMULINK can now produce stand- necessary in Problem 9 of the assignment that requires the
alone real-time executables that are extremely useful in rapid solution of a nonlinear ODE for which no analytical solution is
prototyping applications. The latest version of MATLAB with known. (See [3] and references therein for numerical methods
data structures, excellent graphics, Graphical User Interface for solving ODEs.)
(GUI), etc. is an invaluable tool for systems applications from
the research level to implementation. 2.1 Least-squares regression
Learning MATLAB has always been an exercise in asking for Least-squares regression refers to problems of the form
help. Every m-file contains a contiguous segment of lines
beginning with % (comment indicator) with text that is min | x i wi | 2
displayed whenever the command <help file_name> is entered. i
Recent versions also contain searchable help databases with
more informative comments and pictures. “Who” and “whos” where is an (n x 1) vector of parameters over which the
are another two useful commands in examining what variables minimization is performed and xi, wi are the problem data, xi
are in the workspace. But never ask “why”! being a scalar and wi an (1 x n) vector, often referred to as the
“regressor” vector. This problem appears frequently in very 3.2 title
diverse applications and its solution is well known, e.g., see
[4]. Briefly stated, the least squares solution is Figure titles and axis labels accept strings as arguments. These
commands can “understand” TeX commands to produce special
(W T W ) 1W T X symbols, subscripts etc. For example
where X is an (m x 1) vector produced by the concatenation of title(‘Response of \Delta_m (s)’)
the xi’s and W is an (m x n) matrix produced by the
produces the text
concatenation of the wi’s.
Response of m(s)
2.2 Partial Fraction Expansion
as a figure title.
The complete formulae for computing the residues of a partial TeX is a high quality scientific typesetting software; see more
fraction expansion (PFE) can be found in the textbook [2]. details in [3]. The Lab assignments and Class notes have been
While there is a corresponding MATLAB command that typeset in TeX. The present document has been processed in
provides an easy answer to most PFE problems, the analytical Word. While the quality difference is quite obvious, learning
formulae are not useless. The computation of residues for poles TeX has a significant overhead (in Word WYSIWYG).
with multiplicities is numerically unstable and the MATLAB
function fails to produce the correct result. Depending on the The new MATLAB versions allow title editing on the figure
problem at hand, it may be sufficient to perturb the (double-click on the object) but problems may occur in pause
denominator coefficients slightly to destroy any pole mode.
multiplicities. But if an exact answer is required, it must be
computed the hard way. 3.3 Working with figures
2.3 State-Space equations MATLAB figures can be copied and pasted in a Word
document in a Bitmap format (<Edit/Copy Figure and Copy
The standard state-space description of finite dimensional LTI Options>). The Metafile option can offer better quality but
systems is the following first order vector ODE causes problems in Word when there are multiple figures in one
page. The ultimate is a Postscript/EPSF save of the figure in a
x Ax Bu separate file using a <print> command from the Command
y Cx Du Window. This format can then be imported in a TeX document.
With this definition, statements of the form “the system Sometimes a situation arises where an intermediate figure
[A,B,C,D]” should be interpreted as “the system with state- produced by a program should be saved for further processing,
space representation given by the above equations.” State-space e.g., change of the title in a step response or save in EPSF.
descriptions of linear systems are very useful in modeling Assuming that the program is at a pause mode, click on
multivariable systems, understanding geometric properties of <File/New Figure>. A dialog box will come up: “In pause mode
their behavior and allow for very efficient computations of – Execute anyway?” Reply <Yes> to open a new figure. Unless
system properties, controller design and optimal control the program contains special figure commands the rest of the
problems. It should be mentioned that state-space plots will appear in the new window (or, in general, the active
representations are not unique. There are infinitely many ways window). After the program finishes click on the figure and
to write a system with a given transfer function in state space. edit or save.
Modulo some technical details, all these state space
representations are related by a so-called similarity 3.4 Using SIMULINK
transformation. (These issues are treated in more detail in
courses like EEE 482, 582 etc.) To create a SIMULINK model select <File/New/Model> from
the MATLAB window. This creates a new model file where
3. MATLAB COMMANDS FOR LAB 1 you can define your system in terms of a block diagram. To
import new blocks you need to open the SIMULINK library by
The use of most commands is quite clear from the supplied typing <simulink> at the MATLAB prompt (command
script file [5]. Only a few commands need additional window). Then “drag-and-drop” the desired blocks from the
explanations that are listed below. library and connect them (double-click-hold-and-drag creates a
connection line).
3.1 Left division “\” To run a simulation there are a couple of standard ingredients
that are common in most cases: Sources (to provide excitation)
The operation “\” means left division and is equivalent to the and Sinks (to record/display/save the results). Most are
least-squares formula when the matrix (WTW) is invertible. adequately explained in the SIMULINK Library Browser.
Internally, the computation is performed in a different, However, the following two are very useful and may be
numerically more robust way. initially overlooked: One is the “clock” and the other is the
save “to workspace” block. The former generates a time signal
that is sent to the workspace after the simulation is finished,
2
stopped or paused. Saving other important variables the same
way, makes the signals available for post-processing, plotting,
storing in the workspace. Another useful sink is the scope for
quick plotting of the results. A multiplexer can be used to plot
multiple signals in the same plot. In contrast to the other
plotting blocks, the scope is simpler and less powerful but does
not slow down the speed of execution. For more details on the
use of these blocks, see the supplied model files.
The complexity of block diagrams can become unmanageable
very quickly. For this reason, it is convenient to create
subsystems out of groups of blocks. This command is found
under <Edit> and it helps to maintain a low complexity model
at each level. It is easy to visualize the system connectivity, and
information flow and allows for an easier debugging or code
maintenance. Finally, the numerical attributes of the solver are
found under <Simulation/Simulation Parameters>. The final
Figure 1: Least-squares straight-line fit of the
time is typically the one parameter that can vary depending on
given function and the associated fitting error
the application; the rest of the parameters should be left
unchanged unless there is a specific reason.
4. RESULTS
The script file “lab1.m” was used to generate all the MATLAB-
related results that are included in this section. In general, it is
not necessary to perform all of the Lab experiments with a
single script file but in this case it facilitates the dissemination
of the results.
Figure 1 was produced by a set of commands that are included 4.5 System responses with Simulink
in the script file. Notice that the quality is not very good
because the figure was saved as a Bitmap. This format is easier The block diagram shown in Figure 3 was created in a new
to manage in Word and Powerpoint. SIMULINK window.
After running the simulation for 40 time units, the command
4.4 System responses <plot(T,Y)> was issued in the Command Window to generate
the plot of the step response shown in Figure 4. Notice that the
After defining the transfer function as system objects (“tf”
plot is not as smooth as the one obtained from the “step”
command) the desired results are automatically generated by
command. This is because the SIMULINK solver used a coarse
the “step” function (Figure 2). Notice that the time vector of the
grid of points to compute the solution. If necessary, this can be
step response can be obtained as an additional output argument
adjusted by changing the tolerance in the Simulation
and it can be user defined as an additional input argument.
Parameters, or even enforcing a maximum step size. In general,
Figure 2 shows the step response of G2(s). As expected from
such modifications will slow down the simulation.
the low damping ratio (=0.15), the response is very oscillatory
3
the feedback system has dimensions (1 x 3) and value [0 0
0.4].
Figure 4: Step response of G2(s) generated with The block diagram implementation of the differential equation
can be found in the model file “lab1_9.mdl” in [5]. The key
SIMULINK. principle in constructing such implementations is to begin with
a sequence of integrators whose outputs are a variable of
4.6 Composite systems interest and its derivatives. For a 2 nd order ODE, two integrators
are needed to generate the signals y, y’. The input of the first
The step responses of the cascade and feedback connections of
integrator is y’’ which is then constructed by using functions of
the given systems are shown in Figure 5. These systems were
y, y’ and u according to the ODE. Initial conditions can be set
generated with the commands included in the script file [5].
for each integrator by double-clicking on the block. In this
Observe that in the cascade connection case, the oscillatory
manner, one can create models for arbitrary ODEs.
behavior of G2(s) is attenuated significantly by the low-pass
nature of G1(s). On the other hand, in the feedback case the The step response of this system (which is a modification of the
relationship between closed-loop poles and open-loop poles is well-known Van der Pol nonlinear oscillator) can be seen by
not as simple and a full justification of the response running the “lab1” script [5] and is not included here.
characteristics requires a more detailed analysis (e.g., see root
locus in [2]). 5. CONCLUSIONS
This report demonstrated the use of MATLAB for solution of
4.7 State-space representations various problems arising in Feedback Systems. These problems
The state-space representation of a system can be obtained with included the computation of system responses, system
the “ss” command (e.g., <Gss=ss(G)>). After that, the variable properties (e.g., poles), construction of simulation models, and
Gss is an object that contains the A,B,C,D matrices of the the handling of figures. Of course, this document provides only
representation and other pertinent information about the a quick overview of the tool capabilities. It should be
system. The matrices A,B,C,D can be extracted simply by understood that expertise can only be achieved through
typing “Gss.a” or “Gss.b” etc. For example, the C-matrix for
4
A more detailed report on the Results with answers on all
of the assigned questions. A suitable level of detail should
be similar to Sections 4.2, 4.4 (2 nd order system) of this
report.
A short (one-paragraph) Conclusions section
References
Creating the report document with many figures can be quite
painful because Word is not well suited for this task. To
simplify the editing of the report, it is strongly recommended
that you place all the figures in sequence at the end of the
document. As you paste each figure, select it, then click on
<Insert/Caption> and edit the caption. Then group the figure
with the caption so that they always move together. After you
finish with the editing of the text, return to the figures and
adjust their location.
6. REFERENCES
[1] K. Tsakalis, “EEE 480 Lab Experiments,”
http://www.eas.asu.edu/~tsakalis/coursea/lab.pdf, Arizona
State University, Aug. 2001.
[2] Franklin, Powell, Emami-Naeini, Feedback Control of
Dynamic Systems, 3rd Ed., Addison Wesley, 1994.
[3] MATLAB Help, (On-line documentation, Release 12),
Copyright 1984-2000, The MathWorks.
Figure 5: Step responses of the cascade system [4] G. Strang, Linear Algebra and its applications. 2nd Ed.
(top) and the feedback system (bottom) for Lab Academic Press, NY, 1980
1.6. [5] K. Tsakalis, “Sample Simulink models and Matlab scripts
continuous use and frequent applications of the “help” for the Lab,” in http://www.eas.asu.edu/~tsakalis/coursea,
command. Arizona State University, Aug. 2001.
As a last comment, the intent of this document is both to
provide an introduction to MATLAB and serve as a Lab Report
template. For the first, it contains significantly more
information than a typical Lab Report. For the second, it
contains detailed answers to only a few of the stated problems.
Normally, a Lab Report are expected to contain:
Abstract (or scope)
A much shorter Introduction with the student’s
understanding of the experiment.
A short Background section, if any, with mostly textbook
references for the pertinent theory. Typically, the
theoretical background will be contained in the textbook
and any references (e.g., design equations) can be given
directly in the Analysis section. In such a case, the
Background section can be omitted.
An Analysis section of varying length, containing block
diagrams, lists of design equations, and an outline of the
design procedure. (Bullet- or number- lists are often
adequate.) Notice that in Lab 1, the block diagrams are
part of the results and therefore presented in the Results
section.