Assignment 05
User-Defined Functions


Contents:


Overview

Topic: User Defined Functions
Related Reading: Ch. 7 and class notes.


Assignment Parameters

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.

Please submit your work via email to dferry_submit@slu.edu.


Problems to be Submitted (20 points)

  1. (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.


  2. (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:

    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.


  3. (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.

    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.


Originally by
Michael Goldwasser