Computing for Scientists & Engineers
CMPS 1371 (Hawkins)
Exam I: Chapter One to Chapter Eight
Questions are 3 points each, unless otherwise indicated Name ______
Show your work!
True / False:
1. A function in Matlab can return multiple variables.
2. The file extension of Matlab script files is “.m”
3. You can create a cell array using either parentheses or braces
Answer the following:
4.What vector is created from the following Matlab command:
> v = 20:2:30
5.The two lines below are entered at the command prompt:
> u = [2 4 6]; v = [1 2 3];
> w = u ./ v
What is the output of w?
6.Given the vectorx = [3 1 5 7 9 2 6]
Determine the following outputs:
a)x (3)b)x (1:4)
c)x ([1 6 2 1 1])d)sum(x)
Answer the following:
7.Given x = [3 15 9 12 -1 0 -12 9 6 1], provide the command(s) that will:
a)add 3 to every element
b)remove the values of x that are negative
c)multiply the values of x that are even by 5
8.Matlab defines a polynomial by storing the coefficients in a vector starting with the highest degree term and finishing with the constant term. Thus, the polynomial: x2 – 3x + 2 is stored in Matlab as p = [1 -3 2].
Write an m-function, called psum, that will accept two polynomials, p1 and p2, and compute their sum, p3. Your function should be used as: p3 = psum(p1, p2)
Example p1 = [2 1];% 2x + 1
p2 = [1 -3 2];% x2 – 3x + 2
p3 = psum(p1, p2)% sum = x2 – x + 3 or [1 -1 3]
p3 =
1 -1 3
Answer the following:
9.Given A = {‘hello world’, [1 2 3], ‘cmps1371’}
If you wanted to display the vector [1 2 3], what would you type on the command line?
10.What is the value of “a” when this script is executed?
a = 0;
b = [1 1 0 1 0];
for i = b(1:end-1)
a = a + i;
end
11.Rewrite the following statements to use only one if statement.
if x < y
if z < 10
w = x * y * z
end
end
12.You are given a variable named “sideCount” specifying the number of sides on a geometric figure. You need a string containing the name of the corresponding shape. Write the instructions to calculate the shape based on the table below:
Value of sideCount / Return stringLess than 3 / ‘Not a shape’
3 / ‘Triangle’
4 / ‘Quadrilateral’
5 / ‘Pentagon’
Greater than 5 / ‘Other’
Multiple Choice:
13. What is the output of the following MATLAB commands?
> x = [ ];
> for i = 1:3
> x = [x i];
> end;
> x
a)[3 2 1]
b)[1 2 3]
c)[1 3 6]
d)none of the above
14. What is the result of the following MATLAB commands?
> x = ones(1,3);
> y = zeros(1,3);
> x + y
a)[0 0 1]
b)[0 1 1]
c)[0 1 0]
d)[1 1 1]
15. Knowing that MATLAB function ‘rand’ produces floating-point numbers (i.e., decimal values) in the interval [0,1], which of the following commands will you use to produce an integer random number in the range [10,50]?
a)> fix(10+40*rand())
b)> fix(10+50*rand())
c)> 10 + 40*rand()
d)> none of the above
16. What is the effect of the command clc in Matlab?
a)Clears Matlab memory
b)Resets Matlab path
c)Clears the main screen
d)Creates a new graphics window
17. Consider the following vector in Matlab:vec = [1 0 0 0 0];
Which of the following will produce an error?
a) vec(6) = 1;
b) vec(0) = 1;
c) vec([4 5]) = 1;
d) vec = [[6 [5] vec]];
Multiple Choice:
18. Assume that the following variables are assigned the following values:
A = 1;
B = 1;
C = 0;
D = 0;
What is the result of the following:(A | (B & C)) & ~D
a) 2;
b) 1
c) 0
d) An error occurs
19. Consider the following vector in Matlab:vec = [1 0 0 0 0];
Which of the following will produce an error?
a) vec(6) = 1;
b) vec(0) = 1;
c) vec([4 5]) = 1;
d) vec = [[6 [5] vec]];
Bonus(5 points)
The Fibonacci numbers are computed according to the following relation:
Fn = Fn-1 + Fn-2with F0 = F1 = 1.{1, 1, 2, 3, 5, 8, 13, 21,…}
Write a function that will return the nth Fibonacci number.
1