Matlab is a tool for modeling, prototyping, and investigating computational and mathematical systems. It is an interpreted computer language with lots of support for user interfaces, graphics, and mathematics built-in. What's more, it's extensible and (almost completely) platform-independent, so that you can easily incorprorate others' systems and experiment with them. If you already know matlab, skip to the next section. There are two large matlab documents in /cs/cs153/matlab/docs: using_ml.pdf is a complete introduction to all aspects of matlab (it's a big file!) and buildgui.pdf details the construction of graphical user interfaces in matlab. In addition, matlab has extensive documentation within the program itself. Just type "help" at the prompt.
You may run matlab wherever you wish. It exists on wilkes (not on
turing) as well
as on the lab machines in B105, or you can install it on your home
computer (see http://www.hmc.edu/cis/servers/suppsoft/licensing.shtml#matlab
for details). The files we will use are in /cs/cs153 on
turing/wilkes, so you may need to copy files to your computer if you
choose to run matlab there (if you can't directly access the cs
filesystem).
The following is the "one-minute" introduction to matlab.
To start matlab (on wilkes, at least), type
% matlabNote that you must have
/usr/local/matlab_classroom/bin/matlab in your path. You
will see a window pop up briefly and then matlab prompt will appear in
your terminal window:
>>(Because matlab uses your terminal window for its prompt, it will complain if you try to run it in the background.)
Just to be sure you know matlab is working, see if it can answer important questions like
>> 40+2If it can handle this, handling images should be no problem. You might want to try
>> 40+2;The semicolon at the end of a statement is not a terminator, but suppresses matlab's printing of the evaluation of the expression. Typing
>> quitwill get you out of matlab.
Variables work as you may expect them to in matlab.
>> x = 42;followed by
>> x * 0will convince you that the system has indeed remembered
x's value.
The variable ans always holds the result of the last computation.
Matlab variables are case-sensitive.
If you want to clear the value of a variable you can use
>> clear xThe command
>> clearclears all variables. If you want to check up on your variables, try the commands
>> who >> whos
A big reason for using matlab is that it has considerable support for vector and matrix manipulation. For example, you can define a vector and a matrix as follows:
>> x = [ 1 2 3 ]This declares
x to be a 1 by 3 row vector.
>> A = [ 1 0 1 ; x ]This declares
A to be a 2 by 3 matrix. Note that
the semicolon here indicates that you want to start a new row.
Also, matlab lets you embed matrices within one another, as long
as their dimensions work out.
The transpose of x is
>> x'and multiplication is as you might guess
>> b = A * x'If your dimensions don't coincide, matlab will warn you. It will let you do something like
>> x + 5(componentwise), however.
>> helpTo get more detailed information about one of the topics, try
>> help generalwhich provides yet more topics. Finally
>> help pathgives you a matlab "manual page" about the
path
command (where matlab will look for files). You may also want to set your
MATLABPATH shell variable to add your direcctories
to your search path. Notice that since
lots and lots of things in matlab are global variables (after all,
this is a prototyping system), you may need to be careful about what you name
your own variables and files. And, speaking of files,
It's usually easiest to store commands in files and run them from there. There are two basic types of matlab "m" files: scripts and functions. As an example script, type the following into a file. To do this, you'll need to use a text editor outside of matlab -- like vi, (on second thought, perhaps not one not like vi).
x = 5; y = 10; answer = y*x - y + y/xName this file
test.m . Then if you type
>> testat the matlab prompt, you will find the answer you sought. If matlab complains about not being able to find the file, then you put it in a directory not in its search path (the current directory is in the search path by default). Either move it to matlab's current directory, change matlab's current directory, or set the
path variable. You can get help on
path, cd, or pwd.
The second kind of m file is a function definition. Here's an example: type
function z = fun(x,y) % z = fun(x,y) computes x to the y power a = x^y; z = a;into a file named
fun.m and then try
>> fun(3,5)The
% is matlab's one-line comment character.
The comment in the above file is actually used by the help
system. Type
>> help funto remind yourself what it does in case you forget... .
Note: there was no formal code to write or problems to solve in this section of the assignment.