Lab02, CISC106, Fall 2006

Goals

By the time you complete this lab, you should be able to:

  1. Manage directories in MATLAB with the pwd, mkdir, and cd commands.
  2. Convert simple mathematical formulas into MATLAB assignment statements
  3. Use the input function in MATLAB to ask the user for a value, and initialize a variable

You will also get more practice with

  1. Saving a sequence of MATLAB commands into a file (a so-called "dot-M" file)
  2. Running existing .m files
  3. Using the diary command to save your work into a file you can submit for grading
  4. Submitting your saved work for grading using "MyCourses" (also known as WebCT).

Step-by-Step Instructions

Step 1: Startup MATLAB on a SunRay
(note: this week Step 1 MUST be done on a SunRay)

You can review the instructions from lab01 if you are not sure how to do this.

The reason this step MUST be done on a SunRay is that we need to make sure you know how to access your home directory on strauss (the system where MATLAB is running), and also how to create subdirectories using MATLAB.

Most of the remaining steps in this week's lab can be done on a PC or Mac version of MATLAB if you prefer.

Step 1a: Identify your "home directory"

When you first startup Matlab, you should be able to find your "home directory" in the places indicated in the following diagram. Mine is /home/usra/d9/55560. Yours will be different from that, but still in a similar format:

Figure out what your home directory is, and make a note of it.

Now, in the MATLAB command window, type pwd, like this. The answer that comes back should show you your home directory:

>> pwd
ans = /home/usra/d9/55560 >>

The reason is that MATLAB will typically start you off in your home directory. However, if we keep putting M-files in your home directory, after a while it will get very cluttered. So, it is good practice to create a folder (also called a sub-directory) for each course that you take where you use MATLAB.

Step 1b: Create a cisc106 subdirectory under your home directory

To do this, type the following three commands in the MATLAB command window. Note the use of lowercase—in general don't use capital letters in MATLAB except when the instructions specifically indicate to do so! (An explanation of each command follows the example input/output)

>> mkdir cisc106
>> cd cisc106
>> pwd

ans =
/home/usra/d9/55560/cisc106
>>

Notice that the places in the MATLAB window where you saw your home directory before (the places circled in yellow above) should now show your working directory with the addition of cisc106 stuck on the end, reflecting that this is now your "working directory". That means that this is where you will find the M-files that you store, for example.

Step 1c: Create a lab02 subdirectory under the cisc106 subdirectory

Now we'll do the same thing again to move down one more level, into a directory for lab02. Do the following:

>> mkdir lab02
>> cd lab02
>> pwd

ans =
/home/usra/d9/55560/cisc106/lab02
>>
  

Step 1e: Learn about the squiggle (the tilde), which means "home directory"

In general, the squiggle (~), which is more properly called the "tilde", stands for your home directory.

Try each of these commands. Also try cd .. after each command to see where you end up. Do this until you are comfortable with what the ~ and .. mean.

Step 1d: Edit an M-file called lab02a.m while in your ~/cisc106/lab02 directory

Now, use the cd command to change into your ~/cisc106/lab02 directory. Then, type edit lab02a.m

The M-file editor window should come up. Put the following MATLAB commands into that file. Change the name in the file to your own, and the section to your own.

% lab02a.m   P. Conrad, for CISC106 lab02, Fall 2006, section 013
% Convert from kilometers per liter to miles per gallon


litersPerGallon = 3.7854118;
kmPerMile = 1.609344;

kmPerLiter = input('Enter kilometers per liter:');
milesPerGallon = kmPerLiter / kmPerMile * litersPerGallon;
message = [num2str(kmPerLiter) ' km/liter = ' ...
           num2str(milesPerGallon) ' miles/gallon' ];
disp(message);  
   

Once you've typed this in ( or cut and pasted it, and changed the name and section number), try running it. Do you remember how to run an M-file once you've typed it in? If not, check lab01, and/or Chapter 1 of your textbook.

To understand how this M-file works, you may want to read over p. 29 and 30 in your textbook, specifically the part that starts with the heading "Initializing Variables With Keyboard Input". This explains the third line, namely:

 kmPerLiter = input('Enter kilometers per liter:');  

The rest of the program should be pretty straightforward—it is essentially similar to the program we did in lab last week.

To check whether the program is calculating accurate results, type in a value of 1 (for 1 liter per gallon), and check the result against the following table:

http://encarta.msn.com/media_701500646/Imperial_and_Metric_Conversion_Factors.html

(As it turns out, according to the web site http://www.teaching-english-in-japan.net/conversion/mpg, Europeans don't actually use km/liter to measure fuel economy, but rather a measurement called liters/100km.

I don't know first hand whether that is true or not—but either way, it makes an interesting MATLAB exercise. (Note: After I first wrote this, TA Timo Koetzing verified with first hand knowledge that this is true in Germany.) In the second part of this lab, you'll have an opportunity to develop a MATLAB program to convert from liters/100km to miles/gallon. But we are getting ahead of ourselves—first let's make a diary file to document the work we've done so far.

Step 1e: Create a diary file called lab02a.txt

Create a diary file called lab02a.txt. In your diary file, do all of the following:

Step 2: Writing your own M-file

Now, still in the ~/cisc106/lab02 subdirectory, write your own M-file, called lab02b.m. This file should convert from liters/100km to miles per gallon.

Think about the units, and whether you need to multiply or divide.

Use the program above as a guide, and modify it as needed. Change not only the lines of MATLAB code, but also the comments at the top of the file.

Then make a script (i.e. a diary file) called lab02b.txt. This script should do all the same steps you did in lab02a.txt, except for your lab02b.m MATLAB program, instead of the lab02a.m MATLAB program.

(Note: I may sometimes use the word "script" to refer to a MATLAB diary file. For some folks, the word "script" might refer to an M-file. Both of these are reasonable usages of the word script, so it might be confusing. If you encounter the word "script", and are not sure whether it means diary file or M-file, you may want to ask the person using the word what they mean by it.)

Special note for PC and Mac users

While Step 1 of this week's lab MUST be done from a SunRay, you may do step 2 on any system, including a PC or Mac.

If you do use a PC or Mac version of Matlab for step 2...

You should still create a cisc106 subdirectory, and a lab02 subdirectory under the cisc106 directory, and do your work for step 2 in that directory. However, cisc106/lab02 may be under some other directory instead of your "home directory—perhaps your C:\ drive, for example, if you are working on a Windows machine with a single hard disk.

Step 3: Submit your saved work for grading using "MyCourses" (also known as WebCT).

Log on to MyCourses (WebCT) and find CISC106 (if it is not listed, tell your TA, and email your instructor!)

Follow the instructions for submitting an Assignment in WebCT (submit this as lab02). You should submit four files:

  1. lab02a.m (M-file from part 1)
  2. lab02a.txt (diary file from part 1)
  3. lab02b.m (M-file from part 2)
  4. lab02b.txt (diary file from part 2)

Generally submitting everything according to instructions: 10 pts.

And you are all finished!


Grading

  1. lab02a.m (M-file from part 1) 10 pts
  2. lab02a.txt (diary file from part 1) 20 pts
  3. lab02b.m (M-file from part 2) 40 pts
  4. lab02b.txt (diary file from part 2) 20 pts

Generally submitting everything according to instructions: 10 pts.

End of lab02 for CISC106, Fall 2006 (100 pts)