CS101 - Lab Activity 3

Name: ____________________ NetID: ______________ Section#: ______ (e.g. AYA, AYB, ...)


Name: ____________________ NetID: ______________ Section#: ______


Name: ____________________ NetID: ______________ Section#: ______


This lab is worth 12 points. 1/3 point per question.



Part 1: Standard MATLAB Functions 


Section I. Polynomials

  1. Fill in the blank below to find the roots of the polynomial f(x) = x3 - 3x2+ 2x .  

    >> f = ______ [ 1   -3    2    0] _______________________;
    >result = roots(f)

  2. Write the result you get (the roots).

        0.0000
        2.0000
        1.0000


    result = ___________above may be written vertically or horizontally for full credit and 2.0000 may be written as 2_________________

  3. Use the polyval function to check your that you really have found the roots.

  4.  >> check = polyval( _____ f _______, __________ result ____________)


  5. Write the results you get (the value of check).


        0
        0
        0


    check = ____________________________


  6. Its easy to create a polynomial if you know all the roots. Use the Matlab function named poly to create a the coefficients of a polynomial that
    has  -1, 1, 2 and 3 as its only roots.

    >> p = poly([-1, 1, 2, 3])


  7. Write the results you obtained ( the value of p ).

    p = _______[   1    -5     5     5    -6 ]___________________________________
  8. 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 = _____  [  1, linspace(0,0,98),   -1,  0 ]  ______or ________   [ 1, zeros(1,98), -1, 0]  ________(commas not necessary)___________ 
    >> roots(p)

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



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


    ____________  polyval( [ 1 ,0 , 2, 0, 3], -2)  _______________________________________________________


    Section II. Miscellaneous functions

  10. Use the Matlab sum function and : and .^ operators to compute 20-20+18-18+16-16+ ...+2-2

    total = ____________  sum(   (20:-2:2) .^ -(20:-2:2)    )  OR  sum(   (20:-2:2) .^ (-20:2:-2)    )   ______________(both pairs of inner parenthesis are necessary)____________________

  11. Use the Matlab factorial function ( 4! in our notation below is written as factorial(4) in Matlab) to compute the following sum.
    total = 1 + x + x2/2! + x3/3! + x4/4! + x5/5! + x6/6!  where x is a row vector.

    total = polyval( ____   1./ factorial(6:-1:0) ____________(./ is necessary)________________________ , x)
                Hint: Use 6:-1:0 and remember that factorial(0) equals 1.

  12. Use the Matlab factorial function to compute the following sum.
    total = x - x3/3! + x5/5! - x7/7! + x9/9! - x11/11!  where x is a row vector.

    total = polyval( _____  repmat([-1, 0, 1, 0], 1, 3)./factorial(11:-1:0) ______(./ is necessary)______ , x)
              Hint: Use 11:-1:0 and repmat([-1,0,1,0],1,3) (see lecture 4-9).


  13. Fill in the blank with the result that MATLAB gives when you type the following commands.


  14. >> x = [ 5.9 -5.1 -1 0 4.99 -0.99 ];
    >> floor(x)
    ans =

    _______________   5     -6    -1     0     4    -1  ______________________________________


  15. >> ceil(x)
    ans =


    ______________   6     -5    -1     0     5     0 _______________________________________


  16. >> z = [8  7  5  2  1];
    >> cumsum(z) % note how this differs from the sum function
    ans =

    ______________   8  15   20   22  23    _______________________________________


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

    ________________   1     3    15   105   _____________________________________





  18. Part 2: Scripts and Functions


    Section A. Scripts are just lists of commands


  19. What variables are listed in your workspace after you run the clear command?

  20. __________________  none i.e no variables _______________________________________


  21. What variables are listed in your workspace after running the script?
  22. _________________a, b, c ________________________________________



  23. Are variables that are created within a script accessible to you in the workspace? Y / N  _____ Y_________

  24. Can workspace variables be overwritten(changed) by running a script? Y / N    ______ Y _______


    Section B. Functions are different from scripts


  25. What is returned when you call the rolldice function?


  26. ans =

              1 or 2 or 3 or 4 or 5 or 6
    ________________________________________________________



  27. What is the value of b after calling the rolldice function?

    _____________ b = 1234 _________________________________________

  28. What did you get when you executed r = dieroll ?

    ??? Attempt to execute SCRIPT dieroll as a function:
    \\dcshome.cs.illinois.edu\gambill\My Documents\MATLAB\dieroll.m

    ________________or   ERROR ______________________________________

  29. What do you get after running help rolldice the second time?

     function c = rolldice()
      rolldice: simulate the roll of a six sided die.
      returns a random integer between 1 and 6 inclusive.

    ______________  or  the comments in the function are displayed ________________________________________


  30. Section D. Passing parameters to functions


  31. What do you get after running rolldice(5) ?

    ans =

         3     1     6     1     5
    _________________or  any five integers with values between 1 and 6 inclusive since they occur at random _________________

  32. What happens if you forget the argument to rolldice ?

    ??? Input argument "n" is undefined.

    Error in ==> rolldice at 5
     a = rand(1,n);
    __________________or  ERROR ____________________________________



  33. Part 3: Kinetic Energy of a particle


  34. What value did Matlab return when you typed energy(m,0)?

  35. >> energy( m , 0)


    _________________ 0 _____________________________



  36. What value did Matlab return when you typed energy(m, [0 , .5*c])?

    >> energy( m , [ 0 , .5*c ])

       ans =

                       0   1.26655050546144e-14
    ______________________________________________


  37. What value did Matlab return when you typed energy(m,c)?
    >> energy(m, c)

    ________________ Inf ______________________________

  38. Fill in the blanks to create a vector v that has 200 equally spaced values from 0 to .99*c.

    >> v = linspace( _____ 0 ______ , ____ .99*c ___________ , 200);



  39. Write your code for the function energy in the blanks given. You may not need all the lines.

  40. ______________ function ke = energy(m,v) ________________________________

    _______________ c = 299792458; _______________________________

    _______________ ke = ( 1./sqrt(1 - v.^2 ./ c.^2) - 1) .* m .* c.^2; _______________________________




    Part 4: Writing your own function: finding the farthest points to the origin.

  41. Write the function definition line (i.e. the first line) for the function farthest. Use the variables x, y for input parameters and  xfarthest, yfarthest. as output variables.

    Don't write the entire function, just the function definition (first line of code).

    ___________________function [xfarthest, yfarthest] = farthest(x,y)________________________________________________


  42. For two vectors x and y write a single Matlab expression that returns a vector of the distances from the points to the orign.


      distances = ______________sqrt(x.^2 + y.^2); ( ^ not valid, must use .^)__or________(x.^2 + y.^2) .^ 0.5;________


  43. Write the results of running your function as shown above.

    xfarthest =

         -8     8


    yfarthest =

         8     8

                  


  44. Part 5: if statement and multi-valued function


  45. Complete the function definition line for the function named info. Use the variables named name and age contain the results that the function returns.

    function [name, age] = ______________ info() ______________________________________ 


  46. Complete the code that traps an error.

    name = input('What is your name?');

    if   ischar(name)
        
    error = 0;   % 0 means false in Matlab
    else
         error = 
    ___________ 1; _____________________
    end


  47. Complete the code that traps an error.

  48.   error = 1; % no correct name obtained yet so error is true
           
      while error == ______ 1 ___________


  49. Type at the Matlab prompt,

    >> [name, age] = info()

    And enter Bob and not 'Bob' when asked for your name. What error message do you get?


    ??? Error using ==> input
    Undefined function or variable 'Bob'.

    Error in ==> info at 7
    name = input('What is your name?');
     
    What is your name?
    _____________________or  ERROR _______________________