%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%
%% CCCCCCCCC EEEEEEEEEEEE EEEEEEEEEEEE 222222222222 00000 %%
%% CCCCCCCC EEEEEEEEE EEEEEEEEE 222222222222 000 000 %%
%% CCCCCC EEE EEE 222 222 000 000 %%
%% CCCC EEE EEE 2 2222 000 000 %%
%% CCC EEEEEEEEE EEEEEEEEE 2222 000 000 %%
%% CCC EEEEEEEEE EEEEEEEEE 2222 000 000 %%
%% CCCC EEE EEE 2222 000 000 %%
%% CCCCCC EEE EEE 2222 000 000 %%
%% CCCCCCCC EEEEEEEEE EEEEEEEEE 222222222222 000 000 %%
%% CCCCCCCCC EEEEEEEEEEEE EEEEEEEEEEEE 222222222222 00000 %%
%% %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%
%% QUIZ 1: Practice problems %%
%% %%
%% Prof. Jasper A. Vrugt %%
%% Civil and Environmental Engineering %%
%% UC-Irvine %%
%% ISEB, 3068 %%
%% Email: jasper@uci.edu %%
%% %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Question 1: We define a variable v in MATLAB as follows, v = 1:2:12. What does the command size(v) return
in the command prompt?
a. 1 6
b. 7 1
c. 1 7
d. MATLAB gives an error
Question 2: We define a variable v in MATLAB as follows, v = 1:2:11'. What does the command size(v) return
in the command prompt?
a. 7 1
b. 1 7
c. 1 6
d. MATLAB gives an error
Question 3: Consider two matrices, v and w which both have 3 rows and 7 columns. I want to divide w by v
(element-wise) and assign the output to a variable u. What is the correct statement in the command prompt?
a. u = v/w
b. u = w/v
c. u = w./v;
d. None of the above statements is correct
Question 4: Consider a matrix v of size 3 by 6 and a second matrix w of size 6 by 3. Which of the following
statements in MATLAB is illegal? (illegal = incorrect = does not work)
1
a. u = v/w
b. u = w/v
c. u = w./v
d. u = v'./w
Question 5: In MATLAB, how do you precede written text (within-code explanatory comments) in your program
(script)?
a. End your text comment with a colon, ':'
b. Precede your text comment with a percent '%' symbol
c. End your text comment with the tilde '~' symbol
d. Precede your text comment with a semicolon, ';'
e. All of the above answers are wrong
Question 6: In MATLAB, how do you suppress output writing to the screen?
a. End your statement with a colon, ':'
b. Precede your statement with a percent '%' symbol
c. End your statement with the tilde '~' symbol
d. Precede your statement with a semicolon, ';'
e. All of the above answers are wrong
Question 7: In MATLAB, what does the following statement produce as output, u = ones(5) ?
a. A horizontal vector u with 5 elements
b. A vertical vector u with 5 elements
c. Scalars u1, u2, u3, u4, u5 with each a value of 1
d. A 5-by-5 matrix u with values of one
e. None of the above answers is correct
Question 8: In MATLAB, I want to plot the sine function in a solid green line for x values between 1/2π and
3/2π. Which statement is correct?
a. x = 1/2*pi:1.5*pi; plot(x,sin(x),'g');
b. x = 1/2*pi:1.5*pi; y = plot(x,sin(x),'gr');
c. x = 1/2*pi:pi/49:1.5*pi; y = sin(x); plot(x,y,'g');
d. x = linspace(1/2*pi,1.5*pi); plot(x,sin(x),'gr');
e. All of the above answers are wrong
Question 9: In MATLAB, I want to plot the ratio of the cosine and sine functions using 50 values of x on -2 ≤
x ≤ 2 and a solid blue line. Which statement is correct?
a. x = -2:4/50:2; y = sin(x); z = cos(x); plot(x,z./y,'b');
b. x = -2:4/50:2; plot(x,cos(x)/sin(x),'b');
c. x = linspace(-2,2,50); plot(x,cos(x)/sin(x),'b.');
d. x = linspace(-2,2,50); y = sin(x); z = cos(x); plot(x,z./y,'b');
e. All of the above answers are correct
Question 10: In MATLAB, I have several variables in memory. Of these, I want to save the variables x (=
vector), y (= matrix) and A (= scalar) to a binary file called 'quiz1'. Which statement is correct?
a. save quiz1.mat x y A
2
b. save 'quiz1.mat' x y A
c. save quiz1.txt x y A -ascii
d. save quiz1.mat xyA
e. This is not possible as variables x, y and A are not of the same size
Question 11: Imagine matrix B = [2 3 4 5; 3 4 5 2; 4 5 6 1] in MATLAB. I want to extract the maximum value of
each column of B and store these values in a vector m. Which statement is correct?
a. m = max(B)
b. m = max(B,2)
c. m = max(B,[],2)
d. m = max[B,2]
e. None of the above statements is correct
Question 12: Imagine a 8x8 matrix B in MATLAB. I want to extract the elements of this matrix immediately
above the main diagonal and store these values in a vector c. Which statement is correct?
a. c = diag(B)
b. c = diag(B,[],1)
c. c = diag(B,-1)
d. c = diag(B,[],'above')
e. None of the above statements is correct
Question 13: Imagine a 5x5 matrix B in MATLAB. I want to extract all elements of this matrix below the main
diagonal and store these values in a vector c. Which statement is correct?
a. c = diag(B)
b. c = diag(B,[],1)
c. c = diag(B,-1)
d. c = [B(2,1);B(3,2);B(3,3);B(4,3)]
e. None of the above statements is correct
Question 14: I execute the following statement in the MATLAB command prompt, A = diag([3 4 5 6],0) +
diag([1 2 3],1) + diag([1 2 3],-1). Then I write, c = min(A,[],2). What is the content of variable c?
a. c = [ 0 0 1 2 ]
b. c = [ 1 2 0 0 ]
c. c = [ 0 0 0 0 ]'
d. Variable c does not exist as the 2nd statement, c = min(A,[],2), does not work.
Question 15: Suppose I want to compute the product z of the expontial, f(x), and tangent, g(x), functions in
MATLAB for 1 ≤ x ≤ 3π. We create equally-spaced x values, x = linspace(1,3*pi,200). Then, what is the correct
statement for the product, z(x) = f(x)g(x)?
a. z = exp(x) .* tan(x)
b. z = exp(x) * tangent(x)
c. z = expon(x) * tan(x)
d. z = expon(x) .* tangent(x)
Question 15 (continued) what is the size of variable z?
a. 1 by 1 (scalar)
3
b. 100 by 1 (column vector)
c. 1 by 100 (row vector)
d. 100 by 100 (matrix)
Question 17: Suppose A is a n x m matrix of values. I want to extract the minimum value of this matrix and
return this scalar in a variable c. How do I do this in MATLAB?
a. c = min(A)
b. c = min(A,'all')
c. c = min(min(A))
d. All of the above answers are wrong