MATLAB Tut2

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

MATLAB Summary

MATLAB (matrix algebra)


Matlab is a commercial "Matrix Laboratory" package which operates as an interactive
programming environment. It is a mainstay of the Mathematics Department software
lineup and is also available for PC's and Macintoshes and may be found on the CIRCA
VAXes. Matlab is well adapted to numerical experiments since the underlying algorithms
for Matlab's builtin functions and supplied m-files are based on the standard libraries
LINPACK and EISPACK.
Matlab program and script files always have filenames ending with ".m"; the
programming language is exceptionally straightforward since almost every data object is
assumed to be an array. Graphical output is available to supplement numerical results.
Online help is available from the Matlab prompt (a double arrow), both generally (listing
all available commands):
>> help
[a long list of help topics follows]

and for specific commands:


>> help fft
[a help message on the fft function follows].

Paper documentation is on the document shelf in compact black books and locally
generated tutorials are available and are used in courses.

How to quit Matlab


The answer to the most popular question concerning any program is this: leave a Matlab
session by typing
quit

or by typing
exit

to the Matlab prompt.

Batch jobs
Matlab is most often used interactively, but "batch" or "background" jobs can be
performed as well. Debug your commands interactively and store them in a file
(`script.m', for example). To start a background session from your input file and to put the
output and error messages into another file (`script.out', for example), enter this line at the
system prompt:
nice matlab < script.m >& script.out

&

You can then do other work at the machine or logout while Matlab grinds out your
program. Here's an explanation of the sequence of commands above.

1. The "nice" command lowers matlab's priority so that interactive users have first
crack at the CPU. You must do this for noninteractive Matlab sessions because of
the load that number--crunching puts on the CPU.
2. The "< script.m" means that input is to be read from the file script.m.
3. The ">& script.out" is an instruction to send program output and error output to
the file script.out. (It is important to include the first ampersand (&) so that error
messages are sent to your file rather than to the screen -- if you omit the
ampersand then your error messages may turn up on other people's screens and
your popularity will plummet.)
4. Finally, the concluding ampersand (&) puts the whole job into background.
(Of course, the file names used above are not important -- these are just examples to
illustrate the format of the command string.)
A quick tutorial on Matlab is available in the next Info node in this file. (Touch the "n"
key to go there now, or return to the menu in the Top node for this file.)

MATLAB Tutorial
MATLAB Tutorial (based on work of R. Smith,
November 1988 and later)
This is an interactive introduction to MATLAB. I have provided a sequence of commands
for you to type in. The designation RET means that you should type the "return" key; this
is implicit after a command.
To bring up MATLAB from from the operating system prompt
lab%

you should type matlab


lab% matlab RET

This will present the prompt


>>

You are now in MATLAB.


If you are using the X Window system on the Mathematics Department workstations then
you can also start MATLAB from the Main Menu by selecting "matlab" from the "Math
Applications" submenu. A window should pop up and start MATLAB. When you run
MATLAB under the window system, whether you start from the menu or a system
prompt, a small MATLAB logo window will pop up while the program is loading and
disappear when MATLAB is ready to use.
When you are ready to leave, type exit
>> exit RET

In the course of the tutorial if you get stuck on what a command means type
>> help <command name> RET

and then try the command again.


You should record the outcome of the commands and experiments in a notebook.
Remark: Depending on the Info reader you are using to navigate this tutorial, you might
be able to cut and paste many of the examples directly into Matlab.

Building Matrices
Matlab has many types of matrices which are built into the system. A 7 by 7 matrix with
random entries is produced by typing
rand(7)

You can generate random matrices of other sizes and get help on the rand command
within matlab:
rand(2,5)
help rand

Another special matrix, called a Hilbert matrix, is a standard example in numerical linear
algebra.
hilb(5)
help hilb

A 5 by 5 magic square is given by the next command:


magic(5)
help magic

A magic square is a square matrix which has equal sums along all its rows and columns.
We'll use matrix multiplication to check this property a bit later.
Some of the standard matrices from linear algebra are easily produced:
eye(6)
zeros(4,7)
ones(5)

You can also build matrices of your own with any entries that you may want.
[1

9]

[1, 2, 3; 4, 5, 6; 7, 8, 9]
[1

2 RET

4 RET 5

6]

[Note that if you are using cut-and-paste features of a window system or editor to copy
these examples into Matlab then you should not use cut-and-paste and the last line above.
Type it in by hand, touching the Return or Enter key where you see RET, and check to see
whether the carriage returns make any difference in Matlab's output.]
Matlab syntax is convenient for blocked matrices:
[eye(2);zeros(2)]
[eye(2);zeros(3)]
[eye(2),ones(2,3)]

Did any of the last three examples produce error messages? What is the problem?

Variables
Matlab has built-in variables like pi, eps, and ans. You can learn their values from the
Matlab interpreter.
pi
eps
help eps

At any time you want to know the active variables you can use who:
who
help

who

The variable ans will keep track of the last output which was not assigned to another
variable.
magic(6)

ans
x = ans
x = [x, eye(6)]
x

Since you have created a new variable, x, it should appear as an active variable.
who

To remove a variable, try this:


clear x
x
who

Functions
a = magic(4)

Take the transpose of a:


a'

Note that if the matrix A has complex numbers as entries then the Matlab function taking
A to A' will compute the transpose of the conjugate of A rather than the transpose of A.
Other arithmetic operations are easy to perform.
3*a
-a
a+(-a)
b = max(a)
max(b)

Some Matlab functions can return more than one value. In the case of max the interpreter
returns the maximum value and also the column index where the maximum value occurs.
[m, i] = max(b)
min(a)
b = 2*ones(a)
a*b
a

We can use matrix multiplication to check the "magic" property of magic squares.
A = magic(5)
b = ones(5,1)
A*b
v = ones(1,5)
v*A

Matlab has a convention in which a dot in front of an operation usually changes the
operation. In the case of multiplication, a.*b will perform entry-by-entry multiplic

You might also like