CS 112
Assignment 5
|
You can turn in your assignment up until 5:00pm on 3/15/12 without penalty, but it is
best to hand in the assignment at the beginning of class. Your hardcopy submission should
include a cover sheet and printouts of five code
files: makeThumbs.m, getFavorites.m, supplyDemand.m, loadData.m,
and
computeEQ.m
. Your electronic submission is described in
the section Uploading your saved work
assign5_programs
folder from the cs112d directory onto your
Desktop. Rename the folder to be yours, e.g.
sohie_assign5_programs
. Set the MATLAB's Current Directory
appropriately.
The assign5_programs
folder contains two data files, mathworks.mat
and widget.xls
, and one code file, displayCurves.m
, to be used for
Problem 2. It also contains a folder named images
that contains some class photos
for Problem 1. For both problems, you will be writing MATLAB functions or scripts
from scratch.
drop/assign05
folder
assign5_programs
folder into your
drop/assign05
folder
assign5_programs
folder from the Desktop by dragging
it to the trash can, and then empty the trash (Finder--> Empty Trash).
When you are done with this assignment, you should have all the
code files stored in your assign5_programs
folder that
you wrote in addition to the files that we supplied for you. After creating
thumbnail versions of the images in Problem 1, you may want to remove the
images
folder from the version of assign5_programs
that
you upload to your personal directory.
assign5_programs
folder in a
subfolder called images
. In this exercise, you'll
write two MATLAB functions to process the set of photos.
makeThumbs
that creates a thumbnail version of each image
in the images
folder and stores the new set of thumbnail images
in a folder called thumbnails
. The thumbnails
folder
should be created at the same level as the images
folder.
In order to write this function, you'll need to be able to keep track
of filenames. Some built-in MATLAB functions that will come in handy:
filesep
, mkdir
, pwd
, and dir
.
The functions imread
and imwrite
can be used to read
images from files and write new images to files.
makeThumbs.m
makeThumbs
should assume that there is a folder called
images
that contains the set of images for processing.
makeThumbs
should have one input parameter, called
sample, that indicates how often the original image should be
sampled. For example, makeThumbs(4)
would sample every 4th
pixel of the original image in the horizontal and vertical
directions to create a thumbnail that is one-quarter the original
size.
images
folder, nor the exact filename of the images. You may, however, assume that all image files
in the images
folder end in '.jpg
'
makeThumbs
should create a new folder called thumbnails
to store the thumbnail versions of the originals. The thumbnail images should have the same
filename as the original images, but with the prefix thumb added in front, as shown
for the Harry Potter image below.
For example, suppose the original images are named:
imagea.jpg, imageb.jpg, etc. After running
your function, there should be a new folder called thumbnails
that contains a set of
images named thumbimagea.jpg,
thumbimageb.jpg, etc.
original image imagea.jpg |
thumbnail image thumbimage.jpg |
After successfully generating your set of thumbnails, delete the original set
of images (delete the entire images
folder). The next function
you'll write (described below) works on the thumbnail images.
getFavorites
that allows
the user to step through the set of thumbnails, indicating the user's
top three favorites. The user may step through the set of images
multiple times before selecting her three favorites.
Your function must display the three chosen images together,
and also write the three favorite images to a
new folder called favorites. In the favorites
folder,
the images should maintain the same filename. For example, if the original
thumbnail image is named thumbPICTURE0001.JPG,
then the favorite image (in the favorites
folder, which should be
at the same level as the images
folder) should also be called thumbPICTURE0001.JPG.
Here is an example of the display of the three favorite images:
And here is a sample run of getFavorites
. Note that this is
just an example. Please add your own personal flavor to your function:
>> getFavorites
Please select your three favorite images
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> f
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> f
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> f
Thanks! Your 3 favorite image(s) are now stored in the favorites folder.
>>
>> getFavorites
Warning: Directory already exists/
> in getFavorites at 14
Please select your three favorite images
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> f
Type n for next image, f to add favorite, q to quit ==> n
Type n for next image, f to add favorite, q to quit ==> q
Thanks! Your 1 favorite image(s) are now stored in the favorites folder.
Notes:getFavorites
above, the user cycled through the set
of 10 pictures more than one time in the selection process
makeThumbs.m
and
getFavorites.m
as well as the thumbnails
and favorites
folders (the original images
folder should be deleted).
A competitive market has many buyers and sellers of the same goods or services, and is described well by the supply and demand model. In this model, the quantity of a good supplied by a producer and the quantity demanded by the consumer both depend on the price of the good. The law of supply says that the higher the price, the more the producer will supply, while the law of demand says that the higher the price, the less the consumer will demand. The diagram below shows simple supply and demand curves that capture these relationships between the price of a good and the quantity that is supplied by the producer or demanded by the consumer. The law of supply and demand says that the market price of the product will settle at the intersection of the supply and demand curves, called the equilibrium point. If the price is too low, then consumers will demand more than the producer is willing to supply, leading to a shortage of the good and willingness of consumers to pay more, driving the price up. On the other hand, if the price is too high, producers may produce more than consumers will buy, leading producers to lower the price. At the equilibrium point, the price of the product is set so that producers sell exactly the same quantity of goods that consumers will buy.
Changes in the overall demand for, or supply of a product can shift the equilibrium point. In the picture below, demand increases from the curve D1 to the curve D2, while the supply curve remains the same. This leads to an increase in the equilibrium price, from P1 to P2.
In this problem, you will create a program that allows the user to explore the interaction between supply and demand for two small markets. The first is the market for a new student version of MATLAB supplied by Mathworks, Inc., and the second is the market for a new widget supplied by J. J. Widget Co. This problem will also give you practice with creating a larger program that integrates multiple code files that implement different aspects of the task. There are four subtasks in this case: (1) loading the data for the supply and demand curves, (2) computing the equilibrium point, (3) displaying the results and (4) controlling the overall process. Task (3) has already been completed for you. Your goal is to write functions to implement tasks (1) and (2) and a script to complete task (4). The functions for tasks (1) and (2) can be written and tested independently.
When designing a large program, a good way to start is to draw a picture of the main components of the program. Write comments in English to describe the information needed for, or supplied by, each component, and the main actions that need to be performed within each component. The picture below illustrates this information for the supply and demand program:
Each arrow links a step where a function should be called to the function description. Some of the comments will translate into a single line of code, while others may require multiple lines of code to implement. The next four sections describe each part of the program in more detail.
Write a function named loadData
that loads in the supply and demand data from
a file and returns the data in four output vectors that store the prices and quantities for the
supply and demand curves. This function should prompt the user to select the source of data that they
would like to analyze. There are two sources of data for this problem, stored in the files
mathworks.mat
and widget.xls
. For the first source, imagine
that Mathworks, Inc. is planning to launch a new student version of MATLAB and conducts a market
survey to determine a reasonable price for this new product. The survey data, shown below,
indicates the expected quantities of MATLAB that students will demand at a set of prices shown
in the first column, for six broad areas of the US. The numerical data in the table is stored
in a variable matlabDemand
in the file mathworks.mat
in the
assign5_programs
folder.
area1 area2 area3 area4 area5 area6 $175 500 540 460 525 575 475 $150 600 650 550 620 660 580 $125 800 845 740 820 880 760 $100 1100 1160 1050 1130 1170 1070 $75 1500 1550 1445 1525 1585 1480 $50 2000 2055 1955 2030 2080 1985
For this data, the total quantity that students will purchase at each price is
the sum of the quantities for the six areas. The variable matlabSupply
in the
mathworks.mat
file stores the data in the following table, which
indicates the quantity that Mathworks, Inc. is willing to supply for each of
the same prices:
quantity $175 7250 $150 6750 $125 6000 $100 5000 $75 3500 $50 1000
Data related to the sale of widgets is stored in the widget.xls
file. You can
use the xlsread
function to load the spreadsheet into a matrix, which will have
three columns. The quantities of widgets for both supply and demand are listed in the first
column of this matrix. The demand prices are listed in the second column and supply prices
are contained in the third column.
Write a function named computeEQ
that computes and returns the equilibrium price
and quantity, given six inputs. The first four inputs should be required: prices for the supply
curve, prices for the demand curve, quantities for the supply curve and quantities for the demand
curve. The last two inputs should be optional: change in supply quantities and change in demand
quantities. Consider developing this function in two steps, where the first step determines the
equilibrium point without allowing a change in supply or demand. One strategy for finding the
intersection of the supply and demand curves is to fit each set of data with a smooth curve such
as a quadratic function, and then determine where these two functions cross each other. Suppose
you want to approximate the supply and demand curves with a quadratic function of the form:
y = ax2 + bx + c
where x represents quantity and y represents price. The built-in function polyfit
calculates the coefficients of a polynomial function of degree n that best fits a given set of
points, and returns a vector of these coefficients. This function has three inputs corresponding
to the x and y coordinates of the points and the degree of the polynomial. The companion function
polyval
calculates the values of a polynomial function given a vector of its
coefficients and a vector of x values. In the following script, polyfit
is used to
derive the coefficients of a quadratic function that best fits a set of points whose coordinates
are given in the vectors x
and y
. These coefficients are then used by
polyval
to calculate values of this polynomial for an evenly spaced set of x
coordinates from 1 to 13. The resulting plot is shown below.
% script to test polyfit and polyval
x = [1 3 7 11 13];
y = [10 4 0 1 2];
coeffs = polyfit(x, y, 2);
x2 = 1:13;
y2 = polyval(coeffs, x2);
plot(x, y, 'b', 'Linewidth', 2)
hold on
plot(x2, y2, 'r', 'Linewidth', 2)
axis([0 14 0 11])
axis equal
You can use polyfit
to compute the coefficients of quadratic functions that
approximate the supply and demand curves, and use polyval
to construct a set of
at least 100 samples of each of these curves over the range of quantities spanned by the
data. The samples of the two curves can then be used to estimate the price and quantity of the
equilibrium point. Hints: At the intersection point, the absolute
value of the difference between the two curves reaches a minimum, and recall that
the min
function can return a second output that is the index of the minimum
value of a function.
At the end of your computeEQ
function, print the computed price and quantity for
the equilibrium point and call the displayCurves
function (described in the next
section) to display the supply and demand curves and the equilibrium point. Note that
the computed intersection may be shifted slightly from the apparent intersection, due to the
quadratic approximation of the curves.
After you complete this first step, then add the ability to change the overall supply or
demand, in order to explore its effect. One way to model an increase in supply or demand
is to shift the original supply or demand curves to the right, reflecting an increase in
the quantities of the product that people are willing to buy at each price, or an increase
in the quantities that the company will sell at each price. Analogously, a
decrease in supply or demand can be modelled as a shift to the left of the supply or demand
curve. There are different ways that the user could specify the amount of increase or
decrease to test. In this problem, the quantities of MATLAB and widgets are on very different
scales (roughly 3000-12000 vs. 0-60). One way to specify an amount to change supply
or demand is to specify a fraction of the maximum quantity in the data. This fraction can
be entered as a number between 0 and 1. For example, the fraction 0.1 (corresponding to 10%),
when multiplied by the maximum MATLAB and widget quantities, would yield 1200 and 6,
respectively. Thus, specifying a fraction of 0.1 for the change in supply or demand would
increase the quantities of MATLAB supplied or demanded at each price by 1200, or increase the
quantities of widgets by 6. A factor of -0.1 would decrease these quantities by 1200 or 6.
Modify your computeEQ
function so that it has two
optional inputs for specifying the fractions to use to change
supply and demand, and use these fractions to adjust the quantities for the supply and demand
curves before computing the equilibrium point. These two inputs can have default values of 0.
The function displayCurves
is provided for you, and displays the
supply and demand curves, with the equilibrium point shown as a black dot. A sample figure is
shown in the next section. This function has six inputs: supply prices, demand prices, supply
quantities, demand quantities, equilibrium price and equilibrium quantity.
This function gives the user the option of keeping the current plots on the
display, or clearing the current display.
Finally, write a high-level script named supplyDemand
that controls the overall
flow of the program. This script should print some introductory information to the user about
what the program does, call your functions to load the data and compute the equilibrium point,
and have a loop that allows the user to specify changes in the supply or demand curves and
observe the resulting changes in the equilibrium point. The following interaction illustrates
one example of how a user might interact with your program - the user's input is shown in bold:
>> supplyDemand Welcome to the CS112 Supply and Demand program! Select a data source, view supply and demand curves, see the equilibrium price and quantity, and explore how these values change with supply and demand :) Select the data to analyze: mathworks (1) or widget (2): 1 Equilibrium price: $116.7817 Equilibrium quantity: $5602.0721 Keep current display? yes (1) no (0): 0 To explore the effects of changes in supply and demand, you can specify the change in supply or demand as a fraction of the maximum quantity present in the current supply or demand curves. Do you want to explore these effects? yes (1) no (0): 1 Change in supply (-0.5 to 0.5): 0.0 Change in demand (-0.5 to 0.5): 0.1 Equilibrium price: $130.3746 Equilibrium quantity: $6066.7372 Keep current display? yes (1) no (0): 1 Explore more changes? yes (1) no(0): 1 Change in supply (-0.5 to 0.5): 0.0 Change in demand (-0.5 to 0.5): -0.1 Equilibrium price: $104.4731 Equilibrium quantity: $5140.041 Keep current display? yes (1) no (0): 1 Explore more changes? yes (1) no(0): 0 >>
You are welcome to modify the text as you'd like, and use a different range of factors for specifying changes in supply and demand. The above interaction resulted in the following display:
Be sure to thoroughly test your code. For your hardcopy submission, hand in the three code files that you wrote, which you can cut and paste into a single file.