Practice Test 1

advertisement
ENGR 17
Test 1
Name___________________
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
b. 4 + 4 / 2 – 1
c. x + 4 * y
d. y ^ ( x – 1)
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;
c. x = [0: 0.5: 2];
d. x = x + 5;
3) Write the command(s) to find the roots of x3 – 7x2 + 40x – 34 = 0
4) Write what is output from the following statements
x = [6,3,9]; y = [14,2,9];
a. z = (x < y)
b. z = (x ~= y)
c. z = (x == y)
d. z = x(x < y)
e. z = find(x < y)
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
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:
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.
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
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
3
e. The distance in 3D space between x and y
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)
b. A(: , 1)
c. A(3, 1:2 )
d. A(1:2, 3:4)
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).
4
Download