Assignment 05
User-Defined Functions
Contents:
Overview
Topic: User Defined Functions
Related Reading: Ch. 7 and class notes.
For this assignment, you must work individually in regard to
the design and implementation of your work.
Please make sure you adhere to the policies on academic integrity in this regard.
Submit your work via your Git repository.
Problems to be Submitted (20 points)
- (6 points)
MATLAB provides a function mode that computes the mode
of a set of values, that is, the single most frequently occuring
value (in the case of a tie, it returns the smallest value among
those having the maximum frequency). For example, the command
m = mode([5 8 2 9 2 5 7 2 6 5 4]) sets
variable m to the value 2, as it and 5 occurs three times each, with
2 chosen based on the tie-breaking rule. There is a second form
of the command [m n] = mode(V) where
m is the value of the mode and n is its
frequency. For example, the command
[m n] = mode([5 8 2 9 2 5 7 2 6 5 4]) sets
m = 2 and n = 3.
Your task is to implement a function called myMode(v) that has
similar behavior when called upon a vector of values. You are
not allowed to
use the official mode function. However, you may use
the built-in function sort which sorts values of a
vector into non-decreasing order. Once the data is sorted,
finding the mode is equivalent to looking for the longest
consecutive streak of equal values.
- (7 points)
This problem is motivated by a previous assignment in
which we plotted stock prices. Assume that the price for a
given period of time fluctuates between low and
high. We want to display a y-axis with grid lines
so that the entire range of prices is displayed and so that grid
lines are drawn at "nice" values.
For example, when stock prices range from 7500 to 13010, eight grid
lines should be displayed at 7000, 8000, 9000, 10000, 11000, 12000, 13000,
and 14000. Yet when a stock price ranges from 160 to 426, seven
grid lines should be drawn at values 150, 200, 250, 300, 350, 400, and 450.
More formally, we want the following requirements met:
-
Grid lines for a plot must be drawn at even increments
that are multiples of some fixed M, chosen for
the given data set.
-
That value of M must be of
the form c * 10 ^ k
where c is either 1, 2, or 5 and where
k can be any integer.
-
The lowest grid value must be equal to the greatest
multiple of M that is less than or equal to
low.
-
The highest grid value must be equal to the lowest
multiple of M that is greater than or equal to
high.
-
There must be at least 5 grid values and at most 10.
Write a function of the form
v = computeGrid(low,high) that returns a
vector v of the grid values that should be used when plotting
stocks that range from low to high. For
example,
computeGrid(7500, 13010) could return the
vector
[7000 8000 9000 10000 11000 12000 13000
14000] based on increment of M=1000,
whereas
computeGrid(160, 426) could return the
vector
[150 200 250 300 350 400 450] based on an
increment of M=50.
For this problem, you may assume that a solution exists for some
M ≥ 1. That is, you may assume that k ≥ 0 in our
notation. See extra credit for the more general case.
- (7 points)
We have seen the use of the built-in max function, for
determining the maximum of a vector of elements. The function
is more general, including the following two features.
-
In addition to returning the value of the maximum, it can
produce a second output argument that is the
index of where that maximum occurred. The
calling syntax is
[Y I] = max(X);
where Y is the actual maximum and I is
the index at which it occurs. In the special case where
there is a tie for the maximum, the index will be that of
the first occurrence.
-
While the standard behavior applies on row vectors and
column vectors, the max(X) function has a slightly
different behavior when X is a matrix. It
computes the maximum value and occurring index on a
column-by-column basis. For example, when run on the
following matrix
The first output argument is the vector of column-maximums
and the second output is the corresponding vector of indices
Your goal for this problem is to write your own version of this
function titled myMax. You are to ensure that it works
precisely like the official max function on both
one-dimensional vectors and two-dimensional arrays. To test
your implementation, we have written a function that
generates a series of random vectors and arrays and then
compares the output of the official function to the output of
the myMax. You may download it for your own benefit
(testMax.m). A call to
testMax(n) will perform n random trials, returning true
if the test is a success; otherwise, it reports an error and provides
an example of an array that demonstrates the flaw.
Extra Credit (2 points)
Write a version of computeGrid, that works for any positive
values low and high, even cases when the increment may be less than 1
(that is, when k might be negative in the original notation).
For example, computeGrid(3,6) might return
[3.0 3.5 4.0 4.5 5.0 5.5 6.0] based on increment
of M=0.5, while computeGrid(103.48, 103.59) might return
[103.48 103.50 103.52 103.54 103.56 103.58 103.60] based on increment
of M=0.02.