CS 221 Fall 2011 Problem Set 3
CS 221 Fall 2011 Problem Set 3
Out: Friday, October 28
Due: 23:59:59 Monday, November 7
Problem 1: Plotting Time Series Data in MATLAB
Time series data occur in a wide variety of settings: signal
processing, sales, traffic patterns, brain scans, manufacturing
processes, investing, and even goals scored in soccer. In this
problem, you will be importing, analyzing, and graphing weekly coal
production in eastern and western Kentucky using data from the
U.S. Energy Information Administration. (Source:
Note that you
could pull this the data directly into MATLAB from the Excel file
on the above EIA site, using the
But to make the project simpler, we have extracted a simplified dataset
called coalprod.csv. ("csv" stands for
"comma-separated values". It is a simple, commonly-used file format
in which values are presented with one row per line, with column
values separated by commas.)
Create a MATLAB script, "coalprod.m" in which you:
Use the "csvread" command to import the data from "coalprod.csv" into
a 500-by-3 matrix called "Data". (See the help file for details on how
to use "csvread" to create this matrix from the file.)
The first column of "Data" will contain the time of each data point as a
fraction of a year. The second and third columns contain weekly coal
production estimates for Kentucky's eastern and western regions,
respectively, in thousands of short tons.
Extract the first column of "Data" and store it in a
500-by-1 matrix (vector) called "Years". (Use the ":" notation for
referring to "all rows".)
Similarly, extract the second
and third columns and store them in arrays called
"Eastern" and "Western".
(Note: These will be column vectors. If you want to
convert them to row vectors, you need to transpose
them. The transposition of a matrix is what
results when you interchange its columns and rows.
In MATLAB, the postfix apostrophe is the transpose operator.
For example, if M is a matrix, M' is the transpose of M. But plot
will work with either row or column vectors.)
Plot the historical production data for Kentucky's two regions using
MATLAB's "plot" command. Note that you can plot multiple datasets with
"plot(X1, Y1, X2, Y2, X3, Y3 ...)". (Hint:
Often you will want to use the same variable to represent X1
and X2. See the help page for details.)
Next, we want to project the trend in the data out through
2014, assuming current trends continue. (Of course, many factors could
affect the actual coal production output.)
First, create a range of time values from 2002 to 2014 in increments
of 0.05 years. Store this as an array in a variable called
MATLAB has numerous curve-fitting tools. You can use the graphing
interface for curve fitting, but for this problem, use the following
code to create your trend lines:
EP = polyfit(Years, Eastern, 1);
WP = polyfit(Years, Western, 1);
ETrend = polyval(EP, TrendYears);
WTrend = polyval(WP, TrendYears);
(See the help files for polyfit and polyval for details. You may also
want to experiment with quadratic or higher level polynomial fits by
changing the "1" in the first two lines, above, with "2", "3",
etc. But in the version you submit, use a linear fit, which is what
"1" gives you.)
Update your plot command to plot the actual and projected data on the
same graph. Use "Years" as the X axis for "Eastern" and "Western", and
"TrendYears", as the X axis for "ETrend" and "WTrend".
Choose a suitable title, axis labels, and other details, following the
guidelines in chapter 5 of your textbook. Save your graph as a JPEG
file called "coalprod.jpg".
Put both your MATLAB script (coalprod.m) and JPEG graph
(coalprod.jpg) in same zipped folder with the files for the other
part of this assignment.
Problem 2: "Smoothing" Data
Sometimes when working with a highly variable time-series of
measurements it is useful to create a "smoothed" version of the
data—that is, a version in which sudden changes have been
"filtered" out. A simple way to smooth a sequence of data points is
to replace each value with a "moving average" obtained
by averaging that value with the values in a "window" around
it. In the simplest case, each value is replaced by the average of
the value and the two values on either side of it. (Note that first
and last values are not changed by this method.)
Write a function smooth() that takes a single m-by-n
matrix (array) A as its only argument, and returns a matrix with the
same first and last rows as A, but with with the values in each row
averaged with the values in the previous and following rows.
So, for example, given this matrix:
A = [ 4, 100, 3, 55;
-1, 0, 57, 22;
76, 13, 78, 33;
88, 45, 101, 50 ]
smooth(A) would return the following matrix:
[ 4, 100, 3, 55;
26.3, 37.7, 46, 36.7;
54.3, 19.3, 78.7, 35;
88, 45, 101, 50 ];
The entry B(2,3) is 46 because A(2,3), which is 57, is replaced by
the average of 57 with the entries in the same column, in the
rows before (3) and after
(78) it. So (3 + 57 + 78)/3 = 138/3 = 46. In general, the entry in
row i and column j of the result, for each i, 1 < i < # rows in A,
is equal to (A(i-1,j)+A(i,j)+A(i+1,j))/3.
Note the following:
Submit your file smooth.m in the zip file as described below.
The returned array will be exactly the same size as the original;
the first and last rows of the returned array must be the same as in
the original. (You may want to initialize the array that will be
returned by copying the entire ouput array into it.)
If the original matrix has fewer than three rows, your function should return
a copy of the original matrix, unchanged. (Use the size() command to
determine the number of rows and columns in the array.)
Your function should print nothing. Every assignment
statement should be followed by a semicolon to suppress output, and
the function should not use any calls to "disp()" or "fprintf()".
To submit your solutions, follow these steps:
- Create a folder titled Last_First_HW2 (replacing "Last" and "First"
with your actual names) on the Desktop.
- Move the 3 files you are supposed to turn in (coalprod.m,
coalprod.jpg, and smooth.m) into that folder.
Nothing else should be in the folder.
- Create a zip file containing the folder and its contents.
course web page has instructions for creating
a zip archive.)
Important: Verify that the contents of the zip
archive are correct. It should contain the directory (folder) and the
three files, and nothing else. (Do this by double-clicking the zip
- Upload the zip archive via the portal, as
Important: save the folder and its contents to a
flash drive or elsewhere, for
two reasons: (i) in case something goes wrong with your submission;
and (ii) we will sometimes build on your solutions in later
- Very Important: save the number the submission
system gives after you upload your submission. It is
the only acceptable proof that you uploaded your file.