Instructor: Prof. Juan Alvarez, 3046 ECEB, alvarez@.
Teaching Assistant: Cheng Chen, cchen130@.
Lectures: Tuesdays and Thursdays 9.30-10:50 a.m., 4026 ECEB.
Textbook: Custom book for ECE459, ISBN: 978-1
M-les allow you to save multiple MATLAB commands in a le and then run them
with a single command or mouse click. While you may have solved the simple problem above correctly on the rst try, more complicated problems generally require
some trial an
Changes you make to the current directory and path are not saved from one
MATLAB session to the next. At the end of the Script M-les section below,
we describe how to change these and other items automatically each time you
The Command Hist
Interacting with MATLAB
In this chapter we describe an effective procedure for working with MATLAB, and for
preparing and presenting the results of a MATLAB session. In particular we discuss
some features of the MATLAB interface and the use of M
The result appears in Figure 2.6. Note that we used a semicolon to suppress printing
of the 301-element vector X.
Figure 2.6. Plot of a Parabola.
We describe more of MATLABs graphics commands in Chapter 5.
The term folder is now more common than directory; for a computer le
system, they mean the same thing. We will use directory because MATLAB
uses this term in its documentation. However, its interface sometimes uses
folder, for example in the File Type co
While the Desktop provides some new features and a common interface for
the Windows and UNIX versions of MATLAB, it may also run more slowly
than the basic Command Window interface, especially on older computers.
You can run MATLAB with the old interface
Chapter 2. MATLAB Basics
is that doing so via MATLAB commands in the Command Window provides more
robustness, especially if you want to save your commands in an M-le (see Chapter 3)
in order to reproduce the same graph later on. To close the gure, type
Figure 2.2. Desktop with the Workspace Browser.
You can make very general assignments for symbolic variables and then manipulate them:
> syms x y
> z = x2 - 2*x*y + y
A variable name or function name can be an
function y = sinelimit(c)
% SINELIMIT computes sin(x)/x for x = 10(-b),
% where b = 1, ., c.
b = 1:c;
x = 10.(-b);
y = (sin(x)./x);
The rst line of the le starts with function, which identies the le as a function M-le. (The Editor/Debugger col
to task1.m rst; otherwise, MATLAB will not recognize them.
Any variables that are set by running a script M-le will persist exactly as if
you had typed them into the Command Window directly. For example, the
program above will cause all future numerical o
For M-books to run properly, you must enable execution of the necessary
macros in Word. The safest way to do this is to see whether you get a
security warning dialog box when you open an M-book and, if so, select
Always trust macros from this source. Thi
Several Sine Curves
Figure 3.1. Sine Curves.
mands, and graphics. This allows you more control over formatting than publish.
A simple rst approach is to prepare a Word document with explanatory comment
% These values illustrate the fact that the limit of
% sin(x)/x as x approaches 0 is equal to 1.
If your M-le puts two successive gures in the same gure window, then only
the last gure will be written to the published document, unless the gures appear i
to print the gure every time you run a script, you should not include a bare print
command in an M-le. Instead, you should use a form of print that sends the output
to a le. It is also helpful to give informative titles to your gures. For example:
waits until the user presses a key before proceeding. Of course, if the recipient of your
M-le is not familiar with pause, then you should include appropriate guidance at
such points; for example, with echo on in effect you could type
% Press any ke
None of the values of b from 1 to 5 yields the desired answer, 1, to 15 digits. Judging
from the output, you can expect to nd the answer to the question we posed above by
typing sinelimit(10). Try it!
A loop species
You can greatly enhance the readability of your M-le by including frequent
comments. Your comments should explain what is being calculated, so the
reader can understand your procedures and strategies. Once youve done the
calculations, you can also add co
at the beginning of a script M-le that creates graphics, to close all gure windows
and start with a clean slate.
As mentioned above, the commands in a script M-le will not automatically be
displayed in the Command Window. If you want the commands to be di
Figure 2.4. The Parabola y = x2 + x + 1 on the Interval [2, 2].
You can modify a graph in a number of ways. You can change the title above the
graph in Figure 2.4 by typing (in the Comman
exp(x) and sin(x)
Figure 2.3. Two Intersecting Curves.
In this section, we introduce MATLABs two basic plotting commands and show how
to use them.
Graphing with ezplot
The simplest way to graph a function of one varia
The second solution can be extracted with x(2) and y(2).
Note that, in the preceding solve command, we assigned the output to the vector
[x, y]. If you use solve on a system of equations without assigning the output to
a vector, then MATL
in the expression. This is done by using the subs command. For example, presuming
that the symbol expression w is dened and it involves the symbolic variable u, then
the command subs(w, u, 2) will substitute the value 2 for the variable u in the
Recovering from Problems
Inevitably, when using any mathematical software system, you are bound to encounter
minor glitches. Even while entering simple arithmetic commands, you may accidentally mistype an entry or inadvertently violate a MATLAB rule. In t
You can also use the Help Navigator to locate the documentation that you will
explore in the display pane. The Help Navigator has four tabs that allow you to
arrange your search for documentation in different ways. The rst is the Contents tab
Figure 1.2. The MATLAB Desktop with Several Commands Evaluated.
scroll by. You can use the scroll bar on the right of the window to scroll back up.
Alternatively, you can force MATLAB to display information one screenful at a time
by typing more on. You p
Figure 1.1. A MATLAB Desktop.
MATLAB 6 has a Desktop, but in older versions of MATLAB, for example
5.3, there was no integrated Desktop. Only the Command Window appeared
when you launched the application. (On UNIX systems, the terminal window from which y
In this chapter, you will start learning how to use MATLAB to do mathematics. We
recommend that you read this chapter while running MATLAB. Try the commands as
you go along. Feel free to experiment with variants of the examples we
describe a MATLAB feature that is available only in the Professional Version, we
highlight that fact clearly.
If you intend to run MATLAB, especially the Student Version, on your own computer,
it is quite possible that you will have to instal
We have already described the MATLAB Command Window and the Help Browser,
and have mentioned in passing the Command History Window, the Current Directory
Browser, and the Workspace Browser. These, and several other windows you will