A Brief Introduction to Matlab
A Brief Introduction to Matlab
Math 446/OR 481
To start Matlab, at the osf1 prompt type matlab. The matlab prompt
looks like this:
>>
Matrices and vectors (which is a matrix with a special form) are
the underpinnings of everything in Matlab. To enter a matrix, type
A=[2 1 -3;5 7 9;0 8 -4]
Notice the rows are separated by semicolons, the entries in a row are
separated by a space. You can also use a comma to separate entries in a
row.
By setting A equal to this matrix you can use the letter A to
represent this matrix. To recall what is in A, merely type A at the
prompt and press enter. You can always change the definition of A by
assigning it to something else.
To perform matrix operations on matrices you use the symbols +, -,
*, and ^. However, if you want to do scalar operations on each
entry in a matrix, you need to use a dot "." in front of these
operations. (Functions like sin, cos, log, exp,
and tan evaluate the corresponding function of each matrix
element.)
To plot the function x squared from 0 to 1 with spacing 0.05,
first define a vector x which is evenly spaced from 0 to 1:
x=[0:.05:1]
Then square each entry in the vector x using the ".*" or ".^"
command:
y=x.*x
or alternatively
y=x.^2
and plot:
plot(x,y)
Here are some basic matlab commands that you will need.
For more details, consult the appendix of the book.
(Note: Matlab is CASE sensitive):
- diary filename This stores a file which is a
verbatim account of your matlab session. If you are using osf1, you
must use this command before you can print your work. The file you
create will be saved in your osf1 directory. If you wish to append
work to this file at a later date just type in diary and use
the same file name.
- diary off The diary file starts at the point where you
insert the command diary and ends where you type diary off.
- lpr THIS IS NOT A MATLAB COMMAND. This is the command
from the osf1 prompt that you use to print your diary file. Exit
the matlab program. At the osf1 prompt, type for example: lpr
-Pst220 filename. Note st220 is the printer in ST I room 220.
At other locations you will use another printer name after the -P.
- pico, emacs, or vi filename THIS IS NOT A MATLAB
COMMAND. Type one of these three at the osf1 prompt to edit a file.
Or use the editor of your preference. You will use this on
assignments to clean up your work and make it more presentable.
- who Displays variables you have defined in your current
session.
- quit or exit either one ends your Matlab session.
- save filename This command will save all variables that
you have defined in the current session. This is useful when you
have entered large data sets that you will want to work with later.
- load filename this command loads the variables that you
saved in a previous session.
- clear Clears all variables from your current session.
Only use this command if you want to lose everything.
- % This is used for comments. Matlab
ignores any line that begins with a %.
- ; If you put a semicolon at the end of a command, the
command will be executed but whatever it creates will not be
displayed on screen.
- uparrow Recalls last thing you typed which can then be edited.
- size(A) Gives the size of the matrix A.
- zeros(2,3) Creates the 2 times 3 matrix of zeros.
- format long Displays all numbers with 15 digits instead
of the usual 4 digits.