CS 101 - Lab 3 Prelab Assignment


Name: __________________________       NetID: ________________      Section#: ______

Print out this page and answer the following questions. Hand in this assignment at the beginning of Lab 3. You should use the material found in Lecture 2 of the course notes and/or Matlab by Pratap Chapter 4.2, 4.3 ,5.3 ,6.1. As with all prelabs/labs, if you are not sure of the answer you may go to any EWS lab on campus and check your results by using Matlab, or you can ask anyone of the CS101 staff for help.

The following exercises should help prepare you for the in-lab activities. Don't forget you can use Matlab Help facilities or the help command if you need help with the following functions.

Part I. Random numbers

    The Matlab function 'rand' generates uniform random numbers that take values on the open interval (0,1), that is, any real (Matlab real) number from zero to one but not including zero or one. The 'rand' function can generate a vector or matrix filled with random numbers. The command
    >> rand(row,column)
    creates a row-by-column array of numbers.

  1. Write a single Matlab command to create a row vector of 10,000 random integers (or logical values) with either the value 0 or 1 (like flipping a coin heads-1, tails-0). You must use the rand function. There are a number of ways to do this, for example: use a relational operator <  or use the floor function.

    x = ____________________________________________________________________________

  2. Write a single Matlab command to compute the arithmetic average of 10,000 random integers generated by the 'rand' function in question #1 above. Use the Matlab mean function and the vector x to compute the arithmetic average. Write the Matlab command and not the answer (approx. .5) that your command returns. Use the Matlab help facility if you have a question about using the mean function. If your mean isn't between .48 and .52 then probably your answer to #1 is probably incorrect.

    _________________________________________________


Part II. Polynomials

    Now you will learn how to represent polynomials in MATLAB. You will also use the roots and polyval functions that were mentioned in your course notes. See lecture 2 slides 31,32.

    MATLAB defines polynomials by a vector of the coefficients of the terms in descending order. For instance, f = 2x + 3 would be represented as f = [2, 3], where the first element of the vector (2) is the coefficient of the first degree term (2 x1) and the second element of the vector is the coefficient of the zero-th degree term (3 x0). Polynomials that do not have a term for each degree of x MUST have a 0 in the vector for that element (e.g., f = 3x3 + 2x is missing the coefficient of the second degree term and the zero-th term, so the vector representation would be f = [3, 0, 2, 0]).

    MATLAB provides several built-in functions that work with polynomials in this form. For example, to find the roots of a polynomial, you can simply call the roots function. (Remember, the roots of a polynomial are the values of x where the value of the polynomial is 0.) 

  1. To find the roots of the polynomial x2 - 25 , we type the following into the command window,

    >> roots([1 0 -25])
    Write the result.


    ____________________________________________________________________________

  2. Write the single MATLAB command to find all the roots of -1 + x100 . (Don't write the roots, just write the Matlab command that gives the roots.)
  3. >> p = ______________________________________________________________
    >>  roots(p)

    Hint: -1 + x100 = -1 +0*x + 0*x2+0*x3+...+0*x99 + x100  (with 99 0's)

  4. Use the polyval fucntion to compute the value of 3x3 + 2x - 1 at x=1, 2, 3, 4, ..., 100 . Write a single Matlab command.

  5. ____________________________________________________________________________

Part III. Miscellaneous functions

  1. Fill in the blank with the result that Matlab gives when you type the following commands.
    >> x = [  2   3   4   6 ];
    >> fliplr(x)
    ans =

    _____________________________________________________

    >> y = [ -1   ;  -2      ;  -3]
    >> [fliplr(y),flipud(y)]
    ans =





    _____________________________________________________

    >> z = [1   2    3    4] ;
    >> cumsum(z)
    ans =

    _____________________________________________________


    >> w = 1:4 ;
    >> cumprod(w)
    ans =

    _____________________________________________________