This section is an introduction to MATLAB by way of example.
First some screen shots of MATLAB are shown to help you get
oriented to using MATLAB on your computer. Then some simple
MATLAB commands are presented that you are encouraged to try.
The syntax and general use of these commands are described in
detail later.
If you are using a DOS or Unix computer, start MATLAB by typing
``matlab'' at the command prompt. If you are using a Macintosh,
or a computer running Windows, double click on the MATLAB icon.
Refer to the MATLAB User's guide for additional information to
help you get started on your computer.
To stop MATLAB type ``quit'' at the MATLAB prompt, i.e.
>> quit
MATLAB evolved as an interface to a powerful library of linear
algebra routines programmed in FORTRAN. Though it is a very
modern computer package, MATLAB's roots are still evident in the
way that you interact with it. The most basic way to get MATLAB
to do work is to type a command in the Command Window.
NOT FINISHED
MATLAB can be used as an expression evaluator. To do this you
simply type a mathematical expression into the command
window. The command window prompt is >>.
To enter an expression, type it after the prompt (correct
any mistakes by backspacing) and press return. MATLAB
prints the result back to the command window. For example,
to ``evaluate'' pi type
>> pi
MATLAB responds with
ans =
3.1416
pi is a built-in MATLAB variable. The preceding
command is actually a request for MATLAB to print the value
of this variable.
Try the following examples
>> sin(pi/4)
>> 2^(log2(4))
>> sqrt(9)
MATLAB allows you create variables on the fly. To create
a variable just use it on the left hand side of an equal sign.
The following examples show how to assign values to three
variables, x, y and z. It also shows the MATLAB response
to the assignment statements.
>> x = 5
x = 5
>> y = pi/4
y = 0.7854
>> z = y + x^0.25
z = 2.2807
Note that z exists only as a numerical value, not as the formula
with which it was created.
Use the who command to list the currently active
variables. For the preceding session this results in
>> who
Your variables are:
ans x y z
leaving 7433616 bytes of memory free.
To print the value of a variable that is already defined
just type its name at the prompt.
>> z
z = 2.2807
The
variables
section describes how to work with MATLAB variables
MATLAB has
many very powerful built-in functions. As the preceding
example demonstrated these functions include the familiar
set found on many hand-held calculators. Here is just a
tiny sample of other MATLAB functions
You can also create your own functions. To do this you
type MATLAB commands into a plain
text file. The file must have the extension ".m", and
because of this MATLAB functions are often referred to
as "m-files". Creating and using functions is described
in the
Programming Matlab
section.
MATLAB also has some special variables and functions. You have
already encountered the ans variable which is
automatically created whenever a mathematical expression is not
assigned to another variable.
The built-in eps variable is used as a tolerance for
internal calculations. Initially eps is equal to
machine epsilon. You can reset the value of eps, but
this is not recommended unless you are very sure you know what
you are doing.
The realmax, realmin, Inf and
NaN built-in variables are used to handle floating
point exceptions. All numerical variables in MATLAB are
stored as 32 bit floating point numbers. This corresponds
to ``double precision'' on most computers (supercomputers
and some high-end workstations being the exception).
The Inf and NaN values appear if a
floating point exception occurred during the calculations.
In general you should not try to (re)assign the values of any built-in
variables listed in the table below.
MATLAB provides on-line help for all built-in functions and
commands. To browse a list of help topics type
>> help
To get help on a specific topic, for example the cosine function, type
>> help cos
Using help is one way to learn about the variety of
built-in functions. The help command is most useful
when you know the name of the function, but are unsure how to
use it. The MATLAB manuals provide cross-referenced and indexed
descriptions of all aspects of using MATLAB.
In the preceding examples it was useful to have MATLAB print the
results of the calculations. This is not always the case.
MATLAB will print the result of every assignment operation
unless the expression on the right hand side is terminated with
a semicolon. Unlike C the semicolon is not required by
MATLAB syntax. Rather, it is a convenience that allows multiple
statements to be executed without printing the intermediate
results. For example, most lines in m-files end
with semicolons because only the final results of the
calculations are of interest.
To see how the semicolon works enter the following statements
exactly as shown, ending each line with a carriage return. Do
not enter the prompt character, >>.
>> x = pi/3;
>> y = sin(x)/cos(x);
>> z = y - tan(x);
>> z
>> y
The last two lines do not end in semicolons so MATLAB prints
the results of evaluating z and y. The
the values of z and y were assigned in
the y = and z = statements. The last two
lines merely printed the values of the expressions z
and y.
Setting the internal MATLAB path is such a universal concern
that it has its very own page.
If you are just beginning to learn MATLAB the process of setting
the internal path may be confusing. Don't worry about it right away,
but don't forget about it either. There are links throughout these
pages that point you back to the path-setting information.
[Preceding Section]
[Master Outline]
[Section Outline]
[Next Section]