MA/CS 371 - Lab 0
Introduction to Computing with MATLAB
Section 1: Introduction
Make sure you have the following materials. Hard copies will be
available in lab. Please do not print a copy of these or any other
materials in the lab - the printer in the lab is intended for the use
of the lab staff and TAs.
The goal of this lab is to introduce you to:
- Netscape, a World Wide Web browser (a Web browser
allows you, the computer user, to search through and display
information made available by other users)
- The UNIX operating system (an operating system
allows you, the computer user, to give instructions to the machine)
- The vi text editor (a text editor allows you to
enter information or data into the machine and allows you to store,
retrive, and edit that information)
- MATLAB, an interactive numerical software package
This lab is mainly for new users who have not had previous experience
with the computer environment in the CS department. Users who are
already familiar with a Web browser, UNIX, a text editor, and MATLAB
may skip to Section 6 (shipping the files for this lab into your
account) and from there to the extra credit problems. I'd appreciate
it if you would move to a computer in the back of the lab if you do
so. Users who are familiar with everything except MATLAB are
encouraged to do the commands in Section 6 and then play around with
MATLAB while the rest of the class catches up to you. Read the
MATLAB Primer, start up MATLAB (just type 'matlab' at your UNIX
prompt), and maybe write some small programs or type 'demo' at your
MATLAB prompt and play around.
Keep in mind that this is a problem-solving course. It is
not primarily a course on how to program in MATLAB. You are
responsible for learning how to use MATLAB sufficiently well to deal
with the problems presented in future labs. If you are not
comfortable with programming in MATLAB after this lab, please take the
time to become comfortable before next week. Seek help from your
classmates or the TA if necessary.
Please read this lab thoroughly. New users are strongly encouraged to
complete the practice problems as a measure of how well they have
learned basic UNIX and commands.
Section 2: Lab Policies and Etiquette
- All programming must be done in MATLAB.
- Lab work is due at the end of the lab session. I will typically
publish a lab by the Tuesday before it is due in case anyone wants to
work ahead. I will grant extensions for no later than the following
Monday morning to people who either ask me ahead of time or attend the
lab session and ask me there. If you do not attend a lab and have not
made arrangements with me beforehand, you will not be able to receive
credit for that lab.
- When I am speaking to the entire class, please pay attention and
refrain from typing and using your mouse.
- Do not look at another user's computer files without first asking
that user's permission. It is not only bad manners but can be a
felony in this state to access another user's files without
- You may discuss the lab problems with your classmates but you must
write all of your own MATLAB code. You must make a serious effort to
solve a problem yourself before discussing it with your classmates.
Any discussion beyond a quick question or two must be acknowledged;
comment your code by adding the name(s) of those with whom you have
- Please do not use the printer in the lab. See me if you need a
hard copy of any material for any lab.
Section 3: Logging in for the first time and the X Window
In this section you will learn how to "log in" to a workstation and
invoke X Windows.
Note: Whenever you type a command at the prompt, you must
conclude the line by hitting the Return key.
- To start, you should see a prompt with the name of the machine
followed by "login: ". If the screen is blank, hit Return or
move the mouse around to get the login prompt to appear. Make sure
the computer and monitor are both switched on.
- At the login prompt, type in your username. At the resulting
"Password: " prompt, type in your password. Your password will not
appear on the screen as you type it. Note that UNIX is case sensitive
- capital "A" and lower-case "a" are different characters.
- After about 10 seconds, the machine will ask you what printer you
wish to use. Respond by typing
- For this time only, type the following command exactly (including
the tilde and the slashes):
The above command will copy some files for your account to make it
easier for you to use. This command will take a short while to
process. If any messages appear (other than your next UNIX prompt),
ask for help before proceeding.
After the UNIX prompt re-appears, type
which will log you out.
- At the login prompt, repeat step 2. You should see:
Last login: ...
Xwindows? (^C to interrupt)
From now on the machine will go automatically into X Windows when
you log in.
The screen should now go black or speckled and "windows" will slowly
start to appear - a clock, a mailbox, and then four rectangular
windows. The whole process may take half a minute.
- The mouse is the white thing on the silver pad to the
right of the machine. You will see a cursorsomewhere on the
screen that moves around on the screen when you move the mouse. The
cursor usually looks sort of like a capital I or a crosshairs. The
way your windows work is that you can only type into a window when the
cursor is inside it.
Section 4: The Netscape Web Browser
If you are not already doing so, at this point you should start up the
Netscape Web browser and view this file. If you've never used
Netscape before, type in the exact string below:
The first thing that will happen is you will get a copyright
disclaimer box on your screen. (If all you get is a ghostly-looking
rectangular border, move the cursor until the border is where you want
the new window to go and press the left mouse button.) Press the
'accept' button. After your computer's hard drive churns around for a
little while, you should see another empty rectangular border on your screen.
Position this box in a
convenient part of your screen and press the left mouse button.
Netscape will then open its window on that spot.
Navigating in Netscape is fairly simple and intuitive. Use the scroll
bar on the right to move around on the current document. Clicking on
any of the underlined items will open up the page that they "point"
to. Find the entry in this document for "Lab 0", click on it, and you
should see this lab assignment!
In the future I'm not going to make a lot of hard copies of the lab
assignments, so you should get used to reading the lab assignments on
the computer with Netscape. It's the wave of the future.
Section 5: The UNIX Operating System
In lab we will briefly discuss some basic UNIX terminology (files,
directories, pathnames) and the most frequently used UNIX commands.
Refer to the Useful UNIX and vi Commands
handout for more detailed explanations.
Section 6: Shipping files for this lab
When you did the setup command in Section 3, a directory called
cs371 was created in your home directory. Now it is time to
make a subdirectory for this lab and copy some files from the cs371
account into it. Do the following and don't forget the period at the
end of the fourth command:
cp ~cs371/lab0/* .
Section 7: The vi editor
The way you will often get information (especially MATLAB programs)
into the machine is to type it in using an editor. If you
don't know how to use any UNIX text editor, you will learn how to use
vi (the name stands for visual.) vi has two
modes (ways of interacting with you), the command mode and
the insert mode. In the insert mode, the characters you type
appear on the screen as part of the file. They are "inserted"
into the file. In the command mode, the characters you type cause
things to happen to the information already in the file. It is
important that you understand the difference. When you enter
vi, you are automatically in the command mode. Once you
get into the insert mode, you can return to the command mode at any
time by depressing the Esc key (top left of the keyboard). The
letter "i" is one of several letters that allow you to enter
the insert mode from the command mode.
The best way to learn vi is to use it, so let's edit a file
that contains my favorite poem.
The file raven should now be displayed on your screen.
Remember that you are in the command mode within this window.
Please pause here. As soon as everyone is caught up, we will go
through some vi commands together. Meanwhile, look through the first
three sections of the Useful UNIX and vi Commands
handout and review what you have done so far in the lab.
- Move to the left window.
- Invoke vi on the file containing my favorite poem by typing
Section 8: Running MATLAB and using M-files
At this point, invoke MATLAB by typing matlab at your UNIX
prompt. It might be a good idea to exit vi in your main window
and use the main window to run MATLAB.
As MATLAB starts up, it flashes a little 'splash screen' onto your
screen, and then will show a header and a MATLAB prompt which looks
That prompt is the sign that MATLAB is ready for input.
At this point, bring up vi on the sample MATLAB file in your
directory by doing
Note that all regular MATLAB programs have the '.m' file extension,
just like C programs use '.c' and FORTRAN programs use '.f'.
file mach_eps.m is an example of a MATLAB script file. If you
type mach_eps at your MATLAB prompt, each line from the file
mach_eps.m will be passed into MATLAB and treated as if you had typed
it at the command line. Note that each line beginning with a percent
sign (%) is a comment and has no effect. (You can read more about
MATLAB script files and MATLAB function files in Sigmon's MATLAB
Go ahead and type mach_eps now. MATLAB should report the
??? value = 1:
Missing operator, comma, or semi-colon.
Error in ==> /a/duncan/jade/homes/cs371/lab0/mach_eps.m
On line 11 ==> value = 1:
On simple programs, MATLAB usually does a pretty good job of pointing
out where an error occurred. In this case, the line
value = 1:
is not a valid MATLAB command.
At this point, go into your vi window, edit that line so
that it is a valid MATLAB command setting the variable value to
1 without displaying anything, and make sure to save the changes to
mach_eps.m. Now go ahead and type mach_eps at your MATLAB
prompt again. You should get the output:
Let's review the material that you will be expected to know after this
- How to log in
- Simple UNIX commands and terminology - files, directories, copying
and moving files, navigating around in your file system
- Viewing documents with Netscape
- Editing files with vi or some other editor
- Running MATLAB and simple MATLAB terminology (essentially the
contents of Sigmon's MATLAB Primer)
- How to edit a MATLAB program (an "M-file") in one window and run
the current version on MATLAB in another window
- How to log out
Appendix: Logging out
Be sure at the end of each lab session (and every other time you use a
public UNIX machine) to log out! If you don't, an unscrupulous user
could destroy all your files, send e-mail to people masquerading as
To make sure you log out safely at the end of a session, you should do
At this point your computer's hard drive will chatter and X Windows
will clean up and exit. Our accounts are set up so that you should be
logged out at this point. Make certain that you see the
"cetus login:" prompt before walking away from the computer! If you
see a UNIX prompt on the screen, type logout to finish logging
This week's questions will not be handed in or graded, but you should
be sure you can answer them all. If you have any difficulty with the
questions you should take the time to practice before next week's lab.
- Exit all application programs (things like MATLAB, your text
editor, Netscape, and so on). Remember to save changes whenever
appropriate - for example when exiting vi.
- Move your cursor to a spot on the "background" - in other words, a
place on your screen where there is no window. Your cursor should
look like a fat letter "X" when it's over the background.
- Press and hold the left mouse button. A pop-up menu should appear
with "twm" at the top and "EXIT X WINDOWS" at the bottom.
- Pull the mouse down, while holding down the left button, until
"EXIT X WINDOWS" is highlighted.
- Release the mouse button.