Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Mid Term Exam (60 minutes)

 

Name:                                                                         NetID:
 

Lab Section:                                                                Date: June 29, 2005

 

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 8 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 and to write your signature on the line below:

 

_______________________________________________________________________
(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

Score

Grader

1

5

 

 

2

8

 

 

3

9

 

 

4

7

 

 

5

6

 

 

6

8

 

 

7

9

 

 

8

12

 

 

9

8

 

 

10

9

 

 

11

6

 

 

12

13

 

 

13

5

 

 

14

12

 

 

15

5

 

 

16

15

 

 

17

13

 

 

Total

150

 

 

 

 

 

1.  Assume that the vector x is defined and contains at least three elements.  Write a Matlab statement to display the third element from x:

 



>>                                                  

 

 

2. Write Matlab statements to discretize the sin(x) function and plot it using the Matlab plot command.  Use 50 equally spaced x values from - to +.

 

 

 

 

 

 

 

 

 

 

3. Use the Matlab function fplot to plot a graph for the function y = ex on the range x = 0 to
x = 5.

 

            >>                                                  

 

 

 

 

4. Suppose the following Matlab statement has been executed:

 

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

 

Write the correct response that Matlab would display when executing the following commands. If an error would result then write ERROR.

 

a)     >> B = [ A ; [9 ; 10 ; 11 ; 12]’]





__________________________________________

 

     b) >> A(1,1:4)



 

        __________________________________________

     c) >> A(:,2) = 9

 



                  ___________________________________________________

5.  Complete the code for the Matlab function named  middle, shown below. The function middle has one input parameter x ( a vector). The return value of middle is a vector containing the midpoint between consecutive values of x.  For example,  your function should give the following results.

>> x = [ 1   3   5   6]

>> y = middle(x)

        y   =

                2   4   5.5

 Your code should work for any vector of values for x.


function y = middle(x)

     % write your code here

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6.  Assume the vectors x and y have been defined and contain at least 10 elements each (but either or both may contain more than 10 elements).  Write a Matlab statement to define a vector

 

          z = [x­1 + y3, x2 + y4, x­3 + y5, ..., x8 + y­10]

(where 1 denotes the first element of the vector x , 2 denotes the second element of x, …)

You must use subscripting and the colon ( : ) operator.

 

     >> z =                               

 

 

 

 

 

7. Write Matlab statement(s) to produce the vector [1   -1   1   -1   1 ... 1] containing 99 elements.

 

    

 

 

 

 

 

 

 

8. Fill in the blanks to convert the following linear equations into the form A* b = c:

 

          5X + 3Y + 2Z = 30

          3X­­ + 5Y + 6Z = 27

          7X + 3Y + 9Z = 51     

 

where A is a matrix of coefficients and c is a column vector of constants.

 

    >> A =                                              

 

    >> c =                                              

 

Now write the Matlab statement to solve the system for b (Do not solve the system manually):

 

     >> b =                                              

 

 

 

 

 

 

 

 

 

9. Let A be a 3 x 2 matrix. Which of the following are valid operations? Circle any/all correct answers.

 

 

            A' .* A'                        A .* A'                         A' .* A                         A .* A 

 

 

 

 

 

            A' * A'                         A * A'                          A' * A                          A * A

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

10. Complete the following function tridiag where tridiag(N,b) creates an NxN matrix which is all zeros except that it contains the value b at every element along the main diagonal , super-diagonal and sub-diagonal.  You may assume that N > 2. Hint: use the diag and ones functions.

 

For example, when N = 5 and b = 7,

 

            >> tridiag(5, 7)

 

produces the output:

 

            7  7  0  0  0

            7  7  7  0  0

            0  7  7  7  0

            0  0  7  7  7

            0  0  0  7  7

 

However, your function should work for any value of N (N >2) and b. Here's the function to complete:

 

     function result = tridiag(N, b)

     % create three N x N matrices

    

     % main - matrix has the value b down the main diagonal

     % and zero elsewhere


main =  _____________________________________ ;

% upper - matrix has the value b down the super-diagonal % and zero elsewhere

upper = _____________________________________ ;

% lower - matrix has the value b down the sub-diagonal % and zero elsewhere

lower = _____________________________________ ;

 

result = main + upper + lower ;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

11. Assume the following function has been saved as fun.m:

           

     function result = fun(z)

          x = z / 3;

          y = x * 3;

          result = x * y;

 

Suppose the following statements are typed at the Matlab prompt. Write the correct output that Matlab would display.

 

     >> clear

 

     >> result = 9;

 

     >> result = fun(result);

 

     >> who

 

           

                                                                                                                                               

 

>> result
          result =

    
     _________________________________________________



 

 

 

12. Complete the following function named randomIntegers that returns a row vector of N random integers in the range [1, 6].  That is, the row vector contains N integers greater than or equal to 1 and less than or equal to 6. 

For example,

    >> y = randomIntegers(4);
          

then y is a row vector with four random integer values between 1 and 6 inclusive. Of course these four random values may not be the same.

You must use the rand function.

    

 

function result = randomIntegers(N)

% write your code here

 

 

 

 

 

 

 

 

 

13. Assume the following function has been saved as square.m:

 

     function s = square(x)

          x ^ 2

 

However, this function does not work as expected.

 

Fix the function, so that at the Matlab prompt you should see exaclty the following results:

 

     >> s = square(3)

                   s =

              9

  The function square must also work as expected for all other scalar  inputs.

 

 

function s = square(x)

% write your code for the ‘correct’ version here

 

 

 

 

 

 

 

 

 

 

 

 

 

         

 

 

14. Given the polynomial 4x4 + 6x2 + 3x - 4:

 

a) Write Matlab statement(s) to evaluate the polynomial at  x = 1:500 . You must use the polyval function. Do not calculate the values yourself.

 



 

 

            b) Write Matlab statement(s) to find all the roots of the polynomial.

 

 

 

 

 

 

15. Write the Matlab command to allow a function to access/modify the variable x in a workspace?

                                                                                                                                               

 

16. Complete the following Matlab statements to plot the surface defined by z = x2 + y2 in three dimensions:

 

     >> xvec = linspace(-5, 5, 100)

 

     >> yvec = linspace(-5, 5, 100)

 

     >> [xmat, ymat] =                                    ;

 

     >> zmat =                                            ;

 

     >> surf(                                             );

 

 

 

 

 

 

 

 

17. Write statement(s) to use the Matlab function trapz to approximate the definite integral of y = 2x3 - x on the interval from -1 to 1.  Use 1000 points.