By the time you complete this lab, you should be able to:
pwd
, mkdir
, and cd
commands.You will also get more practice with
Note: this week's lab is written from the perspective of working on a SunRay.
It is possible to do it from elsewhere, but the instructions do not explain how, so you are better off planning to complete it in lab, or on a SunRay (e.g. in the basement of Smith).
The reason we are writing this week's lab only from a SunRay perspective is to make sure that you
Once we've established that, we'll loosen up a bit, and explain how to work on MATLAB from elsewhere (e.g. from a PC or Mac at home, in your dorm, or in a non-SunRay computer lab.)
You can review the instructions from lab01 if you are not sure how to do this.
During your time at UD, you'll likely end up using your UD Unix account for many classes—not just CISC106. So it is important to keep your files well organized. One of the first steps is to create a directory for those files. In the Unix operating system, a directory is what you may be accustomed to calling a folder under Windows or MacOS—it is a place to store a collection of files, and it has a name.
We are going to create a directory called cisc106
(note the lowercase "cisc
"). This directory will be stored inside our home directory, which is the top level directory for all the information we store on the UD Unix systems.
When you first startup MATLAB, you should be able to find your "home directory" in the places indicated in the following diagram. The one shown in the example below is Phill Conrad's home directory (/home/usra/d9/55560). Yours will be different from that, but still in a similar format:
Figure out what your home directory is, and make a note of it.
Now, in the MATLAB command window, type pwd, like this. The answer that comes back should show you your home directory:
>> pwd
ans = /home/usra/d9/55560 >>
The reason is that MATLAB will typically start you off in your home directory. However, if we keep putting M-files in your home directory, after a while it will get very cluttered. So, it is good practice to create a folder (also called a sub-directory) for each course that you take where you use MATLAB.
To do this, type the following three commands in the MATLAB command window. Note the use of lowercase—in general don't use capital letters in MATLAB except when the instructions specifically indicate to do so! (An explanation of each command follows the example input/output)
>> mkdir cisc106 >> cd cisc106 >> pwd ans = /home/usra/d9/55560/cisc106 >>
mkdir cisc106
, makes a new subdirectory (folder) called cisc106
. This is the folder where you will store all of your labs for this semester.cd cisc106
, changes your current directory to be cisc106
. pwd
, asks MATLAB to print the working directory. Note that the working directory is now your home directory with cisc106 stuck on the end of it.Notice that the places in the MATLAB window where you saw your home directory before (the places circled in yellow above) should now show your working directory with the addition of cisc106
stuck on the end, reflecting that this is now your "working directory". That means that this is where you will find the M-files that you store, for example.
Now that we have a directory called cisc106 under your home directory, each week we will make a separate directory for that weeks lab. We'll call these directories lab02
, lab03
, lab04
, and so forth.
In general, the squiggle (~
), which is more properly called the tilde, stands for your home directory.
So, to
you can use the following sequence of commands:
>> mkdir ~/cisc106/lab02 >> cd ~/cisc106/lab02 >> pwd ans = /home/usra/d9/55560/cisc106/lab02 >>
lab02
directory under cisc106
. cd ..
(that's cd dot dot).cd ~
(that's cd squiggle) In general, the squiggle (~
), which is more properly called the "tilde", stands for your home directory.
cd ~
that means "change directory to my home directory"cd ~/cisc106
it means "change directory to the cisc106 that is immediately under my home directory"cd ~/cisc106/lab02
it means "go into lab02, which is inside cisc106, which is inside my home directory"Try each of these commands. Also try cd ..
after each command to see where you end up. Do this until you are comfortable with what the ~
and ..
mean.
howMuchPizza.m
while in your ~/cisc106/lab02 directoryNow, use the cd
command to change into your ~/cisc106/lab02
directory.
Next, we are going to create a MATLAB m-file to solve the following problem:
Determine how much pizza (in square inches) you get if you order two regular pizzas (12 inch round) vs. 1 x-large pizza (16 inch round).
To do this, we will "edit" a file called howMuchPizza.m
Then, type edit
howMuchPizza.m
The M-file editor window should come up. Put the following MATLAB commands into that file. Change the name in the file to your own, and the section to your own.
% howMuchPizza.m % 09/03/2007 Phill Conrad for CISC106 section 99 % Script to compute how much pizza you get if you % get 2 12-inch pizzas vs. 1 x-large 16-inch pizza regPizzaDiam = 12; regPizzaRadius = regPizzaDiam / 2; regPizzaArea = pi * (regPizzaRadius ^ 2); xLargePizzaDiam = 16; xLargePizzaRadius = xLargePizzaDiam/2; xLargePizzaArea = pi * (xLargePizzaRadius ^ 2); % leave off semicolons to get output to print totalAreaTwoTwelveInchPizzas = 2 * regPizzaArea totalAreaOneSixteenInchPizza = xLargePizzaArea % end of howMuchPizza.m
Once you've typed this in ( or cut and pasted it, and changed the name and section number), try running it. Do you remember how to run an M-file once you've typed it in? If not, check lab01, and/or Chapter 1 of your textbook.
Do the answers that you get seem reasonable? Check them with a calculator, and pencil and paper work.
Once you've verified that the file works, make a diary file to document the work we've done so far. The more we practice these skills, the more they will stick in our brains.
Create a diary file called lab02.txt. (If you are not sure about how to create a diary file, review step 6 of lab01 before proceeding.)
In your diary file, do all of the following:
pwd
to show that you are in the ~/cisc106/lab02 subdirectory (the ~ will be replaced with your actual home directory)ls
to list the contents of the current directory
howMuchPizza.m
file, you might see a file called howMuchPizza.asv
or a file called howMuchPizza.m~
. This is the MATLAB "automatic save" file. Don't worry about it—it's perfectly normal. Also, don't worry if you don't see this file. type howMuchPizza.m
to list out the contents of howMuchPizza.m
diary off
to end the recording. You'll upload both the files howMuchPizza.m
and lab02.txt
to WebCT in the final step of this lab. From time to time, you'll need to copy files from the course web site, and work with them in MATLAB. We are going to practice that in this step of the lab.
The file we want to copy is on the web at the following link:
http://www.udel.edu/CIS/106/pconrad/07F/labs/lab02/compareGrottosPizzas.m
There are several ways we could get the file from there into your directory, but the best way is a direct copy. It turns out that when you are using MATLAB on strauss, you have direct access to the hard drive where files for the UD web server are stored.
In MATLAB, to copy a file directly from one place on the disk to another, we use the copyfile
command.
(Note that the >>
is the MATLAB prompt, not part of the command)
>> copyfile /www/htdocs/CIS/106/pconrad/07F/labs/lab02/compareGrottosPizzas.m ~/cisc106/lab02
This MATLAB command says to copy from the place where this file is stored on the hard drive, into the directory ~/cisc106/lab02
. After you do this command, type ls
and you should see the file in your directory listing, like this:
>> copyfile /www/htdocs/CIS/106/pconrad/07F/labs/lab02/compareGrottosPizzas.m ~/cisc106/lab02 >> ls compareGrottosPizzas.m howMuchPizza.m >>
A few notes:
cp
command (with filename auto-completion), and the Unix wget
command. The file compareGrottosPizzas.m
is related to the problem you worked on in step 1, but it actually solves a different problem, namely:
Determine the price per square inch of a regular pizza (12 inch diameter round) that sells for $8.99, vs. an x-large pizza (16 in diameter round) that sells for $11.99.
(These values come from Grotto Pizza on Main St. in Newark, as of 09/03/2007, and pertain to the regular price for a plain cheese pizza.)
compareGrottosPizzas.m
on the webYou can read through the file compareGrottosPizzas.m
at the following link:
http://www.udel.edu/CIS/106/pconrad/07F/labs/lab02/compareGrottosPizzas.m
Take a moment to visit this link, and look through this file. Notice each of the following things:
%
). Comment lines are ignored by the MATLAB software, but are very important for any human beings (including yourself!) that might read the code. The rest of the file is divided into sections with comments that look something like this.
%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This is a section header % %%%%%%%%%%%%%%%%%%%%%%%%%%%%
These comments have no special "meaning" in MATLAB—since they are lines that start with a percent sign, they are treated just like every other comment (i.e. they are ignored by the MATLAB software, and are there only to help humans make sense of how the code works. However, they do have a special purpose for the human reader.
When an author writes a book, she/he divides it into chapters. The chapters are divided into sections, and the sections are divided into paragraphs. These divisions make it easier to read and understand the book. Imagine how hard it would be to read a book that was not divided up in this way!
In the same way, well-written code is divided up into sections and paragraphs. We often introduce a section with a big fluffy comment like this one, and then use a smaller comment such as this one to introduce a "paragraph" of code
% compute area of both pizzas
assignment statements
, calculations
and output
. This is a typical division into sections for a program. We set up some initial values, we do some calculations, then we output the results. ;
) at the end of it. compareGrottosPizzas.m
in MATLAB You copied the compareGrottosPizzas.m
file in your directory in Step 6. Now that we've gone through the file to understand what it does, the next step is to ask MATLAB to carry out the computations in this file, and show the result.
As you already know, we can do that by simply typing the name of the file (without the .m
), like this:
>> compareGrottosPizzas ppsiRegPizza = 0.0795 ppsixLargePizza = 0.0596 >>
So, we can see that an X-Large cheese pizza from Grottos costs about 6 cents per square inch, while a regular pizza costs about 8 cents per square inch, or about 33% more, square-inch-for-square-inch.
A limitation of this file is that the values in it are hard-coded. That is, this file is only good for calculations involving Grotto Pizza. you cannot change the values without editing the compareGrottosPizzas.m
file itself.
Suppose, for example, you wanted to do a similar calculation for Pat's (on Elkton Rd), or Peace-a-Pizza (also on Main St. in Newark), using the values below:
Menu from Pat's on Elkton Rd. in Newark (retrieved Sep. 03, 2007)
Menu from Peace-a-Pizza on Main St. in Newark (retrieved Sep. 03, 2007)
To use this file to answer questions about Pat's or Peace-a-Pizza, we'd have to make many changes to the file. In fact, we have to edit the file and change it each time we wanted to solve another instance of the problem—Grottos being one instance, Pat's being a second instance, and Peace-A-Pizza being a third instance.
A more efficient way is to create a special type of M-file called a function M-file. A function M-file is one that can take one or more inputs, and produce a result. The nice thing about a function is that once we've written a useful function, we can plug in different values—thus solving different instances of the same problem—without having to do any extra work. That's the subject of lab03.
There is nothing to turn in or save from steps 6 and 7. However, they are important preparation for understanding the work you'll be doing in lab03, so you can't really skip them. If you skip them, you'll end up having to do them later anyway, as you start to work on lab03.
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 lab02). You should submit two files:
Generally submitting everything according to instructions: 10 pts.