In lecture, I had indicated that in lab07 we would be developing our lab06 software a bit further by
We are still going to do those things—but in lab08 instead of lab07. The reasons are these:
This week's lab provides extra practice with the idea of a "design recipe" introduced in lecture of Monday October 16:
It also introduces a concept that we have covered in lecture, but not yet in lab:
function
(Chapter 5)Before starting this lab:
Create a new subdirectory ~/cisc106/lab07, 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.
There are two ways to copy the files for this week's lab. Read over both methods before deciding which one to use.
/www/htdocs/CIS/106/pconrad/06F/labs/lab07
Assuming that you are already in ~/cisc106/lab07
as your current working directory, the following command will copy all the files you need.
cp
, and right after the *
>> !cp /www/htdocs/CIS/106/pconrad/06F/labs/lab07/* .
>>
There are additional notes about this command at lab04, step 1b if you need to review how this command works.
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.
By now, I'm guessing you already know why. (Look back at lab04 step 2, lab05 step 2, and lab06 step 2 if you still need this pounded into your head.)
In lecture on October 16, we developed two files: areaOfDisc.m and areaOfRing.m to illustrate the Design Recipe idea.
In the lab07 directory, there is one more example: areaOfRect.m
Take a look at this M-file. Note that since it is an M-file that defines a function, to try it out, you can't just type:
>> areaOfRect
Instead, you have to type something like this:
>> areaOfRect(8,3)
That's because the areaOfRect
function expects two input arguments (also called actual parameters). We can see this in the function header, which is on the first line of the M-file:
function area = areaOfRect(length,width)
In addition, I've provided two other M-files, both of which are ordinary M-files (your textbook calls these "script" files). These files don't define a function, so you can just type their names at the MATLAB prompt. These files are:
Try running these files. Then look at the code inside.
The testAreaOfRect.m file is an example of what is called regression testing. Regression testing is used to make sure that as we make changes to software, that we don't accidentally break something (causing it to "get worse instead of better", i.e."regress").
The testAreaOfRect.m file calls the areaOfRect function twice, and checks that the value returned is the value we expect. Note that we use exactly the same examples as the ones in the H1 comment of the areaOfRect.m file. Every time we run testAreaOfRect.m, it tells us whether areaOfRect.m is working properly or not.
The computeAreaOfRect.m
file is an example of an interactive driver. That is, it is a program that asks the user for input, computes a result, and displays the result. But note: the actual computation is done inside the file areaOfRect.m
by the areaOfRect()
function—the formula "area equals length times width" never appears in the computeAreaOfRect.m
file itself.
For a formula as simple as "area equals length times width", you might say that this is overkill—and I wouldn't disagree.
However, this program is meant to serve as an example, and as a teaching tool—to help you see that an interactive driver uses a function you've already written. In later examples, the function will be one that is more complicated, and the benefit of splitting it out into a separate file will be more clear.
Furthermore, once we develop and test the areaOfRect()
function, we can use it with other functions as well.
In fact, in a later stage in this lab, you'll do exactly that—you'll develop a function pricePerSquareUnitRectangle() that takes length, width and price, and computes the price per square unit (e.g. the price per square inch of a rectangular pizza, or the price per square foot of a rectangular apartment or storage unit).
This function will take three input arguments: length, width and price, and produce one output argument, the pricePerSquareUnit. In the body of the function you'll be able to use areaOfRect() to do part of the computation.
You'll also be asked to develop a regression testing file, and a couple of interactive drivers for your new function, similar to the ones illustrated here. So be sure you understand these files before moving to the next step.
Look at the files myMin.m
and testMyMin.m
:
myMin.m
defines a function which computes the minimum element in a vectortestMyMin.m
does regression testing on this function.In this case, there is no interactive test—asking the user to input a vector interactively is beyond the scope of the MATLAB features we've covered so far, so we'll skip that for now.
In a later stage, you'll be asked to develop your own files myMax.m
and testMyMax.m
which are similar to these files, except that myMax.m
computes the maximum element in a vector.
However, we'll be a lot less free with the explanation of how to do Steps 3 and 4! That is for you to figure out on your own. So ask questions at step 2 if you don't understand. Once you do, you are ready to tackle some problems on your own.
Write an M file pricePerSquareUnit.m
, using the "Design Recipe" steps:
When your function is complete and it tests ok, continue to the next step:
Using the example programs testMyMin.m
and testAreaOfRect.m
as a model, write a regression test script for pricePerSquareUnit.m
. Use your examples that you came up with in step 2 of the design recipe.
Refine your M files pricePerSquareUnit.m
and testPricePerSquareUnit.m
until all the tests pass.
Using the example program computeAreaOfRect.m
as a model, write an interactive driver for pricePerSquareUnit.m
.
Important Instructions (deductions will be made for failing to follow these instructions)
pricePerSquareUnit()
to do the computation.Now, write another interactive driver that uses your pricePerSquareUnit()
function to compute the price per square foot of a rectangular apartment or storage room.
Important Instructions (deductions will be made for failing to follow these instructions)
pricePerSquareUnit()
to do the computation.I mention this, because sometimes if something is too easy, students think "I must be doing this wrong". No, this is actually very simple. I'm requiring you to do it anyway—because if you actually do it, it is more likely to sink in.
Use the Design Recipe to design a function myMax()
. This function should take a vector of numbers (row or column) and return the maximum number in that vector. It should operate in a manner similar to the myMin.m
function provided in the lab07 directory, which you may use as a model.
Note that there is a built-in MATLAB function called max()
. You may not use that function to do this assignment—one of the learning goals here is for you to know how to compute a minimum or a maximum when you do NOT have a built-in min or max function already available.
Following the steps outlined previously develop a regression testing file called testMyMax.m to do regression testing on the function you developed in step 4a.
If you aren't sure what to do, review steps 2b, 2c and 3b.
Once the M-files developed for this lab work, create a diary file called lab07.txt
in which you type out each of the M files you developed in steps 3 and 4, and also demonstrate that they work properly.
pricePerSquareUnit.m
testPricePerSquareUnit.m
rectPizza.m
rectRoom.m
myMax.m
testMyMax.m
type pricePerSquareUnit.m
" command first, and then show that pricePerSquareUnit.m
works. Then do the "type testPricePerSquareUnit.m
" command, and then show that it works. Then proceed with the type
command for the third file, etc.You need to submit seven files in all—the six M-files, and then the one diary file lab07.txt.
As the number of files is growing, as part of next week's lab, we'll cover how to create a .zip file containing all the files other than your diary file so that you only have to upload two files: a zip file and a diary file.
But, for this week, unless your TA tells you otherwise, please do not create a zip file, tar file, rar file, or anything else—it will be easier if everyone does the work in a consistent way.
pricePerSquareUnit.m
(20 pts) testPricePerSquareUnit.m
(10 pts) rectPizza.m
(10 pts) rectRoom.m
(10 pts) myMax.m
(20 pts) testMyMax.m
(10 pts) lab07.txt
diary file, correct filenames, etc.) (10 pts)