Department of Computer Science

University of Illinois at Urbana-Champaign

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

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: September 27, 2007

 


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.

The number of points for a question is roughly proportional to the amount of time you may need for it. Don’t spend too much time on any one question.

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

6

 

 

2

13

 

 

3

3

 

 

4

9

 

 

5

4

 

 

6

5

 

 

7

7

 

 

8

14

 

 

9

4

 

 

10

8

 

 

11

8

 

 

12

6

 

 

13

6

 

 

14

8

 

 

15

9

 

 

16

5

 

 

17

10

 

 

Total

125

 

 

 

 

Exam Scoreà

 

A

  1. The following commands are typed in the Matlab command window. Write the results Matlab returns.

    >>  A=[0 2 3 ; 1 3 0];

>>  B=[1 0 3 ; 1 5 0];

>>  A+B

    ans =

 

 

 

 

 

 

    ______________________________________________________

 

 

 

 

 

 

 

  1. The following commands are typed in the Matlab command window. Write the results Matlab returns.

>> X=[1,2,3,4,5];

>> X(3)

        ans =

 

  ______________________________________________________

>> X(1:3)

   ans =

 

            ______________________________________________________

>> X(3:end)  

   ans =

 

  ______________________________________________________

 

>> [X(end:-1:3) ; X(1:2:5)]’  

   ans =

 

 

 

 

  ______________________________________________________

 

 

 

  1. Assume that the variable y holds a scalar and the variable x holds a vector.

Write a single Matlab expression that will compute y divided by x :

For example, if  y = 3  and x = [ 1   3   6 ] then  y divided by x should return [ 3  1  .5 ] . However the Matlab expression you write should work for any scalar assigned to y and vector assigned to x. You may assume that x doesn’t contain the value 0.

 

 

  ___________________________________________________

 

 

 

 

  1. Fill in the blanks below to plot the function y = cos(x) using the Matlab plot function.  Choose a sample of 50 equally spaced values from 0 to π for x .

 

>> x = __________________________
       

     >> y = __________________________

 

 

>> ______________________________

 

 

  1.  The following function generates an error,  ( the line numbers are not part of the code)

 

 line 1)   function = dummy(parameter)
 line 2)  
global x
 line 3)  
result = x + parameter;

 

when the following commands are typed at the Matlab prompt:

 

 

>> clear

 

>> global x

 

>> x = 1;

 

>> dummy(5)

 

 

The command dummy(5) should produce the value 6 but instead generates an error. Write the line number in the function dummy that generates the error and the corrected line of code below:

 

Line # ________________________________________



Corrected Line: ________________________________________________________

6.      Fill in the blanks to call the function fplot to plot the function y = sin2(x) + sin()  on the interval [0,40].

 

 

>> fplot(_______________________________ , _______________________)

 

 

 

7.      Write the single Matlab command to compute all the roots of function y =x5-1 . Do not write the roots just write the command that will produce all the roots.

 

 

 

>> _____________________________________________________________________

 

 

 

 

8.      We would like to plot the following 3D surface:

 

 

 

 

Fill in the blanks to plot the surface above a grid of points in the x-y plane with 50 equally spaced values for x between 1 and 10 and 50 equally spaced values for y between -3 and 3 .

 

>> x = linspace(_______________, _________________,_________________);

 

>> y = _________________________________________________________________;

 

>> [XMAT ,YMAT] = meshgrid(_______________,___________________);

 

 

>> ZMAT = _____________________________________________________________ ;

 

 

>> surf(_______________, ________________, ___________________)

 

  1. We have two functions f1 and f2 saved in two separate files f1.m and f2.m as follows:

 

function y = f1(x)

 global g

 g = g + x;

 y = g;

 

function y = f2(x)

 g = 100;

 y = g * x;

 

 

The following sequence of commands is entered at the Matlab prompt. Fill in the blank with the correct value that Matlab returns for the variable named g.

>> clear

>> global g

>> g = 0;

>> a = f1(3);

>> b = f2(g);

>> g

     g =

 

          ________________________________________________

 

 

 

 

 

 

 

 

 

  1. For each of the following write down the output that Matlab returns. If the command results in an error, write ‘ERROR’:

 

>> fzero('x-1' , 0.1)

 

            _____________________________________________

 

 

 >> fzero('x-1')

 

            _____________________________________________

 

 

            >> roots('x-1')

 

            _____________________________________________

 

 

            >> roots([1 -1])

 

            _____________________________________________

 

 

  1. The following sequence of commands was typed in the Matlab command window:

 

>> clear

>> x = linspace(0, 2 ,100);

>> y = sin(x);

 

 Assume that the commands below are typed in the Matlab command window immediately after the above commands. Circle each command that does NOT generate a Matlab ERROR.(there may be more than one command that does not generate an ERROR)

 

a) trapz(@sin,x,y)




b) quadl(@sin,x,y)




c) quadl(x,y)




d) quadl(@sin, 0, 2)




e) quadl(sin, 0, 2 )




f) trapz(@sin)



 

g) trapz(x,y)




h) trapz(y,sin(y))

 

 

 

 

 

 

  1. Circle each of the following commands that plots a graph containing at least the points (1,4), (2,5), (3,6), (4,7) (and possibly other points too). There may be more than one correct answer. 

 

a) plot([1 2 3 4], [4 5 6 7])



b) plot([4 5 6 7])



c) plot(1:1:4)



d) fplot([1 2 3 4], [4 5 6 7])



e) plot(4:7)



f) fplot('x + 3')

 

 




  1. Write the result produced when the following Matlab commands are executed.

>> x = [ 1  3  6  12 ];
>> diff(x)

   ans =

_______________________________________________

>> y = [ 1   1   1 ];

>> cumsum(y)
   ans =

     _______________________________________________

 

 

 

 

 

 

 

 

 

  1. Assuming that the matrix A is entered as follows at the Matlab prompt,


    >> A =[9   8   7 ; 6   5   4 ; 3   2   1 ; 0   -1   -2]
       A =
     
        9    8    7
         6    5    4
         3    2    1
         0   -1   -2


    a)  Write the result produced when the following Matlab command is executed.


         >> A( : , 1:2:3)
 

 

 

 

 

 

                        ____________________________________________________________

b) Fill in the blanks to complete the Matlab command to replace the second column of the matrix A with the values of  the vector [-1   0   1   0] .
 

             >> A( ____________, _______________) = __________________________

             c) Write the result produced when the following Matlab commands are executed.

              >>  B = [ 6 ; 6];
     >>  C = [ A([1 2],[2 3]) , B]
          C =



                       ____________________________________________________________