Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Mid Term Exam (60 minutes)

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: 7/8/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

8

 

 

2

10

 

 

3

12

 

 

4

8

 

 

5

11

 

 

6

22

 

 

7

12

 

 

8

12

 

 

9

12

 

 

10

12

 

 

11

12

 

 

12

12

 

 

13

10

 

 

14

15

 

 

15

10

 

 

16

12

 

 

17

10

 

 

Total

200

 

 

 

 

Exam Scoreà

 

A

 

 

1.      Circle the correct output the following command produces when typed at the Matlab prompt:

>>  2:3:7

 

 

a) 2

 

 

 

b) 2   3   

 

 

 

c) 2   5

 

 

 

d) 2   5   7

 

2.      Fill in the blanks with the correct Matlab response to each of the following expressions typed at the Matlab prompt.

 

 >> cumsum( diff([1  2  3  4  5]) )

 

   ans =

 

 

 

        _________1    2          3          4_______________________

 

 

>> sum( [ 1   2   3 ; 4   5   6] )

 

 

    ans =

 

 

        _________5                7          9________________________

 

 

>> [ -1   1   -1 ] .^ 2

 

     ans =

 

 

 

        _______1        1          1_____________________________

3.      Including air friction, a toy parachutist’s motion is described by the following system of ordinary differential equations:

dy/dt  = v

dv/dt =  -g - (b/m) * v
                                                            falling object
with initial conditions v(0) = 0  , y(0) = 3  and constants g = 9.8  , m = .01  and b = .1 .

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  Dyv = Derivatives(t , yv)

     y = ________yv(1);_____________________________


     v = ________yv(2)_____________________________
     
     g = 9.8;

     m = .01;

     b = .1;

     Dyv = _____[v ; -g-(b./m).*v;___________________


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

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

 

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

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

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

  >> v = yv(: , 2); 

  >> plot( t , v)

 

  1. Circle the correct output the following command produces when typed at the Matlab prompt:

 

>> linspace(2,2,4)

 

 

a) 2   2   2   2  

 

 

 

b) 2   3   4

 

 

 

c) 2   4

 

 

 

d) 4   2

 

 

 

5.      Complete the Matlab code below by filling in the blanks to solve the system of linear equations shown below. You may assume that the system has a unique solution (so that Matlab does not display an error message). Do NOT write the solution to the system of equations, just write the code necessary for Matlab to find the solution.

 

 



 


>> A =  ___[ 2 3 -7 ; 5 2 3; -3 7 2];______

 

>> c =  ____[4 ; 7; 5];__________________

 

     >> solution = _____A\c;____________________

 

 

6.      Write a complete Matlab function named totals, that has one input parameter named mat containing a matrix of numbers. The function named totals will add all the values in the matrix mat. For example, after your code for totals is complete then it should work in the following manner when called from the Matlab prompt,

>> mat = [ 1 2 3; 4 5 6];
>> result = totals(mat)
      result =
                       21
 (which is the sum of all the values in mat, 1+2+3+4+5+6)

 Your code should work correctly for any non-empty matrix of numbers. Do NOT write comments.

 

function result = totals(mat)

     result = sum(sum(mat));

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7.      You are given a set of points in the plane in the form of two row vectors X and Y of equal length, i.e. X contains the x-coordinates of the points and Y contains the y-coordinates of the points. Complete the code for a Matlab function named max_dist to compute the maximum distance of the points and the origin. The distance between the i-th point [X(i),Y(i)] and the origin [0,0] is given by the formula:

           

For example, if X= [ 1 , 3 , 4] and Y = [1 , 4 , 3] then the distances between each of these points and the origin would be [sqrt(2), 5 , 5] and thus the maximum is 5 since  sqrt(2)  < 5.  That is, your function would return the scalar 5 as the result for this example.  Of course your function should work correctly for any non-empty set of points and not just the example cited above.

 function  result = max_dist(X,Y)

% no comments are necessary

% write your code here ..

  result = max( X.^2 + Y.^2);

or

 result = max( sqrt(X.^2+Y.^2));

 

 

 

 

8.      Write a single Matlab command to compute the following sum,

, which represents the sum    .

 

>> sum = __sum(1./(1:100));__parenthesis not optional_______

 

 

 

9.      Assume that you are given the code for the function named match in the file match.m shown below.

 

function result = match(x,y)

 global radius
 result = x.^2 + y.^2 <= radius;

Write the result Matlab returns for the following sequence of commands typed at the Matlab prompt.

>> global radius

>> radius = 1;

>> x = [ 0 1 2];

>> y = x;

>> z = match(x,y)

   z =

 

          _________1    0    0________________________________

 

 

 

10.  Write the Matlab command(s) to find all the roots of the polynomial shown below. Do NOT write the roots just write the Matlab command(s) that would produce all the roots.

 

roots( [1 -7 7 21 -30])

 

 

11.  Assume that you are given the code for the function named test in the file test.m shown below.

 

function c = test(a)

 scr

 b = 2;
 c = a+b;

 

and you are also given the code for the script named scr in the file scr.m shown below.

 a = 3;

 b = 4;

 c = 5;

 

Write the results Matlab returns for the following sequence of commands typed at the Matlab prompt.

 

>> clear

>> c = test(1);

>> c =

 

        ______5____________________________

>> who

      

        _______c____________________________

 

12.  Given the following Matlab statements typed at the Matlab prompt,

>> x = linspace(0,5,1000);

>> y = cos(x) + sin(x);

which of the following Matlab commands is a correct way of computing an approximation of the definite integral of the function f(x) = cos(x) + sin(x) on the real interval [0,5]? Circle each of the correct answers. There may be more that one correct answer.

 

a) >> trapz(x, cos(x) + sin(x))

 

 

 

 

 

b) >> trapz(@(x) sin(x) + cos(x),0,5)

 

 

 

 

 

c) >> trapz(x,y)

 

 

 

 

d) >> trapz(x,cos(x)) + trapz(x,sin(x))

13.  Given that the matrix A has the values assigned below,

 

>> A = [ 1   2   3   4;   5   6   7   8;   9   10   11   12]

     A =

 1    2    3    4

 5    6    7    8

 9   10   11   12

 

write the results Matlab returns for the sequence of commands typed at the Matlab prompt:

  >> A( 2 , :)




          ________5    6      7    8________________________________

 

>> A( [1 2] , [3 4])
                  
               3 4
               7  8    

       ________________________________________

 

 

 >> A(end, end)

 

 

 

 

                        _____________12___________________________________

 

 

 

 

 

 

 

 

 

14.  Let A be a 3 x 2 matrix, B be a 3 x 4 matrix and C be a  6 x 3 matrix that contain numerical values. Which of the following are valid operations, that is, which of the following will not produce Matlab errors? Circle each valid operation. There may be more than one valid operation.

 

(B') * A       A + B       [A,B]*C      C * [A, B]      C * [A ; A]

 

 

 

 

 

 

 

15.  Write the result Matlab returns for the sequence of commands typed at the Matlab prompt.

 

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

>> x([2  4]) = 6

      x =

 

                      ______5            6          3          6          1___________________

 

 

 

16.  Fill in the blanks below to complete a Matlab command to create the following 5 x 5 matrix:

 

 

      >> mat = diag( _1:5_, _0_) + diag( _5:-1:2_, __1____)

 

 

 

17.  Given that the file named fancy.m contains the following code:

function y = fancy(x)

y = x - 4;


Write the result Matlab returns for the following command typed at the Matlab prompt.

 

>> y = fzero(@fancy, 1)
   y =

   
              _____________4________________________________________________________