ENGR 17 Test 1 SOLUTIONS CLOSED COMPUTER – DOUBLE SHEET OF NOTES -- SHORT ANSWER 1) Evaluate the following arithmetic expressions in MATLAB. Assume x=4 and y=2 a. 3 ^ 2 – 1 8 b. 4 + 4 / 2 – 1 5 c. x + 4 * y 12 d. y ^ ( x – 1) 8 2) What does variable x have after the following assignment statements? Each statement is performed in sequence after the preceding one. Current Value of x a. x = 5; 5 b. x = x + 2; 7 c. x = [0: 0.5: 2]; 0 0.5 1 1.5 2 d. x = x + 5; 5 5.5 6 6.5 7 3) Write the command(s) to find the roots of x3 – 7x2 + 40x – 34 = 0 roots([1 -7 40 -34]) 4) Write what is output from the following statements x = [6,3,9]; y = [14,2,9]; a. z = (x < y) 1 0 0 b. z = (x ~= y) 1 1 0 c. z = (x == y) 0 0 1 d. z = x(x < y) 6 e. z = find(x < y) 1 1 5) Consider the following conditional statement: if x >= 9 y = 15* x + 10 elseif x >= 0 y = 10*x + 10 else y = 10 end What will be the value in y after running this code when x contains the following values: a. b. c. d. x = 10 x=5 x = -5 x=0 y=160 y=60 y=10 y=10 6) Write a function file that calculates the area and perimeter of a field defined by a square of size L conjoined with a semi-circle of the same diameter (see picture below). The function should accept L as a parameter and return both the Area and Perimeter. The following example function call: [a, p] = field( 100 ); Will result in variable a containing 13925 and variable p containing 457. Recall that the area of a FULL circle is pi*r^2 and the FULL perimeter is 2*pi*r, where r is the radius of the circle. Your Function File Code: function [a p] = field(L) r = L/2; a = L*L + pi*r^2/2; p = 3*L + pi*r; Figure p6: the field in question 2 7) Suppose x, y, and z contain three numbers. Write some logic (if or if/elseif statements) that displays the variable with the smallest number in it. For example, if x=4, y=2 and z = 8, your code will display the value 2. However, if x=0, y=12 and z = 2, your code will display the value 0. You may use the relational operators <, >, <=, etc and the logical operators && , || or ~ to combine relational expressions as needed. Assume that x, y and z are already defined. Use disp( ) to display the desired variable. if (x < y && x < z) disp(x); elseif ( y < x && y < z) disp(y); else disp(z); end 8) Suppose we have 2 vectors: x = [6,3,9]; y = [14,2,9]; Write statements to compute z, which contains: a. A vector containing x followed by y z = [x y] b. A vector containing the element-by-element product of x and y c. The magnitude of x d. The number of cells in y z = x.*y; z=norm(x); z = length(y) e. The distance in 3D space between x and y z = norm( x-y); 3 9) Suppose A = [ 1 2 3 4; 5 6 7 8; 9 10 11 12] Write what will be shown by the following commands a. A(3, 1) 9 b. A(: , 1) 1 5 9 (arranged vertically) c. A(3, 1:2 ) 9 10 d. A(1:2, 3:4) 3 4 (new line) 7 8 10) The following vector contains the number of hours worked by an employee over an 8 week interval. hours=[40, 65, 50, 30, 20, 55, 45, 40]; Write a single, one-line matlab command using vector syntax to calculate the number of overtime hours worked by the employee. Your command must be expressed in terms of the hours vector, not any specific numbers contained in the example. You may use the vector relational operators, and pre-defined matlab functions like sum and length if you wish. For the above example, it should compute an answer of 55 (that’s 25+10+15+5). overtime = sum( hours( hours>40) – 40) 4