Name - Dalton State College by panniuniu

VIEWS: 8 PAGES: 10

									                                                                                                           Spring 2008

                               Computing for Scientists & Engineers
                                             CMPS 1371 (Hawkins)
                                   Exam II: Chapter Nine to Chapter Seventeen

           Questions are 5 points each unless otherwise noted.            Name ____________________
           Show your work!

2 points   True / False:
each
           1.           If v is a vector, then diag(v) returns a square matrix with the elements of v on
                        the diagonal.
           2.           The function strcmp(…) can compare strings of unequal length.
           3.           For the command [x y z] = xlsread(excel_file), the variable z represents both
                        numerical and string data.
           4.           The subplot(…) function allows multiple plots to be overlaid.

           Answer the following:

           5.      Read the following piece of code and then answer the questions that follow.

                           value = struct('band', 'nickelback', 'album', {'all the right reasons',
                                         'curb'}, 'song', {'animals', 'fly'}, 'genre' , 'rock');
                           value(2).band = 'U2';
                           value(2) = setfield(value(2), 'song', 'sweetest thing');
                           value(3) = value(1);
                           value = rmfield(value, 'genre');

                           A = isstruct(value)
                           B = isfield(value(1),'genre')
                           C = getfield(value(2), 'album')
                           D = value(3).band

                   What is the output of the following variables when the script above is executed:
                   a)      A=                                b)   B=




                   c)      C=                                d)   D=




                                                                                                           1
Answer the following:

6.      Write a recursive MATLAB function called ‘removeSpace’ that takes in a
        character array and deletes all blank spaces from it.

        HINT: The space character is represented by ASCII number 32.
        Note: You must use recursion to solve this question.

        For example:
               >> a = removeSpace ([‘Hello, how are you doing?’])
                      => a = ‘Hello,howareyoudoing?’




7.      Ackermann's Function is "a function to end all functions."
        The work done by the function ack grows much faster than polynomials or
        exponentials.
        Given the following recursive method:

                function ans = ack(x,y)
                if (x == 0)
                        ans = y + 1;
                elseif (y == 0)
                        ans = ack(x - 1, 1);
                else
                        ans = ack(x - 1, ack(x, y - 1));
                end

        What is the result of ack(1, 5)?




                                                                                  2
Answer the following:

                       1 3       4 3       10 
8.      Given:       A    , B  2 1 , C  20 , D  30 40 
                       2 4                  

        Determine the result of executing the following commands:

        a) A .* B




        b) A * B




        c) 2C' + D




9.      Given:
                        3x + 4y + 3z = 10
                        2x + 5y + z = 12
                        x+y+z=5

        Write the code to solve the above system of equations.




                                                                     3
            Answer the following:

            10.     You are working for the CIA, and you receive an encrypted message. You know
10 points           that in order to find the real meaning of the code you must do the following
                    changes:
                            1. First, remove every 'q'
                            2. Next, remove every second letter
                            3. Last, replace every 'z' with a 't'

                    For example, decoder('HeaprfrlqywqPuogqzyzdeqer') -> 'HarryPotter'

                    Write a function called decode that consumes a string, makes the above changes,
                    and returns the decrypted message.

                    function ans = decode(str)




                                                                                                      4
      Answer the following:

11.   You have the following Microsoft Excel®
      worksheet saved in a file called ‘Baby_score.xls’.
      The following lines of code are executed. What is
      the value of the variables when the following
      code is executed?

              [num txt] = xlsread('Baby_Score.xls');

              vec = num(:,end);
              [numrows numcols] = size(num);
              [txtrows txtcols] = size(txt);

              varA = find(vec == max(vec));
              varB = txt{numrows,2};
              varC = ischar(txt);

      a) varA =




      b) varB =




      c) varC =




12.   Write the code to plot the graph y = sinx.
      Be sure to label properly




                                                           5
Answer the following:

13.     Consider the following piece of code:

                A = [1:4; linspace(7,13,4); 15,18,21,24; 25:4:39];
                B = zeros(3,3);
                B(1:2,1:2) = A(3:end,3:end);

        What is the output of the variable B?




14.     What is the difference between eye(4,4)\A and A\eye(4,4) where A is a
        nonsingular 4x4 matrix?




                                                                                6
           Multiple Choice:
3 points
           15.           The following attempt to plot vectors x and y produces an error. Why?
each
                                  >> x = [0:1:5]; y = [0:2:8]; plot(x,y)

                              a)   x is a column vector and y is a row vector
                              b)   x and y must have the same values
                              c)   x and y have different lengths
                              d)   none of the above


           16.           Suppose that the matrix A has been loaded into Matlab:
                                       9 4 4 4
                                  A  2 8 0 6 
                                                  
                                       6 7 8 7 
                                                  
                       What would matrix B would look like if we define B to be:
                        >> B = A(:,1:2)

                                 9 4               4 4              9 4         9 4 
                              a) 8 0 
                                                b) 0 6 
                                                                   c)  2 0
                                                                                 d)  2 8 
                                                                                           
                                 7 8 
                                                   8 7 
                                                                      0 6 
                                                                                    6 7 
                                                                                           



           17.           When the following code is run, what is the result?
                                          x = [-1 1 1 -1];
                                          y = [1 1 -1 -1];
                                          plot(x,y)

                              a)                                     b)




                              c)                                     d)




                                                                                                 7
18.   Given the following piece of code:         a.price = 10;
                                                 b = a.price;
      What is the data type of the variable b?

       a)   struct
       b)   logical
       c)   array
       d)   char

19.   Which of the following are essential for a recursive function?

       a)   Modifying the input so that it moves towards termination
       b)   A call to the function itself
       c)   Terminating condition
       d)   All of the above
20.   When the following code is run, what is the result?
                       x = -3:.1:3;
                       y = x;
                       [xx yy] = meshgrid(x,y);
                       zz = xx.^2+yy.^2;
                       surf(xx,yy,zz)

       a)                                        b)




       c)                                        d)




                                                                       8
Bonus (5 points)
Sombrero:            Write a MATLAB script to rotate the function z = 2*sin(x)/x, which
goes from x = 0 to x = 10, about the z-axis, using an appropriate number of points. Be
sure to include proper labels for all the axes and a title. Your graph should be a smoothly
colored surface with no lines and should look something like the following figure.




                                                                                          9
Reference Section:
diag(m) – returns the diagonal elements of the matrix m
factorial(n) – returns n!
find(m) – returns the indices of the true elements of m
getfield(a,att) – returns the value of field att in the structure a – equivalent to a.(att)
image(x) - display the image from the matrix x.
imread(filename) - returns a matrix representation of an image
iscell(a) – checks if a is of class cell (a cell array)
ischar(a) – checks if a is of class char (a string)
isempty(here) – checks if here is null (usually represented by [], the empty vector
isfield(a,att) – checks if att is a field in the structure a
isstruct(a) – checks if a is a structure
length(a) – largest dimension of a
magic(n) – builds a n * n magic square
max(a) – value and index of the max value in a
mesh(x, y, z) – plot the surface defined by the x, y and z arrays with colored lines and
     white faces
[xx, yy] = meshgrid(x, y) – compute the plaid from the x and y vectors
min(a) – value and index of the min value in a
mod(a, b) – the remainder when a is divided by b
ones(rows, cols) – generate a matrix filled with 1
prod(v) – compute the product of all the elements in a vector v
rmfield(str, att) – removes a structure field
setfield(str, att, value) – set a structure field – equivalent to str.(att) = value
sin(th) – sin of the angle in radians
size(a) – all the dimensions of a
sort(v) – arranges the vector v in ascending numerical order
struct(att,val, … ,att,val) – builds a structure with the given attribute/value pairs
sum(v) – total all the elements in the vector v
surf(x, y, z) – plot the surface defined by the x, y and z arrays with colored faces and
     black lines
title(str) – titles the plot with the given string
[x, fs] = wavread(filename) – gives the waveform and sampling frequency for a .wav file
(x/y/z)label(str) – labels the plot axes with the given string
zeros(rows, cols) – generate a matrix filled with 0




                                                                                         10

								
To top