By the time you complete this lab, you should be able to:
You will also get more practice with
pwd
, mkdir
, and cd
commands.You'll also review a few account setup items at http://www.udel.edu/network
/bin/tcsh
or /bin/bash
instead of /bin/csh
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.
Our first step this week is to take care of three basic housekeeping items related to your Unix account:
/bin/tcsh
or /bin/bash
instead of /bin/csh
. 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.
Click where it says "Change your Unix Default Group". That should take you to the following screen:
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.
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.
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:
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.
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 CISC106.
Once you've made the change to your quota, you can log out of the http://www.udel.edu/network screen.
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.
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:
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. There 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 >>
x = x + 1
x
, which is 5, and we add 1, resulting in the value 6. ans
, the "answer variable". 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
single
precision, and double
precision.double
precision used twice as much memory as single
precision.double
), you could have more accurate resultssingle
precision for most calculations, and double
only when extra accuracy was needed.double
is almost always used. 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!
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.
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%
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 ~/cisc106/lab04 strauss.udel.edu% cd ~/cisc106/lab04 strauss.udel.edu%
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/106/pconrad/07F/labs/lab04/* .
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 lab04 under the labs directory for this semester.
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:
areaOfPizza(10)
, and MATLAB would return a number representing the size of a 10 inch pizza in square inches.2 * areaOfPizza(10)
to determine how much pizza (in square inches) you get if you order two 10 inch pizzas. There are actually many differences, but here are a few. We'll learn more as we go through the course.
function
as the very first thing on the very first line of the M-file.
function area = areaOfPizza(diameter)
function
.area
,=
(called the assignment operator) areaOfPizza
(diameter)
.()
, usually with one or more actual values for the arguments inside (also called actual parameters). areaOfPizza.m
M-file, we can type areaOfPizza(12)
12
is the actual argument to the function12
is the actual value that will be assigned to the variable diameter
and used in the calculation of the result. testAreaOfPizza
A few more points:
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 lab04 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
function result = distance(x1, y1, x2, y2)
%
followed by the name of the function, and a one line description of the functionreturn;
end
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.
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:
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.
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 lab04 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:
The first two lines read as follows
% testDistance.m test distance.m % P. Conrad for CISC106, 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.
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.
A special note about coming up with your expected values:
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!Note: You'll record your work on this step in a diary file that you create at step 9.
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.
Note that in each case, we've stated the problem in terms of single letter variable names such as p>, f, s, l, and so forth—and, we've stated the formulas in those terms as well. This is how formulas are often stated in math and science textbooks. However, in most cases it is not good programming practice to use single letter variable names. We'll see some exceptions to this rule, but for this problem, avoid single letter names.
Instead, use the names given in the M-file headers indicated below.
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. inputs: p, starting pitch in hertz; h, half-steps up or down 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); 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. |
t = 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 | s = (2πr/p)(3600/5280) |
Follow these tips for success:
Write the test script first—before you write the function M-file.
Hand calculate the test cases (see the "a special note" box earlier in this lab on that subject)
Do a great job both on the function, and on the comments. Don't fall into the trap of thinking that since the comments are "ignored by the computer" that they not important. They are not ignored by the instructor or the TA—and we are the ones that give you your grade!
On the contrary, the comments exist only for the instructor and the TA. Leaving them out (or doing a mediocre job on them) sends the message: "what I have to say to the computer is important, but what I have to say to the TA and instructor are not". That's downright rude when you think about it! It is not in your best interests to be rude to the folks giving you a grade.
The equivalent in the real world is: the comments are there for your co-workers and your boss. You will have to spend 40+ hours with your co-workers, and your boss determines your raise. Don't skimp on the part of the code that is "just for them". Instead, take time and care with it—and you'll make lots of helpful friends.
That last point—doing your own work—is so important, it deserves some extra commentary.
If you need help on part 8, here's how to get it.
Do:
Don't:
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 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 lab04 and submit!
Now, create a diary file of your work with the name lab04.txt. This file should be inside your ~/cisc106/lab04 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:
pwd
and ls
commands (on separate lines) to show your current directory, and list your files. type testDistance.m
to show the contents of the testDistance.m file you copied in step 4 type distance.m
to show the contents of the distance.m file you created in step 6testDistance
to invoke the test script on your distance.m file. distance(0,0,3,4)
at the MATLAB prompt to show one more test of your distance.m function M-file.type slope.m
to show the contents of the slope.m file you copied in step 4type testSlope.m
to show the contents of the testSlope.m file you created in step 7testSlope
to invoke the test script on your slope function M file.slope(0,0,3,4)
at the MATLAB prompt to show one more test of the slope.m function M-file.Once you've created the diary file, use the Unix command more lab04.txt
to examine the diary file and make sure that the diary file contains all the necessary steps.
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 lab04). You should submit seven files.
Please note that you MUST submit all seven files even though the diary file (lab04.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.
And you are all finished!
file | what we're looking for | points |
---|---|---|
opening lines of lab04.txt (pwd and ls) | did the student do it? is the student in the correct directory ( ~/cisc106/lab04 )? |
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 lab04.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 lab04.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). Note, the first line should match the function M-file header given in the table at step five (-4 pt deduction if it doesn't). |
20 |
portion of lab04.txt for step 8 | the test script should show the tests passing (5 pts) and there should be one more test typed in interactively(5 pts) | 10 |
overall following of directions | student should submit all work according to directions given | 5 |
Total |