Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Mid Term Exam I (60 minutes)

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: October 1, 2009

 


No questions will be answered during this examination.  If you do not understand a question, read it again.  If you still do not understand it, make reasonable assumptions and write them down. (Points will be deducted if unreasonable assumptions are made.)

DO NOT CHEAT: Cheating includes not only copying from another person but also allowing someone to copy from you.  Anyone who copies or allows someone to copy will receive a score of zero.  So be defensive and protect your work.

This examination contains 10 pages including this page. Check that your copy is complete, and ask for a replacement if it is not. Do all your work on these pages. For your own protection, in case pages come apart, write your NetID at the TOP of each page before beginning work.

 

Do not forget to sign the attendance list. If your exam is misplaced and you did not sign the attendance list then you will receive a zero score for the exam.

You may not use any electronic devices, book, notes or other references during this examination.

DO NOT WRITE IN THIS SPACE

Section

Possible Score

Deduction

Grader

1

5

 

 

2

3

 

 

3

6

 

 

4

6

 

 

5

8

 

 

6

8

 

 

7

8

 

 

8

6

 

 

9

8

 

 

10

6

 

 

11

4

 

 

12

6

 

 

13

3

 

 

14

5

 

 

15

5

 

 

16

6

 

 

17

4

 

 

18

5

 

 

19

8

 

 

20

9

 

 

21

6

 

 

Total

125

 

 

A

 

Exam Scoreà

 

1.      Write a single Matlab command using the linspace function to create the vector

 

x =  [ 1, 2, 3, 4, 5 ]

 

>> _________x  = linspace(1,5,5)______________________________________________

 

 

2.      Write a single Matlab command using the : operator to create the vector

 

x =  [ 1, 2, 3, 4, 5 ]

 

>> _________x = 1:5___or x = 1:1:5_________________________________________________

 



3.      Using the vector  x  from question 1 write a single Matlab command to create a matix with the values:


Your answer must include the variable x and NOT the numbers 1,2,3,4,5.


>> _______[x ;  x, ; x]________or_[x’ , x’ , x’]’______________

 

 

4.      Write the results Matlab returns for the following commands typed in the Matlab command window.

4.

 

a)  >> x = [ 1, 3, 2, 1, 3, 0, 3 ];

   >> max(x)

__________3___not ___3  3  3______________________

 

 

b) >> y = [ 1, 2, 3, 4; 1, 2, 3, 4; 4, 3, 2, 1];

   >> size(y)

 

___________3   4___ or [3 4]_______________________________________________

c) >> z = [3  6  9] ./ [1  2  3]

____________3   3   3_________________________________________________

5.      Given mat = [ 1, 2, 3; 4, 5, 6; 7, 8, 9 ] , which Matlab command generates the following matrix?

 



Circle each correct answer.  (Note: there may be more than one correct answer.)

 

a) mat(2:1:3, 2:1:3)

 

b) mat([2,3],[2,3])

 

 

c) mat(2:end, 2:end)

 

 

d)  mat(2:3, 2:3)

 

 

 

 

 

           

6.      The vectors a and b are defined as:

 

>> a= [ 1, 3, 5];

>> b= [ 9, 5, 4];

 

Which of the following Matlab commands returns the value  8 ? Circle each correct answer.  (Note: there may be more than one correct answer.)

 

a)>> min(a+b)

 

 

 

b)>> min( sum([a', b']) )

 

 

 

c)>> min(a) + min(b)

 

 

 

d)>> min( sum([ a ; b ]) )

7.      Write a single Matlab command to find all the roots of the function p(x)= x5-3x3-9. Do NOT calculate the actual roots. Just write the Matlab command that would produce the correct roots.

 

 

 

_________roots( [ 1  0  -3  0  0 -9]) ____________________________________________

 

 

 

 

8.      Fill in the blanks below to plot the function y = sin(x) using the Matlab plot function. Choose a sample of 100 equally spaced values from -2π to 2π for x.

 

>> x =_______linspace(-2*pi, 2*pi, 100);_____________

    

>> y =_____sin(x);____________

 

>> ________plot(x,y)________________________________________

 

 

 

9.      Write the results Matlab returns for the following sequence of commands typed in the Matlab command window:

 

 

a) >> x = floor([1.7, 4.9, .8])

 

________1   4   0 __or [ 1   4   0]____no deduction for x=___

 

b) >> y = polyval([1, 0, -16], 1)

 

_______-15________no deduction for y = ___________

 

c) >> z = [0, cumsum([2, 4, 6])]

 

______0   2   6   12______no deduction for  z = _____

 

 

10.  Fill in the six blanks to create the following matrix named design_mat.

 

>> design_mat = [zeros(_3_,_2_),2*eye(_3_);3*ones(_2_,_3_),eye(_2_)]

 

>> design_mat =

 

     0     0     2     0     0

     0     0     0     2     0

     0     0     0     0     2

     3     3     3     1     0

3     3     3     0     1

 

11.  Circle each Matlab function definition below that would NOT generate a Matlab error. Hint: the function definition is the first line of Matlab code in writing a function.(Note: there may be more than one correct answer.)

 

a) function output = fn_name(in1; in2)

 

 

 

b) fn_name output = function(in1, in2)

 

 

c) function output = fn_name(in1, in2)

 

 

 

d) function fn_name(in1, in2) = output


 

12.  There is a function called horse which takes as input two variables horn and hoof in this order and outputs a variable called unicorn. Your task is to write down the first line of code that comprises the function definition of this function.

 

 

_______function   unicorn = horse(horn, hoof)________________

 

 

 

13.  Suppose the code for the horse function that you defined above has been completed. Now suppose the following commands were executed in the Matlab workspace:

 

>> x = [2 3 4]

>> y = 7

 

Your task is to fill in the blank below to make a call to the horse function, where the horn variable should be initialized with the value from y, while the hoof variable should be initialized with the value from x. The output from horse is assigned to a variable named z . You must use the x and y variables in your answer.

 

 

>> z = _________horse(  y , x) ________________________________________________

 

 

 

 

 

           

14.  Which of the following Matlab commands is a correct way of computing an approximation of the definite integral of the function f(x) = cos2(2x) on the real interval [0, π]? Circle each of the correct answers. (Note: there may be more that one correct answer.)

 

a) >> quadl('cos2(2x).^2',[0, π])

 

 

 

b) >> quadl('(cos(2x))^2',0,pi)

 

 

 

c) >> quadl(@(x)(cos(2*x)).^2,0,pi)

 

 

 

d) >> trapz('(cos(2x)).^2',0,pi)

 

e) >> x = linspace(0,pi,1000);

>> y = (cos(2*x)).^2;

>> trapz(x,y)

 

 

 

 

 

 

 

15.  Write the result Matlab returns when the following sequence of commands are typed in the Matlab command window.

 

>> x = [2 3 5 7 4];

>> y = [12 17 31 21 1];

>> z = x( y > 5*x )

   z =

 

 

 

__________2   3   5_________________________

 

 

 

 

 

 

 

 

 

 

 

16.  The file glob.m contains the following code

 

function a = glob(b,c)

global g

g = [g, b+c];

a = c-b;

 

At the Matlab prompt you type the following commands. Fill in the blanks with the correct values that Matlab returns.

 

>> clear

>> global g

>> g = [1 2];

>> b = glob(2, 4);

 

 

>> who

           

           

________b   g_____________________________________________________

           

           

>> g

 

   g =

 

 

________1   2   6_____________________________________________

 

 

>> b

 

   b =

           

           

___________2__________________________________________________

           

           

           

           

17.  Write the result Matlab returns when the following sequence of commands are typed in the Matlab command window.

17.

 

>> r = fzero('x.^2-sqrt(x)',2);

>> result = r.^2 – sqrt(r)

 

result =

 

_________________0_______________________________________

 

 

18.  Which of the following Matlab commands creates a window of x-axis from 0 to 1.5 and y- axis from  

-2.5 to 4. Circle the correct answer. (Note: there is only one correct answer.)

 

 

a) axis([-2.5 4 1.5 0])

 

 

b) axis([0 1.5 4 -2.5])

 

 

c) axis([-2.5 4 0 1.5])

 

d) axis([0 1.5 -2.5 4])

 

 

e) axis([0 -2.5 1.5 4])

           

 

 



19.  Circle the correct answer choice that, when replacing the blank below, produces the results shown below. Hint: note that the result displays the x values that correspond to the smallest value in y.  (Note: there is only one correct answer.)

 

>> y = [ -1,  2,  5, -1,  3,  0,  4,  0,  5,  1 ];

>> x = [ .1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0 ];

 

>> result = ______________________________________________

 

result =      0.1000      0.4000


a) >> y(x == min(x))

    

 

b) >> x(y == min(y))

 

 

 

c) >> y(min(x))

 

 

 

d) >> x(min(y))

 

 

 

 

20.  Complete the code for a Matlab function named count that takes two row vectors of the same length, named x and y, as inputs and returns the count of all x’s values that are larger than y’s values at the corresponding positions in the vectors. Do not write any comments.  After you write your function it should give the following results:

 

>> count([1 2 3] , [1 2 3])

   ans =
                0

>> count([2 1 3] , [1 2 3])

   ans =
                1

>> count([3 5 10] , [1 2 3])

   ans =
                3

 

The code for your function should work correctly with the input of any two row vectors that are of the same length and not just for the three examples shown above.

 

 

       function result = count(x,y)

       % your code goes under this line

   

                             result = sum( x > y);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

21.  Including air friction, a parachutist’s motion is described by the following system of ordinary differential equations:       (Correction to original)

dy/dt  = v

dv/dt =  -g + (b/m) * v2

                                                           

 

 

 

with initial conditions v(0) = 0  , y(0) = 5000  and constants g = 9.8  , m = 100  and b = 10.

a) Fill in the blanks to complete the code for the function named Derivatives .  For a scalar  t

    and a vector yv , Derivatives(t,yv) must return a column vector containing the derivatives   

    as shown in the formulas above.
   
     function  dydv = Derivatives(t , yv)

     y = _____yv(1);________________________________


     v = _____yv(2);________________________________
     
     g = 9.8;

     m = 100;

     b = 10;

     dydv = ______[ v ;-g + (b/m)*v.^2];_

 

     Due to typos on original the following are also acceptable,

     dydv = ______[ v ;-g - (b/m)*v.^2];_

     or

     dydv = ______[ v ;-g - (b/m)*v];_



  b) At the Matlab prompt fill in the blanks to call the ode45 function with initial values y(0) = 5000 ,

v(0) = 0 over the time range 0 to 100 seconds.  Hint: The format of the ode45 is as follows:

 

[t, yv] = ode45(@aux_function, time interval, initial_conditions);
 

  >>[t ,yv ] = ode45(_@Derivatives_ ,__[0,100]__, _[5000;0]__);

    which then allows you to plot the velocity versus time using the commands,

  >> v = yv(: , 2); 

  >> plot( t , v)