CS100 Fall 1998
Assignment 8
Matlab
Due:
beginning of lecture on November 10Assignments will not be accepted late
Note: You may wish to consult the accompanying Matlab Hints handout in completing this assignment.
Working with others: For this assignment, you may work with one other person. If you do, then you must actually do the work together; sit at a computer together, and make changes together. It is not proper for one person to do the work and then to add someone else's name to it. If you work with someone else, then hand in only one assignment; put both person's name, id and section information on it.
The goal of this assignment: The goal of this assignment is to get you working with Matlab. Try to spend some time experimenting with Matlab and trying out different commands rather than just doing the assignment.
Grading of the assignment: There are five questions. Each is worth 3 points.
What to Turn In: Follow the directions given and turn in a copy of the Matlab command window, any graphs you produce, and all the files you create. The printouts should only show the calculations requested in the assignment; please don't turn in your experiments, false starts, and other work. however, if you make a typo or other small error at some point, you don't need to start over. Just fix the error, and cross out anything you don't want the grader to look at.
Your answers should be in the right order. Please use either Matlab comments (beginning with a % sign) or hand-written annotations to number your answers. The command window printout should include the commands used to produce the various plots.
On-screen Matlab plots normally have light-color graphics on a black background. Printed graphs normally have dark lines on a white background. To avoid wasting ink, stop any printouts that come out light on dark. Then select Output Preferences from the Options menu, click the box labeled "Invert background", click OK, and try again.
Your Assignment
Start up Matlab and do the following problems. Just type the appropriate commands in the command window. For each question, do the assignments and calculations in the order in which they are given. Do not type a semi-colon at the end of any lines unless we tell you to do so. That way the values of the variables and matrices you create will be displayed. Type a return at the end of each line.
1. (a) Print the powers of 2 from 1 to 10, i.e., the values 21, 22, …, 210.
(b) Create a vector x which holds the numbers from 0 to 2*pi in increments of pi/4. (Note that the constant pi
is predefined for you in Matlab.)
(c) Create another vector y, which holds the cosines of the corresponding elements of x.
2. Here are the daily high and low temperatures for Ithaca for the first two weeks of September (from the Ithaca Climate Page maintained by the Northeast Regional Climate Center at Cornell).
Highs: 77 55 56 53 61 64 68 61 60 53 53 58 64 66
Lows: 54 35 38 32 30 30 35 56 48 49 51 43 43 47
(a) Create two arrays hi
and lo
containing the high and low temperatures, respectively.
(b) Calculate the medians of the daily extremes (e.g., the median of the daily highs and the median of the daily lows.) Also calculate the number of days where the daily low hit 32 degrees or lower.
(c) Produce a single graph showing the daily high and low temperatures for these two weeks of September. The high temperatures should be drawn with a '+' at each data point and a line connecting the points, while the low temperatures should be drawn with a 'o' at each data point and a line connecting the points. Put appropriate titles and labels on the graph and print it. (Hint: type help plot
for details about how to graph vectors; at the bottom are listed associated commands for adding labels and titles. You will need to use the hold
command to put multiple plots together.)
3. Evaluate and plot the function f(x) = 4x3 - 2x2 - 3x +1 for values of x from -1 to +1. Be sure to use enough x values over the range from -1 to +1 to produce a smooth graph (100 points or so is probably plenty, but experiment). Put a suitable title on the graph, label the axes with something appropriate, and print it.
4. Create a 2-dimensional matrix M containing these values:
8 3 -2 7
3 0 5 -6
-5 9 7 -1
(a) Print the second column of matrix M. Print the bottom row of matrix M.
(b) Create a 1D matrix (row vector) whose elements are the sums of the columns of M. Create a 1D matrix (column vector) whose elements are the sums of the rows of M. Use the sum
command.
(c) Create a new matrix C that contains the third, first, and fourth columns of M, in that order. Use Matlab operations on M to create N; don't type in the elements again.
(d) Create a new matrix D that has a new row of all ones inserted between the top and middle rows of M. Again, use Matlab operations instead of typing in the elements. You should use the ones
command.
5. (a) Create a new Matlab function (.m file) called even
. The result of a function call even(M)
should be a matrix of ones and zeros the same size as M, with a one everywhere that m contained an even integer. For example, if even
is called on the M from problem 4, the result would be
1 0 1 0
0 1 0 1
0 0 0 0
Hint: Matlab doesn't have a remainder operator like % in Java. But you can divide by 2 and test whether the result is still an integer. You may want to use the round
command.
(b) Test your function on the following matrix N:
4 7 0 -2
3 -3 -1 1.5
0.5 0 -2.5 6