CS101 - Lab Activity 3

Name: ____________________ NetID: ______________ Section#: ______


Objectives

In this lab, you will:


References


Part 1: Standard MATLAB Functions

Section I. Random numbers



  1. Complete the MATLAB command to create a row vector of 10,000 random integers with either the value 0 or 1 (like flipping a coin heads-1, tails-0). You must use the rand function.


    x = floor ( .5 + ___________________________________ );


  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.

    _________________________________________________


  3. Fill in the three blanks below to complete a MATLAB command to create a column vector of 500 random real numbers with a value between -2*pi and 2*pi.


    y = ______________ + _____________ * rand(_________________________________) ;

    (You can test your code by typing min(y) and max(y). These values should lie close to -2pi and 2pi respectively.)

        Section II. Polynomials

  4. To find the roots of the polynomial 2x3 + 15x2 + 6x - 1, we type the following into the command window,

    >> roots([2 15 6 -1])

    Write the result.





    ____________________________________________________________________________

  5. Write the single MATLAB command to find all the roots of -x + x100 . (Don't write the roots, just write the MATLAB command that gives the roots.)

    >> p = __________________________________________________
    >> roots(p)

    Hint: x100 - x = x100 + 0*x99 + 0*x98+ ... + 0*x3 + 0*x2 - 1*x + 0. Create a vector [ 1 (followed by 98 0's) -1 0]. Use linspace or zeros function to create the 98 0's.

  6. Use the polyval function to compute the value of x3 + 2x2 + 3 at x=3 . Don't write the solution, just write the MATLAB command that gives the solution.)


    _________________________________________________________

        Section III. Miscellaneous functions

  7. Fill in the blank with the result that MATLAB gives when you type the following commands.
    >> x = [ 5.9 5.1 -1 0 4.99 -3.99 ];
    >> floor(x)
    ans =

    _____________________________________________________

    >> ceil(x)
    ans =


    _____________________________________________________

    >> z = [1 3 5 7 9];
    >> cumsum(z) % note how this differs from the sum function
    ans =

    _____________________________________________________


    >> w = 1:2:9;
    >> cumprod(w) % note how this differs from the prod function
    ans =

    _____________________________________________________







Part 2: Scripts and Functions

  1. ______________________________________________________________________________________



  2. ______________________________________________________________________________________



  3. Y / N

  4. Y / N

  5. ___________________________________________


  6. ___________________________________________


  7. ___________________________________________


  8. _________________________________________________________________


    _________________________________________________________________


    _________________________________________________________________



  9. ______________________________________________________________________________________




  10. ______________________________________________________________________________________




Part 3: Kinetic Energy of a particle


  1. ____________________________________________________________________________________


  1. ____________________________________________________________________________________



  2. ____________________________________________________________________________________



  3. ____________________________________________________________________________________


  4. ____________________________________________________________________________________



    _____________________________________________________________________________________



    ______________________________________________________________________________________



    ______________________________________________________________________________________




    ______________________________________________________________________________________




Part 4: Writing your own function

  1. _________________________________________________________________________________



  2.  z = _____________________________________________________________________________________________________________




  3. _____________________________________________________________________________________________________________








Part 5: Multi-valued functions, if statement

    19. Write the result MATLAB returns when you type the following:
    >> t = [ 3, 7, 1, 9];
    >> max(t)
    >> min(t)

    ans =
    ____________________________________________________________

    20. Write the result MATLAB returns when you type the following:
    >> m = 6;
    >> mod(m, 2)
    >> n = 7;

    >> mod(n,2)

    ans =

    _________________________________________________________


    21. What values do you get for numbers, num, maxNum, minNum = __________________________ ?


    22. What values do you get for numbers, num, maxNum, minNum = __________________________ ?