CS101 - Lab Activity 3

Name: ____________________           NetID: ______________     Section#: ______

Name: ____________________           NetID: ______________     Section#: ______

Name: ____________________           NetID: ______________     Section#: ______


Each question is worth 1/4 point.

Objectives

In this lab, you will:

References


Part 1 :  Matlab Functions

  1. What variables do you see before running the script?
  2. __________none ____________________________________________________________________________

  3. What variables do you see after running the script?
  4. __________a b c____________________________________________________________________________

  5. Are variables that are created within a script accessible to you in the workspace?


     

  6. Can workspace variables be overwritten by a script?


     

  7. What is returned when you call the 'rolldice' function?

    ________(any integer from 1 to 6)___________________________________

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

    ________1234 ___________________________________

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

    ________(Error...)_________________________________________

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

  11.  

     

    _____(some comments/help message for the function)______________________________

  12. What do you get after running 'rolldice(5)' ?
  13. _____(any 5 integers from 1 to 6)______________________________________________

  14. What happens if you forget the argument to 'rolldice'?
  15. _____(Error...)_________________________________________________________________________________



  Part 2 :  Kinetic Energy of a particle

Do not open the Matlab editor and write code for your functions yet. Just answer the following questions and then go back to the instructions step 4) Write the code
  1. What value did Matlab return when you typed ke(m,0)?

     ____________ 0____  

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

     __0__   1.266550505461440e-014_or approx._

  3. What value did Matlab return when you typed ke(m,c)?

     ______ Inf ___________

  4.  Fill in the blanks with the same values you typed above.

    v = linspace( ____0_______ , _____.99*c__or __.99.*c______ ,  200);

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

              ___________function   ke = energy(m,v) _______no deduction for using other names just so long as the function works correctly____
              __________c = 299792458; _______________________________
              __________ke = (1./sqrt(1 - v.^2./c.^2) - 1).*m.*c.^2;_________



  Part 3 :  Writing your own function
 
  1. Write the function definition line (i.e. the first line) for the function 'max_dist'. Use the variables x, y and z. The inputs will be x and y, while z is the return value. Don't write the entire function.

  2. ______function z = max_dist(x,y)__________________________________________________________

  3. For two points   x = [ x x2  ] and y = [ y y2  ], then x+y in Matlab gives the result [ x1 + y1     ,   x2 + y2  ].write a Matlab expression which creates a vector  w =  [ x12 + y12     x22 + y22  ]. (Use only x and y in your expression, and don't use subscripting.)

    _____w = x.^2 +y.^2;____or_______w = x.*x +y.*y;__________________________________________

    Note that the expression above should work for any number of points, as long as vectors x and y have the same length. Don't include this Matlab code inside your function.


  4. The built-in 'sqrt' function computes the square-root, for example if w = [ 4  16  ] then sqrt(w) would return the vector [  2  4]. Write a Matlab expression which creates the vector u  =   sqrt using only the variables x and y.

    _______u= sqrt(x.^2+y.^2);______or__________u= sqrt(x.*x+y.*y);__________________________
    Don't include this Matlab code inside your function.

  5. The built-in 'max' function computes the maximum value, for example  if u = [2 4] then max(u) would return the value 4. Write a Matlab expression which
    creates the vector z = max(vector square root)  using only the variables x and y.

             ______z= max(sqrt(x.^2+y.^2));_or ____z = max(u);_____or __variations______________________
             Include this statement in your function.

  1.  TA signature   __________________________________________________________________

Part 4 : Absolute and Relative Error

  1.     temp = ______________cumprod(1:100);______________no deductions for missing ;_______

  1.     temp2 = _____________fliplr(temp);_____________________________________________



  2.     y = _________________polyval(p,x);________________________________________________________


  3.     TRUE             FALSE