lab08, CISC106, Fall 2006
complete, final version
(new material highlighted)

Introduction

In this lab, we'll practice some more with user-defined functions

This week, we will first continue what we started in lab07, by doing some more practice with creating M-files that define new MATLAB functions, and with scripts that test those M-files.

Then as promised, we'll develop our lab06 software a bit further

We will then use these new functions to develop our lab06 software a bit further by

Overview

We are continuing the "design recipe" ideas introduced in lab07

Other references for the "design recipe" idea:

Also see Chapter 5 in your textbook for information on M-files that define a function.

Prerequisites

Before starting this lab:

Step-by-Step Instructions

Step 1: Preparing your lab08 directory, and copying files you will need

Step 1a: Create a new subdirectory for lab08

Create a new subdirectory ~/cisc106/lab08, and make that your working directory.

If you are not sure how to do this, then review the instructions from lab03, steps 1a through 1d.

Step 1b: Copy the files needed for this week's lab

There are two ways to copy the files for this week's lab. Read over both methods before deciding which one to use.

  1. Download them from the web page http://www.udel.edu/CIS/106/pconrad/06F/labs/lab08
  2. If you are working on strauss, copy them directly from the directory /www/htdocs/CIS/106/pconrad/06F/labs/lab08
Copying all the files at once (if you are on strauss)

Assuming that you are already in ~/cisc106/lab08 as your current working directory, the following command will copy all the files you need.

>> !cp /www/htdocs/CIS/106/pconrad/06F/labs/lab08/* .
>>

There are additional notes about this command at lab04, step 1b if you need to review how this command works.

Step 2: Examining the example programs

As in past weeks, there is nothing to turn in for Step 2.

The work you do in step 2 of this lab is only for practice and learning. There is nothing to turn in from this step of the lab. But it is still important and required.

Step 2a: Yet another example M-file that defines a function: sumUpToK.m

In lecture on October 13, we developed a file called sumUpToK.m. That file is making a reappearance, but now in the context of the "design recipe" idea, along with a script file for doing regression testing on it.

Look at the files sumUpToK.m and testSumUpToK.m. Look at these files and try to understand how they work. Try running testSumUpToK.m. Also try a few of the examples at the command line, to see what the output is, e.g.:

>> testSumUpToK
...


>> sumUpToK([4 2 9 1 3],3)
...

>> sumUpToK([4 2 9 1 3],4)
...

>> sumUpToK([4 2 9 1 3],0)
...

>> sumUpToK([4 2 9 1 3],5)
...

>> sumUpToK([4 2 9 1 3],6)
...

Look inside these two M-files and understand how they work. It is crucial that you understand this before continuing. Ask your TA for help if you don't understand, and/or, come to see the instructor during office hours.

Look next at isLeapYear.m and testIsLeapYear.m. Try to understand how they work. Try a few examples at the command line, and see what output you get.

>> testIsLeapYear
...


>> isLeapYear(2004)
...

>> isLeapYear(1997)
...

>> isLeapYear(2000)
...

>> isLeapYear(1900)
...

Look at the M files also. Note the nested if/else inside of isLeapYear.m. You need to be able to read and understand this code and how it works before going on, because later steps depend on that understanding.

Also, be sure you understand the idea of regression testing (as introduced in Step 2b of lab07)—the idea of writing a separate M-file that defines a script (e.g. testIsLeapYear.m) to test an M-file that defines a new MATLAB user-defined function (e.g. isLeapYear.m). Go back and review Step 2b of lab07 if you are not clear on this point.

Step 2b: cisc106URL.m: another user-defined function

Take a look at cisc106URL.m. This file defines a very useful user-defined function. It is based on the idea of refactoring—that is, taking part of your existing code, and factoring it out into a separate file.

The code inside cisc106URL.m is part of the hurricane.m script that we used in lab06. However, we have parameterized it by specifying a function that consumes a lab number (as a string, e.g. 'lab06' or 'lab08'), along with a jpeg filename, and produces the URL that would be used to retrieve that from the web.

Try running this file on a few examples, as shown below. Note that your output will be different from mine, because it will reflect your userid.

You'll also note the URLs returned are not the ones of any files that actually exist—they are just the addresses of where we would normally place the files on the web if we used our standard naming conventions:

>> cisc106URL('lab08','foo.jpg') 

ans =

http://copland.udel.edu/~pconrad/cisc106/lab08/foo.jpg

>> cisc106URL('lab06','foo.jpg') 

ans =

http://copland.udel.edu/~pconrad/cisc106/lab06/foo.jpg

>> cisc106URL('lab06','plotWind.jpg')

ans =

http://copland.udel.edu/~pconrad/cisc106/lab06/plotWind.jpg

>> cisc106URL('lab08','plotTemp.jpg')

ans =

http://copland.udel.edu/~pconrad/cisc106/lab08/plotTemp.jpg

>> 

Look inside this file and understand how it works.

Step 2c: The tempPlot1.m file: an unsuccessful script

Often you can learn a great deal from looking at a failed attempt at doing something. The testPlot1.m file is an example of this.

First, let's point out that testPlot1.m is not a complete failure. It does illustrate one thing very well:

But, it doesn't do its main job very well, which is to produce a plot of the mean temperature in Wilmington Delaware (from the file sepOctTemps.dat). Try running it, and look at the graph that is produced. It will probably look something like this:

The problem is that we are plotting the "day" field on the x-axis, and the day field "starts over again" at 1 when we move from September into October. So the plot is messed up... the line returns to the left of the graph, and starts over again, plotting from left to right.

Step 2d: The tempPlot2.m file: a more successful script

Instead, consider the file tempPlot2.m.

Note: You won't be able to run this file, because I have NOT provided you with a complete working version of the function ordinalDay.m, which is needed!

But you can look at how it works, and see an example of the plot it produces (shown below):

Notice a few things:

Clearly, this file is meant to illustrate several improvements that you can make to your lab06 file. That's what the rest of this lab is about. But first, we have to write and test the function ordinalDay.m

 

Step 3: Writing the ordinalDay function (ordinalDay.m)

Look at the "stub" for ordinalDay.m, and the file testOrdinalDay.m

I've provided you with a "stub" for ordinalDay.m. This is a file that has the first two steps of the design recipe worked out.

I've also provided you with testOrdinalDay.m, which can be used to complete step 4 of the design recipe.

If you run testOrdinalDay.m now, it will show that all six tests fail.

>> testOrdinalDay
Results for ordinalDay:
0 tests passed, 6 tests failed
>>

Your job is to carry out step 3, and fix the body of ordinalDay.m so that all six tests pass.

Write the body of the function, and test it. When all the tests pass, you know you are done!

The idea of the ordinalDay function is this: given a month, day and year, determine what day of the year that is (the so-called ordinalDay). For example:

You can test this function in three ways:

  1. by typing test cases at the MATLAB prompt (e.g. ordinalDay(12,31,2006) and seeing what the result is
  2. by running the testOrdinalDay.m regression test script
  3. by running the tempPlot2.m script and seeing if the resulting plot looks like the one shown above (in step2d).

When your function is complete and it tests ok, continue to the next step:

Step 4: Write a improved version of lab06.m (call it lab08.m)

Step 4a: Copy your lab06.m file to lab08.m under your lab08 directory

You can use either a unix command to copy from lab06/lab06.m file to your lab08/lab08.m (you need to be in your cisc106 directory to do that), or you can use MATLAB's file browser, and do a "Save As".

Step 4b: Change your lab08.m file to use the new functions cisc106plot.m

Change the references inside the file from lab06 to lab08. Make sure it still works.

Then take out all the detailed steps needed to produce the plot, and replace those with a simple call to the cisc106plot() function. When you do this, be sure that the filename you pass into the cisc106plot() function has the .jpg extension. The MATLAB strrep() function may be useful as you go about this.

Step 4c: Add some code to make sure the wind line doesn't touch the edge of the graph

Use the myMin.m and myMax.m functions you defined in lab07

Compute the min and max of the wind vector, and set the edges of the graph to be 10 mph lower then the min and 10 mph higher than the max, using the MATLAB axis function, as illustrated above in step 2.

Step 4c: Use ordinalDay to make your elapsedTime vector accurate even when spanning multiple months

The idea here is to construct an elapsed time vector that reflects the number of hours that have elapsed from the first line of the file. You can do this by first computing two intermediate results:

You can then multiply elapsed days by 24, and add it to elapsed time (which in the case of a negative value, is actually a subtraction), to compute the elapsed time.

Step 5: Create a diary file lab08.txt to show your work.

Your diary file should contain the following:

  1. a listing of your ordinalDay.m function
  2. a listing of your testOrdinalDay.m script file (the regression test)
  3. a run of your testOrdinalDay.m script file (it should pass all tests for full credit)
  4. a listing of your lab08.m file
  5. at least three runs of your lab08.m file. Be sure that at least one of these is on a file with data that spans multiple months

Be sure that the URLs that appear when you run your lab08.m file link to files on the web that are readable, and that contain valid graphs of the results from that particular hurricane. Also be sure that the edges of the plot don't touch the top or bottom of the graph. This should be because of the code you added in step 4c, not because of a "careful choice of data files". :-)

Step 6: Upload and Submit your work on WebCT

Upload and submit the following files:


That's it for lab08!

Due Date:

Due 11:55pm Thursday November 16, with NO LATE PENALTY for submissions by 11:55pm Friday November 17.

Penalties for late submissions:

 


Grading Rubric

 

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