Due: Fri, September 26, During your lab session
Worth: 100pts
Remember:
For this problem, you will create directories and move files around, and then show that you did so and draw a picture of what you did.
mv
, ls
, and cd
in your Unix text. Then use the mv
command, as described below, to relocate the m-files and text files you have made in previous labs. For example, circleArea.m
is in your home directory. You can verify this by typing in the shell:
> ls
to list all the files. Sometimes there are too many to easily see. In this case, you can use a
wildcard character. Try this:
> ls *.m
This will show any files in this
directory that end in .m. Similarly, typing ls c* will show all files
starting with the letter c.mv circleArea.m lab02/
cd
command to change into the lab02 directory and verify that your file is there.
Then go back up a directory level (use cd ..
) and finish relocating your lab files.MATLAB provides a function for summing the elements of a vector, but the problem is a good demonstration of traversing (walking through the elements of) a vector with a loop.
Enter your lab03 directory (how?). Copy the file sumInts.m from the class websites lab directory
by using a Unix command (more practice!) in a shell (your xterm on Strauss is a shell):
cp /www/htdocs/CIS/106/tharvey/08F/labs/sumInts.m .
The dot at the end means the directory I am in now so that is where the file is being copied to.
You could also say:
cp /www/htdocs/CIS/106/tharvey/08F/labs/sumInts.m /lab03/
See your Unix text for more information.
Now run the code by calling the function from the Matlab command line (Are you in the right directory? How do you find out?). Use assignment to store the value that the function returns to the interpreter.
Now copy the file sumIntsTest.m from the same directory and look at it. Note that it does not contain a function. This is a MATLAB script file. It contains MATLAB expressions that will be evaluated when you type the filename in the interpreter.
A script M-file is not as nice as a function because you cannot specify parameters, and it shares variable names with the interpreter (how could that cause problems?).
This script M-file is a unit test that is designed to run the examples from the sumInts function. Run the file and see if it works (it should).
Now make four new unit test script files (you should always write the test file first! (why?)) for the functions sumOddInts, sumIntSquares, sumOddIntSquares and sumSquareIndices described below. Then write the functions themselves, and run your tests in a diary file. Note: you must use the type of loop specified.
1) sumOddInts will use a for loop to sum all the odd integers between two parameters called start and finish, e.g. sumOddInts(5,9) -> 21
2) sumIntSquares will use a while loop to sum the squares of the integers from 1 to finish.
3) sumOddIntSquares will use a for loop to sum the odd squares of integers from start to finish, i.e. 16 will be skipped but 25 would be added to the sum.
4) sumSquareIndices will use a while loop to pick out the elements at square indicies (ie. 1, 4, 9, 16, 25, etc) from "start" to "finish" and add sum them. Notice the similarity to HW1.
The function sumOddInts can be correctly written many ways. One common way is to test a number, and add it to the sum if it is odd; another is to start at an odd number, use an interval of 2, and thereby only generate numbers that are odd. Rewrite the function in a new file, sumOddInts2.m, in one of these two ways (but not the same way you did before) and modify your previous unit test file to test the new name.
Type and demonstrate your function in a new diary file.
Here is a chance to receive up to 3 extra credit points.
You must figure out a way to write the function sumSquareIndices using only one line of code. You may not use any loops of any kind.
The shorter, cleaner and nicer your code is the better. All points will be awarded at the discretion of your TA. If you choose to try the extra credit, use the diary command to show that your code produces the correct results. Don't create a whole new M-file. Hand in this diary file in addition to everything else.
At this point you should have:
- 1 drawing of your directory structure (to be handed to TA)
- 5 .m files for unit test functions
- 5 .m files containing the functions you wrote
- 3 diary files
How To Submit: Be sure that you have a printed copy of your five function M-?les, five script M-?les, three diary ?les and a drawing. All must be stapled together, with your name and lab section on the top page. Be sure that you upload a copy of all ?les to Sakai. Then, click submit ONLY ONCE to send these to your Sakai and your TA.
On the first page of every printed copy for this course you must have: your name, section, and TAs name.