Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

University of KwaZulu-Natal

School of Electrical, Electronic and Computer Engineering


EXAMINATIONS : May 2010 SUBJECT, COURSE AND CODE: Applied Computer Methods ENEL2CMH1
DURATION: Three Hours TOTAL MARKS: 100

This question paper contains a total of 9 pages

Internal Examiner: Dr Tahmid Quazi Moderator: Mr Thippeswamy Muddenahalli


Instructions: This exam comprises of 3 Sections. Answer all the questions. Calculators are permitted. Answer Section A in the multiple-choice answer sheet. Three answer books are supplied and are to be used as follows: One for Section B and one for Section C. One for rough work. Do not clutter your answer books with unnecessary rough work. Marks are awarded for the manner in which you present your solution. Hand in your rough-work book. This book will not be marked. Clearly indicate which book contains Sections B and C and the rough work.

Additional Material Supplied: A multiple-choice answer sheet is provided. Indicate your answers to section A on this multiple-choice answer sheet. Please read the instructions on this sheet. You MUST fill in your Student AND seat Number on this sheet. Ask the invigilator for assistance in this regard if you require.

Page 1

Section A
Answer on the multiple-choice sheet. Use an HB pencil only. Negative marking will be used. Correct (+2%), not attempted (0%), incorrect (-0.5%). This section is worth 30 marks.

Assuming the following assignments:


a = [3 4 5; 6 4 3; 2 1 2]; b = [5 3 4; 5 6 2; 3 4 9];

1. What will be the result of the following expression?


a(2,:)* b(:,2)./9.^1/2 + b(1,3)/a(3,2)

A) 21

B) 15

C) 7

D) 19

E) 4

2. What will be the result of the following expression?


sum(a>4* mod(b,2))

A) [1 2 2]

B) [2 1 1]

C) [2 1 3]

D) [1 3 2]

E) [3 2 2]

3. What will be the value of the vector C after the following lines of code?
>> a(1:3:end) = [] >> C = length(a)

A) 7
__________

B) 6

C) 5

D) 8

E) 3

4. Which of the following is not a valid MATLAB statement for comparing two strings? A) 'abcd' == 'abcd' B) strcmp('Abcd', 'abcd') C) 'abcd' == 'a' D) 'abcd' == 'abc' E) strcmpi('Abcd', 'abcd') 5. Assuming that x = 'GreenPeace, what will be the result of the mask:
find(x == 'e')

A) [4 B) [0 C) [0 D) [3 E) [3

5 0 0 4 5

7 1 1 6 8

10] 1 0 1 1 0 0 1] 1 0 0 1 0 0 1] 10] 11]

Page 2

Study the following lines of code and answer the questions that follow:
mat = ones(5,5) for i = 1:5 for j = 1:5 p = i+j; if p > 5 break else mat(i,j) = mat(i,j)*3; end end end

6. What is the value of p after the final end statement? A) 8 B) 10 C) 6 D) 5 7. How many times will the break statement be executed? A) 5 B) 15 C) 6 D) 10

E) None of these

E) None of these

8. How many of the original matrix elements are changed after the final end statement? A) 15 B) 9 C) 10 D) 12 E) None of these
__________

9. Assuming the following string assignment is made.


str = 'today USD EUR and GBP are ZAR 7.25 9.87 11.20'

Which of the following is valid in extracting a column vector of the numbers in str? A) B B) B C) B D) B E) B A) 3
__________ = = = = = sscanf( sscanf( sscanf( sscanf( sscanf( str, str, str, str, str, '%s %s %s %s %s %s %s %f %f %f') '%*s %f %f %f') '%*s %*f %*f %*f') '%*s %*s %*s %*s %*s %*s %*s %f %f %f') '%s %s %s %s %s %s %s %f %f %f')

B) 5

C) 4

D) 7

E) None of these

Assuming the following statements are executed in MATLAB.


x = [1 2 3 4 5]; y = [13 24 33 44 56]; h_x = 1:0.5:5;

Page 3

coeffs = polyfit(x,y,4); h_y = polyval(coeffs,h_x);

10. What is the length of the vector coeffs? A) 4 B) 3 C) 5


11. What is the length of the vector h_y?

D) 9

E) None of these

A) 9
__________

B) 11

C) 10

D) 20

E) None of these

12. Assuming that ifn is a valid file name, which of the following is correct code for opening a file reading? A) ih = fopen('r', ifn ); B) ih = fileopen( ifn, 'r' ); C) ih = fopen( ifn, 'read' ); D) ih = fileopen( ifn, 'w' ); E) None of these 13. Assuming that ih is a valid file name, which of the following is valid code for reading in an entire line (including the new line character)? A) s = fgetstring(ih); B) s = fgetl(ih); C) s = fgets(ih); D) s = getstring(ih); E) None of these 14. Select the location of a figure in which 'subplot(2,3,3)' will draw a graph. A) row 2 column 3 3rd graph B) row 1 column 2 2nd graph C) row 1 column 3 3rd graph D) row 2 column 2 2nd graph E) None of these
__________

15. The following MATLAB code is used to produce the graph in the figure below. What is missing in position $$15$$?
x = -5:5; y = x; [xx, yy] = meshgrid(x,y);

$$15$$
xlabel('x'); ylabel('y'); zlabel('z'); axis tight

A) z = y.^2; surf(x, y, z); B) zz = yy.^2; surf(xx, yy, zz);

Page 4

C) zz = xx.^2; surf(xx, yy, zz); D) z = x.^2; surf(x, y, z); E) zz = xy.^2; surf(xx, yy, zz);

25 20 15

z
10 5 0 5 5 0 -5 0 -5 x

Section B
Question 16
A = magic(4) gives the following matrix:

[10 marks]

16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1

a) Write Matlab code to produce matrix B from A by swapping As 2nd and 4th rows. b) Write the code (using the sum function in Matlab) to calculate the sum of the last three elements of the 2nd column of A. c) Write the code to create a matrix C that will contain the elements of A that are greater than 4 and are odd. Give the values in C. d) What will be output to the command window if the following lines of code are executed? First:
k = find(A>=11)

Page 5

Then:
A(k) = -1

Question 17

[20 marks]

If you have a mass hanging from a spring, pulling down on the mass and releasing it causes the mass to oscillate up and down. Eventually, the oscillations will decay until we no longer see them. How fast the oscillations decay is a function of the damping of the system. The response of the system is determined by formulating and solving a differential equation. Equation (1) is the solution to the underdamped condition, in which the damping is low enough to allow the mass to oscillate. = 0 +
0

(1)

where, = the displacement of the mass relative to its original position at any time. 0 = the initial displacement (the displacement at time = 0); indication of how much the mass has been pulled down. = the natural frequency of the system; a measure of how fast the system will oscillate freely. = the damping coefficient; a value between zero and one. = time and = the damped frequency which is given by: = 1 2 Equation (1) is plotted in Figure 1 for the following parameters: a time domain of 0 to 5 seconds, 0 = 3, = 2 radians/second, and = 0.10. is the exponential function and can be calculated using the Matlab function exp 17 a) Write the Matlab code to produce the graph in Figure 1. Use an appropriate resolution for the time vector and use a for loop to in your calculation of the displacement vector (). Ensure that your code includes instructions to produce the appropriate formatting i.e. grid, labels, title. 17 b) Re-write the code to calculate the displacement vector (), but this time use Matlabs vectorization capability.

Page 6

Displacement versus time 3

Displacement [cm]

-1

-2

-3

0.5

1.5

2 2.5 3 Time [seconds]

3.5

4.5

Figure 1

Comment your code marks are awarded for effectiveness of comments Present your work neatly Marks are awarded for the clarity and effectiveness of presentation.

Question 18

[10 marks]

Assume that the following set of temperature measurements is taken from an experiment. Time, sec 0 1.0 2.0 3.0 4.0 5.0 Temperature, C 0 20 60 68 77 110

Write the appropriate MATLAB code to achieve the following. a) Determine the temperature at time = 3.7 seconds. b) Determine the Internal Energy at 0.2 second intervals between 1 and 4 seconds. Use the cubic spline interpolation method. c) Use 'polyfit' to compute the coefficients of a polynomial of degree 3 that best matches the temperature and time interval data.

Page 7

d) Use 'polyval' to interpolate on the above coefficients to plot the polynomial using the time vector produced in question b. e) Plot the polynomial derived in Question d.

Section C
Question 19 [30 marks]

Create a MATLAB program that reads in table of measurements from a text file (InputFile.txt) as shown in Figure 2. After some processing it outputs the data into a text file (OutputFile.txt) as an example shown in Figure 3. The processing script is required to perform the tasks given below. Note: You may not use ANY of MATLABs built in functions except those required for file processing.

Figure 2

Figure 3

Pseudo-code: 1. Access the input file to extract the input matrix. The matrix can be of any size and so do not assume that the matrix dimensions are always as found in the above example.

Page 8

2. Perform error checking to ensure that the matrix is not a 1x1 matrix, i.e. it doesnt just hold a single number. Hint: use the error function which has the following functionality: error('message') displays an error message and returns control to the keyboard. The error message contains the input string message. The message should be: ERROR: The matrix size must be higher than 1x1! 3. Perform the following algorithm. a) Access each row of the matrix b) Pass the row vector to a function called PerfectNums that goes through the vector and extracts the numbers that are perfect numbers. If it cannot find any perfect numbers it should output zero.
Hint: An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6 = 1 + 2 + 3.

c) If the output of PerfectNums either zero or a single number, append this to the end of the row. d) If the output of PerfectNums is a vector (i.e. the row has more than one perfect number) pass the vector to a function called LargestNum which outputs the largest perfect number in the vector passed to it. Append this number to the end of the row being processed. e) Concatenate the (modified) rows to form a resultant matrix C. 4. Write the resultant matrix into the output text file (OutputFile.txt). You can see an example operation of this algorithm in Figure 1 and Figure 2. Comment your work marks are awarded for effectiveness of comments Present your work neatly - Marks are awarded for the clarity and effectiveness of presentation.

Page 9

You might also like