lab05, CISC106, Fall 2006

Overview

This week's lab focuses on if/else from Chapter 3 of the textbook, and for loops from Chapter 4 of the textbook.

Prerequisites

Before starting this lab:

Step-by-Step Instructions

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

Step 1a: Create a new subdirectory for lab05

Create a new subdirectory ~/cisc106/lab05, 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/lab05
  2. If you are working on strauss, copy them directly from the directory /www/htdocs/CIS/106/pconrad/06F/labs/lab05
Copying all the files at once (if you are on strauss)

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

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

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

Step 2: Learning about the if/else command, while loop, and for loop

First: 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 don't skip this step.

You'll look pretty foolish if you end up asking questions about stuff you should have learned by doing this step.

It would also be a shame to miss some easy questions on the exam about stuff you could have learned by doing this step.

So, don't skip it.

Step 2a: Working with the if/else

The if/else is used to make a decision in MATLAB. It allows you to send MATLAB to choose between executing one set of command (the if part), and another set (the else part).

In your lab05 directory, you should find (at least) three files that you copied back in step 1b (there may be others too):

Use the type command or the edit command to look at the contents of these three files.

Also, run them and experiment with them, typing in different values until you are comfortable with how an if/else works.

If you are not sure what to do, check out these reminders:
Now, try this challenge for practice

This is not to "turn in"—it is only for practice.

  1. Copy the file letterGrade.m to letterGrade2.m. Use the Unix cp command to do it, like this:
    >> !cp letterGrade.m letterGrade2.m
    >>
  2. Edit the file letterGrade2.m so that in addition to producing grades such as A, B, C, D and F, it can also produce grades such as A-, B+, etc. Consult the following table (copied from the course syllabus) for guidelines.
  3. Once you've coded the solution, test it out to make sure it works.
grade >= 93 A 73<= grade < 77 C
90 <= grade < 93 A- 70<= grade < 73 C-
87 <= grade < 90 B+ 67 <= grade < 70 D+
83<= grade < 87 B 63<= grade < 67 D
80<= grade < 83 B- 60<= grade < 63 D-
77 <= grade < 80 C+ grade < 60 F

Step 2b: Working with a for loop

Now, notice that there is a file in your lab05 directory called grades.dat. This is a file of grades (made up, not real ones) that you can load into a MATLAB matrix called grades (actually, a vector)

Now try several commands:

  1. Type a MATLAB command that will do that. (If you are not sure how, consult lab03, step 2 for some hints)
  2. Then, type grades (without a semicolon) at the MATLAB prompt, to list out the contents of the grades vector.
  3. Then, type length(grades) at the MATLAB prompt, and see what you get.
  4. Finally, type 1:length(grades), and see what the result is.

Now, use the type or edit command to look at the contents of listGrades.m. This M-file will load the contents of grades.dat, and list the contents one grade at a time, showing both the grade, and the corresponding letter grade. Notice how the M-file uses a for loop and if/else to accomplish its task.

Also notice the use of the fprintf MATLAB function for formatted output. You can read about fprintf on pages 41-43 of your textbook.

Try running the listGrades.m M-file

Experiment with this file. Try modifying it to see if you can make it do something different. Work with it until you are comfortable that you understand how it works, because this week's lab is essentially going to be just like the listGrades.m file, except you'll be working with the katrina.dat file again, and classifying hurricane strength instead of grades.

Your TA and I will freely explain how listGrades.m works if you have questions

However, we'll be a lot less free with the explanation of how to do Step 3! 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 a problem on your own, in Step 3.

Step 3: Write an M-file that outputs a table of Katrina information

Using the file listGrades.m as a model, your job is to write an M-file called lab05.m that

At 18 hours GMT on 08/23/2005, Katrina was a Tropical Depression
At  0 hours GMT on 08/24/2005, Katrina was a Tropical Depression
At  6 hours GMT on 08/24/2005, Katrina was a Tropical Depression
At 12 hours GMT on 08/24/2005, Katrina was a Tropical Storm
At 18 hours GMT on 08/24/2005, Katrina was a Tropical Storm
At  0 hours GMT on 08/25/2005, Katrina was a Tropical Storm
At  6 hours GMT on 08/25/2005, Katrina was a Tropical Storm
At 12 hours GMT on 08/25/2005, Katrina was a Tropical Storm
At 18 hours GMT on 08/25/2005, Katrina was a Tropical Storm
At  0 hours GMT on 08/26/2005, Katrina was a Cat 1 Hurricane
At  6 hours GMT on 08/26/2005, Katrina was a Cat 1 Hurricane
At 12 hours GMT on 08/26/2005, Katrina was a Cat 1 Hurricane
At 18 hours GMT on 08/26/2005, Katrina was a Cat 2 Hurricane
etc...

Use the table at Wikipedia article about the Saffir-Simpson_Hurricane_Scale to come up with your scale for the various categories of storm.

You can use the Weather Underground's Hurricane Katrina Archive to check your results.

Some hints:

Step 4: Create a diary file lab05.txt to show your work.

Create a diary file called lab05.txt in which you type out your lab05.m file, and then test it to show that it works.

This time, one run is sufficient to test your program since all of the program input comes from the katrina.dat data file.

Step 5: Submit two files on WebCT.

The two files you need to submit are lab05.m and lab05.txt. Upload these to WebCT and submit.

That's it for lab05!


Grading

  1. lab05.m (60 pts)
  2. lab05.txt (30 pts)
  3. Generally submitting everything according to instructions: 10 pts.

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