lab03, CISC105, Fall 2007

Goals

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

  1. Describe the concept of variables, assignment statements, and type
  2. Demonstrate how to write test scripts for MATLAB functions and MATLAB function M-files for problems with simple formulas.

You will also get more practice with

  1. Managing directories in MATLAB with the pwd, mkdir, and cd commands.

You'll also review a few account setup items at http://www.udel.edu/network

  1. Checking your default login shell, and setting it to either /bin/tcsh or /bin/bash instead of /bin/csh
  2. Checking your default group, and making sure it is one the one with the most "strauss dollars" in it.
  3. Requesting a disk quota increase if you are eligible for one.

lab03 can be done from a SunRay, or from a PC or Mac

The instructions are generally written from the standpoint of working on a SunRay.

However, you can do them from a PC or Mac. Advice on how to do that appeared in lab03—review the instructions there is you are not clear on how to proceed.

Step-by-Step Instructions

Step 1: Some housekeeping items at http://www.udel.edu/network

Our first step this week is to take care of three basic housekeeping items related to your Unix account:

  1. Checking your default group, and making sure it is one the one with the most "strauss dollars" in it
  2. .Checking your default login shell, and setting it to either /bin/tcsh or /bin/bash instead of /bin/csh
  3. Requesting a disk quota increase if you are eligible for one.

Step 1a: Setting up your default group

On the SunRays, bring up a web browser. (You should have learned how to do that in lab01. If you are not sure, return to the instructions for lab01.)

Now, access the web page: http://www.udel.edu/network. On that page, type in your UDelNet username and password.

You should see a screen that looks something like the following.

UD network page

Click where it says "Change your Unix Default Group". That should take you to the following screen:

UD network page change group

On the example screen above, one of the items on this page is listed as CISC181010-18, which means CISC181, sections 010 through 018.

On your screen you should see several groups, one of which is a group for CISC105 or CISC106. Change your default group either to that one, or whichever one has the most money in it.

In the drop down box shown, select the group, and click submit. You are now done with this step, which you should only have to do once per semester. However, there is still some important stuff to read.

Some stuff you should know about "strauss dollars"

Hopefully the dollar amount listed for CISC105 or CISC106 will be something like $300 for you (in my case, it is $54,000, which is the total for everyone in the course.) We'll talk about the "money" in a minute; for now, just know this: it's not real money. It's just pretend money used to track computer usage. You wont get a bill for it. Ever.

Let's come back to that $54,000. Because this example is from the instructor account, the "dollar amount" is huge. Your dollar amount as a student will be much smaller, but that's ok. The dollar amount will go down throughout the semester as you use strauss. It should not get below $50. If it does, contact your instructor immediately: he/she can request more "dollars" for your account. You can track your dollar amounts remaining by typing "chdgrp" when you are logged into strauss.

Step 1b: Choosing your default login shell

Now we need to change your default login shell. Click where it says "return to the main page". Then click on the option for "Change your Unix Login Shell". That should take you to the following screen:

change login shell to tcsh

If your current Unix Default shell is csh, you should change it to tcsh by selecting /bin/tcsh from the drop down box, and clicking submit. You can then logout of the UDel Network page. This gives you command line editing (the ability to recall commands with the up and down arrows), as well as nifty things like file name completion (all of which your instructor will demonstrate in lecture.)

If your current Unix Default shell is tcsh, you don't need to do anything; you can log out of the UDel network page.

If your current Unix Default shell is bash, then you are probably already a confirmed "bash" user. The following paragraph is for you. Everyone else can ignore it.

Prof. Conrad writes: is is my intention to eventually migrate this course to "bash" rather that "tcsh". I acknowledge that "bash" is a better shell to teach.

That being said, you might be better off using tcsh for the time being. The reason has to do with the "path" variable you need to get to the compilers. We've tested the commands to access them from tcsh, but not yet from bash. So consider switching your default login shell to tcsh, or "typing in" tcsh after you login to strauss each time. The remainder of this lab, and all further labs (until future notice) will assume tcsh. Until I figure out the new environment as it relates to bash, if you use bash (or any other shell other than tcsh), and your commands don't work, you are on your own to fix it.

If your current Unix Default shell is anything else, then read the paragraph above about bash, because it applies to you as well. Please considering switching to tcsh for the time being.

Step 1c: Requesting a disk quota increase if you are eligible

Next, go to the main menu of http://www.udel.edu/network again, and select "Request a Disk Quota Change".

The screen that comes up next should look something like this. What you should do is request the maximum quota increase for which you are eligible. You want to request an increase that will get you up to the number listed under "Max allowed without IT Approval.". This number will vary from student to student, from semester to semester. In the past, it has been as small as 10, and as large as 200.

A "reason for request" is generally not required unless you exceed the amount that doesn't require IT approval (requesting more beyond this amount is not recommended unless you have a very good reason that does not relate to this course.) But if you want to put in a reason (or if the form is changed in the future to require it) just type in CISC105 or CISC105.

Once you've made the change to your quota, you can log out of the http://www.udel.edu/network screen.

Changes are not immediate

Note that all three of the changes you made above on the http://www.udel.edu/network screen take overnight to take effect. But no worries—you can continue immediately with the rest of the lab anyway.

Step 2: Understanding variables and assignment statements

Three of the most important concepts to understand in MATLAB—and indeed in all of Computer Science—are the concepts of variables, assignments, and types. We'll start first with the idea of variables

We are familiar with the idea of a variable from algebra. Typically, the idea of a variable is introduced by saying something like this:

The concept of a variable in Computer Science is similar, but it is also a bit different. In Computer Science, a variable typically represents a storage place in memory—in the RAM of the computer.

Suppose, at the MATLAB prompt, we type:

>> x = 5; 

What happens when we type x = 5; is that MATLAB sets aside some memory for the variable x, and stores the value 5 in that memory location.

The statement x = 5; is called an assignment statement or more simply, just an assignment.

An assignment statement assigns a value to the variable x.

In MATLAB, if you type an assignment statement for the variable x, and the variable x does not already exist, then MATLAB will create a new variable with that name, and set aside memory for it.

A side note:

Algebra vs. Computer Science, and the meaning of  x = x + 1

So, here's one difference between variables in Algebra, and variables in Computer Science.

In Algebra, if you write:

x = x + 1

what you have is an unsolvable equation. This is no value for x that will make this equation valid.

However, at the MATLAB prompt, if you previous typed in:

>> x = 5; 

and now you type in

>> x = x + 1

the result is as follows:

>> x = x + 1

x =

     6

>> 

How to make sense of an assignment statement such as  x = x + 1

Step 3: Understanding the idea of type—the MATLAB whos command

Every variable that you create in a programming language has

The type of a variable can be seen by typing the MATLAB command whos. The following MATLAB session illustrates this. Try typing these commands yourself:

>> x = 5

x =

     5

>> word = 'five'

word =

five

>> whos
  Name      Size            Bytes  Class     Attributes

  word      1x4                 8  char                
  x         1x1                 8  double              

>>

What we see in the output illustrates the difference between an assignment statement for a numerical variable (e.g. x=5) and one for a variable containing text (e.g. word='five') .

The variable word contains text, so its type is char, which is short for character. Each of the letters f, i, v, and e are considered to be one character each. The size column shows us that word is a 1x4 matrix of characters, those characters being f, i, v, and e,

The variable x contains a number, and its class (type) is double . The double type is used to represent most numbers in MATLAB. The name double may seem a little odd, until you understand that back in the day, memory was scarce. Here's a more detailed explanation

A key point to understand is that if you put something in single quotes, it is treated as char, even if it happens to be a number.

So for example, if we type y = '1743'; and then type whos the output will show y as a 1x4 char variable.

But if we type y = 1743; and then type whos in that case y will show up as a 1x1 double variable.

Try this for yourself!

Note: Although there is nothing to turn in for this step, there could be an exam or quiz question based on the material in this section. So, don't skip it!

Step 4: Create a ~/cisc105/lab03 subdirectory, and copy this week's files

We've covered how to do this in previous labs, but here is a quick way to do it at the Unix prompt. Of course, if you are doing this lab from a PC or Mac, you always start at the Unix prompt. But if you are on a SunRay, we haven't yet covered how to get the Unix prompt. So, here's how.

Bringing up a Unix prompt on the SunRays

To bring up a Unix prompt on strauss on the SunRays, you can

Your Unix prompt may look like any of the following, or even something different.

>
%
strauss.udel.edu%  

Creating the ~/cisc105/lab03 directory

Note that because of the use of the ~ at the start of each path, this particular sequence of commands works no matter what your current directory is.

strauss.udel.edu% mkdir -p ~/cisc105/lab03
strauss.udel.edu% cd ~/cisc105/lab03
strauss.udel.edu%

Copying this week's files

Last week, we showed how to use the copyfile command inside of MATLAB. This week, we'll use the Unix cp command to copy the files at the Unix prompt. Remember the "space dot" at the end of the command that signifies that you are copying files into the "current directory".

strauss.udel.edu% cp /www/htdocs/CIS/105/haggerty/07F/labs/lab03/* . 

After typing this command, do an ls command to see the new files you copied into your directory. They should match the files listed at the web link lab03 under the labs directory for this semester.

Step 5: Remind yourself what you did in lab03

In lab03, you worked with a couple of function M-files, and with a couple of test scripts for function M-files. To refresh your memory:

Review of some differences between Script M-files and function M-files.

There are actually many differences, but here are a few. We'll learn more as we go through the course.

  1. Function M-files always start with the word function as the very first thing on the very first line of the M-file.
  2. The rest of the first line of a function M-file is always something like this.

         function area = areaOfPizza(diameter)


    Here's a breakdown of what's on this line:
  3. To run a function M-file, you need to type the name of the function M-file (without the .m), and then a set of parentheses after the name (), usually with one or more actual values for the arguments inside (also called actual parameters).
  4. Script M-files do NOT start with the word function.
  5. To run a script M-file, you just type the name of the script M-file with the .m.

A few more points:

Step 6: Writing your own function M-file (given a test script)

In this step, we will provide you with a test script, and you will write the function M-file that goes with it.

The problem we want to solve is to calculate the distance between two points in the Cartesian plane.

(If this doesn't ring any bells, try the following links for an explanation:

Under the lab03 folder, you'll find a file called testDistance.m. If you try running this test file, you'll see that you get an error:

>> testDistance
??? Undefined function or method 'distance' for input arguments of type 'double'.

Error in ==> testDistance at 24
actual = distance(1,5,-2,1);

>> 

Your job is to produce a correct implementation of the distance function as an M-file, distance.m. You can see the links above for the mathematical formula).

Follow the example of the function M-files from lab03. Be sure to include, the following in this order

Once you've finished this file, try testing it by running the test script at the MATLAB prompt. When it works, you can move on to step 7.

Note: You'll record your work on this step in a diary file that you create at step 9.

Step 7: Writing a test script for an existing function M-file

Why you should write the test script before writing the function M-file

We strongly advocate the "test-first" approach to development, where you write the test script before you write the function M-file.

Real world experience has shown this method to be beneficial for at least two important reasons:

Fine. But sometimes its too late—the function M-file already exists!

Sometime though, you join a project that is already in progress, and there are already some function M-files lying around. Your job might be to make changes to those files. This is where a regression test script comes into play.

As we mentioned before, a test script can be used for both acceptance and regression testing. Regression testing means we don't want the software to get worse as we make changes.

So, we might be called upon to write a test script for a function M-file that already exists. Here's a simple recipe for doing that.

How to write a test script, quick and dirty

The easiest way to create a test script for a function M-file is to take an existing test script and modify it to suit your purposes.

One of the files you copied into your account from the lab03 directory is this one: slope.m—this file is a function M-file for calculating the slope of a line.  You also should have the file testDistance.m that we used in a previous step—this file is a test script for the distance.m function M-file.

We are going to make a new file called testSlope.m, which is initially a copy of testDistance.m.

At a Unix prompt, type this to copy the file testDistance.m to testSlope.m:

>> cp testDistance.m testSlope.m  

Now, use either the MATLAB editor or the emacs editor to edit the file testSlope.m

Follow these instructions to turn the contents of testSlope.m into a proper test script for the slope.m file:

  1. The first two lines read as follows

    % testDistance.m   test distance.m
    % P. Conrad for CISC105, sect 99, 09/15/2007
    

    Change these lines as appropriate. For example, instead of testDistance.m, make the filename be testSlope.m, and where it indicates that it is testing distance.m, indicate that it is testing slope.m. Also, change the name, date, class and section to your name, date, class and section.

  2. Now, find the part that defines the tolerance.
  3. Next, find the two lines that assign the actual and expected values for test 1, i.e.
    actual = distance(1,5,-2,1);
    expected = 5;
    
        

    Replace the right hand side of the assignment statement actual = distance(1,5,-2,1); with a call to the function you are testing, and the value of expected, with the value you expect. For example:

    actual = slope(1,1,9,5);
    expected = 0.5;

    Be sure to end both of your assignments with semicolons.

  4. Now do the same for the second test. For this one, you'll need to come up with your own sample slope and expected values.
  5. A special note about coming up with your expected values:

  6. Ok, now you are ready to test—the remainder of the test script can pretty much stay the way it is. By now, you should know how to run the test script. If you get any errors, adjust the script until it works properly.
  7. Then, use the "evil programming trick" from lab03 to be sure that the test script itself is working properly. That is, insert a line in slope.m that says result = -42; after the line that calculates the correct formula for slope. Use this to make sure that the test script is working properly—that is, that when the function M-file is wrong, the test script tells you so!
  8. Once you are assured of this, change the slope.m file back to the way it was.

Note: You'll record your work on this step in a diary file that you create at step 9.

Step 8: Writing both a test script and a function M-file

Up to this point, we've been holding your hand and keeping the training wheels on.

We've done all of the following:

Now, its up to you. Show us what you've learned.

Here are three sample problems. Choose one of them, and create both a function M-file, and a test script to go along with it.

Choose one of these three problems to work on for step 8:

For some example results, see http://www.udel.edu/sportsinfo/womens_swimming/stats07.pdf

Note: this function calculates results only in seconds. Traditionally, paces are reported in the format mm:ss.ss. For example, e.g. 2:07.38 was a winning time in the 200 meter butterfly in the results file linked to above. Later this semester, We'll cover how to convert a numerical result in seconds into this format (either as a character string, or as a 1x2 matrix). For now, just report a time of 2:07.38 as 127.38 seconds.

    problem
title
problem
description
formula
(1) Music Frequencies in a well-tempered scale

Given a starting pitch, e.g. A=440hz, and a number of half-steps up or down from that starting pitch, calculate the resulting frequency, using a well-tempered scale.

The formula uses a constant step, which is the "twelfth root of two." This is the factor you multiply by for each half step up or divide by for each half step down. (note that negative exponents result in division automatically)

inputs: p, starting pitch in hertz; h, half-steps up or down
output:
f, resulting frequency
constant: s, the twelfth root of two, i.e. (2 1/12)

For more information, see: http://members.cox.net/mathmistakes/music.htm

f=p(sh)
(2) Sports Pace

An athlete is running a timed course of a certain distance. (as in track, luge, cycling, swimming, etc.) Given the total length or the course, a split time in seconds, and the distance traveled so far at that split time, calculate the total time required to complete the course, assuming the athlete stays at the same pace.

inputs: l, total length of course in meters; d, distance traveled so far in meters (split distance);
s, split time
output: t, total time to complete course at current pace

 

= ls/d

 

(3) Carnival Rides
(Also, Math & Physics)
Ride
Speed

On some carnival rides, riders travel in a circle facing forward. These rides are often variations on the theme of a carousel, but might go considerably faster and involve seats that swing out on ropes or arms. A question arises—at what speed are you traveling around the circle? Given the radius of the circle in feet, and the period (how long it takes to make one rotation, in seconds), calculate the speed around the circle in miles per hour.

inputs: r, radius in feet; p, period in seconds
output: s, speed in mph
constants: 5280 feet per mile, 3600 seconds per hour


s = (2πr/p)(3600/5280)

 

If you want to get a good grade...

Follow these tips for success:

More Important reminders:

That last point—doing your own work—is so important, it deserves some extra commentary.

Don't get by with a little help from your friends
Do get by with a little help from your TA and instructor!

If you need help on part 8, here's how to get it.

Do:

Don't:

Some additional words about academic honesty...

There really is no good reason for folks to inappropriately collaborate when so much help is available—and indeed, when the way to tackle the problem has been spelled out for you so clearly.

But later on, the problems may get tougher, and you may get busier, and you may find it more difficult to make time in your schedule to see your instructor or TA. So the temptation may arise to cut corners, and work together on problems where you haven't been specifically allowed to do so.

So be warned: we compare code. We compare code by hand, and we also use software to compare code. It raises "red flags" when several folks all make the same mistake, or have the same formatting of their code. It also may raise red flags when seven people at random all choose the same problem from among three choices (which is very unlikely), and make a similar mistake that no-one else made.

The red flags get redder when a cluster of mistakes unrelated to each other conceptually appears on the same group of papers.

It can also raise red flags when we notice that folks in a cluster of similar papers tend to do any of the following:

In cases where code is identical, or so similar that is it highly unlikely to have arisen by random chance, the presumption is usually that the code was electronically copied from one person to another.

So it is in your best interest to choose different problems to work on, work independently, and don't let other students have access to your code (i.e., don't leave printouts laying around, don't leave yourself logged in), etc.

When you've finished step 8...

When you've successfully written both your test script and your function M-file, and your function M-file passes the tests, you are ready to create a diary file of your work on lab03 and submit!

Step 9: Create a diary file of your work

Now, create a diary file of your work with the name lab03.txt. This file should be inside your ~/cisc105/lab03 directory.

If you are not clear on how to create a diary file, check back with the instructions in lab01.

In your diary file, do all of the following:

  1. Enter the pwd and ls commands (on separate lines) to show your current directory, and list your files.
  2. Enter the command type testDistance.m to show the contents of the testDistance.m file you copied in step 4
  3. Enter the command type distance.m to show the contents of the distance.m file you created in step 6
  4. Enter the command testDistance to invoke the test script on your distance.m file.
  5. Enter the command distance(0,0,3,4) at the MATLAB prompt to show one more test of your distance.m function M-file.
  6. Enter the command type slope.m to show the contents of the slope.m file you copied in step 4
  7. Enter the command type testSlope.m to show the contents of the testSlope.m file you created in step 7
  8. Enter the command testSlope to invoke the test script on your slope function M file.
  9. Enter the command slope(0,0,3,4) at the MATLAB prompt to show one more test of the slope.m function M-file.
  10. Enter a command to type out the test script M file you created in step 8.
  11. Enter a command to type out the function M file you created in step 8.
  12. Enter a command to invoke the test script you created in step 8
  13. Enter a command to perform one more test on the function M-file you created in step 8.
  14. End your diary file.

Once you've created the diary file, use the Unix command more lab03.txt to examine the diary file and make sure that the diary file contains all the necessary steps.

Step 10: Submit your saved work for grading using "MyCourses" (WebCT).

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

Follow the instructions for submitting an Assignment in WebCT (submit this as lab03). You should submit seven files.

Yes, all seven files

Please note that you MUST submit all seven files even though the diary file (lab03.txt) contains a listing of the other files. Here's why:

So, points may be deducted if you don't submit according to those instructions.

I do realize that submitting seven files is a bit of a pain. Starting next week, we'll learn a technique for submitting multiple files all at once—we'll learn how to create a zip file that contains multiple .m files in a single .zip file. That allows us to only submit one file to WebCT.

Here's a list of the seven files you need to submit for lab03

  1. lab03.txt
  2. testDistance.m
  3. distance.m
  4. testSlope.m
  5. slope.m
  6. the test script M-file you created in step 8
  7. the function M-file you created in step 8

And you are all finished!


Grading

file what we're looking for points
opening lines of lab03.txt (pwd and ls) did the student do it?
is the student in the correct directory (~/cisc105/lab03)?
5
testDistance.m file should be present, and unmodified from original
(if modified in a way that affects correctness, make deductions as appropriate)
 
distance.m file should be correct (5 pts) and well commented (5 pts) 10
portion of lab03.txt for distance problem the testDistance file should show the tests passing (5 pts) and there should be one more test (5 pts) 10
slope.m file should be present, and unmodified from original
(if modified in a way that affects correctness, make deductions as appropriate)
 
testSlope.m file should be correct (5 pts) and well commented (5 pts) 10
portion of lab03.txt for slope problem the testSlope file should show the tests passing (5 pts) and there should be one more test (5 pts) 10
the test script M-file you created in step 8 file should be correct (10 pts) and well commented (10 pts) 20
the function M-file you created in step 8 file should be correct (10 pts) and well commented (10 pts) 20
portion of lab03.txt for slope problem the testSlope file should show the tests passing (5 pts) and there should be one more test (5 pts) 10
overall following of directions student should submit all work according to directions given 5
Total    

Due: 09/27/2007
End of lab03 for CISC105, Fall 2007 (100 pts)