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    2    6

              2    8    0

 

 

    ______________________________________________________

 

 

 

 

 

 

 

  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 =

 

  ____________3__________________________________________

>> X(1:3)

   ans =

 

            _____________1        2          3______________________________

>> X(3:end)  

   ans =

 

  _____________3      4          5______________________________

 

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

   ans =

          5    1

          4    3

          3    5

 

  ______________________________________________________

 

 

 

  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.

 

 

  __________ y ./ x    or    x .\ y _________________________________________

 

 

 

 

  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 = _____ linspace(0,pi,50);_________
       

     >> y = ______ cos(x)_________________

 

 

>> ___ plot(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 # _______ 1_________________________________



Corrected Line: __ function result = dummy(parameter)________

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

 

 

>> fplot(___ ‘sin(x).^2 + sin(sqrt(x))’___ , ____ [0,40]____)

 >> fplot(___@(x) sin(x).^2 + sin(sqrt(x))___ , ____ [0,40]____)

 

 

 

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.

 

 

 

>> _____________ roots( [ 1   0   0   0   0   -1] )________________

 

 

 

 

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(___ 1____________, _____ 10____________,_____ 50____________);

 

>> y = ____ linspace( -3 , 3 , 50)_____________________;

 

>> [XMAT ,YMAT] = meshgrid(____ x___________,_____ y______________);

 

 

>> ZMAT = ____ exp( - XMAT.^2) .* cos(2.*YMAT)__________ ;

 

 

>> surf(___ XMAT__, ___ YMAT_____, ___ ZMAT______)

 

  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 =

 

          ________ 3________________________________________

 

 

 

 

 

 

 

 

 

  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)

 

            ___________ 1__________________________________

 

 

 >> fzero('x-1')

 

            ____________ ERROR_________________________________

 

 

            >> roots('x-1')

 

            _____________ ERROR________________________________

 

 

            >> roots([1 -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 =

________ 2    3    6_____________

>> y = [ 1   1   1 ];

>> cumsum(y)
   ans =

     _________ 1   2    3_____________

 

 

 

 

 

 

 

 

 

  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)
 

                                    9          7

                                    6          4

                                    3          1

                                    0         -2

 

                        ____________________________________________________________

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( ____ : ___, _____ 2_____) = ____ [-1 0 1 0] (may add ’ )_
             c) Write the result produced when the following Matlab commands are executed.

              >>  B = [ 6 ; 6];
     >>  C = [ A([1 2],[2 3]) , B]
          C =
            (assuming b) has no affect on A)
                  8     7    6
                 5      4    6

            (assuming b) changes A)

                                          -1               7          6
                                           0               4          6

   

                 ____________________________________________________________

 

 

 

 

 

  1. Solve the following system of equations using MATLAB. Do not write the solution, just fill in the blanks with the Matlab commands that would solve the system of equations.

 

10x = 6y – 4z + 20

8z + 16y = 6x + 40

8y + 4x = 9 + 9z

Assuming the system of equations is viewed as A*B = C then we need to assign values to A and C and then solve for B.

 

>> A = ____ [ 10 -6  4 ; -6  16  8  ; 4  8  -9]___;

 

>> C =  ____ [20 40 9]’ or [20 ; 40 ; 9]__;

 

     >> B = _______ A\C______________;

 

 

 

 

 

 

  1. Fill in the blanks to create the following 100x100 matrix,

 

 

 

where the only values not equal to 1 are on the super-diagonal. The super-diagonal contains the 99 values  99, 98 , 97, … 3,  2,  1.



>> matrix =  ones( __ 100_or 100,100 ) + diag( _98:-1:0_ , __ 1_ )

 

 

 

 

 

 

 

 

 

  1. Write the complete code for a Matlab function named count_bigones. The function count_bigones has one input parameter x which will hold a row vector. The function count_bigones has one output value count which represents the count of all values greater than 100 in the vector x .

 

For example, when the user enters the following command at the Matlab prompt,

>>  count = count_bigones([ 2  121   7.5  -123.4  345.6 ] );

count will have the value 2 since only two values (121 and 345.6) are greater than 100. However your code for the function count_bigones should work not just for the values shown above but for any vector of values. Do not include comments in your code.

 

 

 

function count = count_bigones(x)
          count = sum(x > 100);