Solutions to the practice quiz
Functions 1
Replace the blanks with the correct functions to accomplish the commented goal.
% compute the row reduced echelon form of A concatenated with b
A = rand(5, 5);
b = rand(5, 1);
x1 = rref([A,b]);
% generate a vector of 100 equally spaced points, given first value = 0, last value = 10.
x2 = linspace(0, 10, 100);
Decision branching 1
Write a conditional statement that will set y to 'positive' if the variable x is positive and set y to 'negative' if
the variable x is negative, and set y to 'zero' if the variable x is exactly zero.
Note: Your variable must EXACTLY named y, otherwise your response will not be graded.
function y = decisionbranching(x)
%Write your conditional statement here
if x == 0
y = 'zero';
elseif x > 0
y = 'positive';
elseif x < 0
y = 'negative';
end
% Randomly generate an integer value between -10 and 10
x = randi([-10, 10])
x = -1
% Test your function
decisionbranching(x)
ans =
'negative'
Debugging
Fix any syntax and runtime errors in the provided code to accomplish the stated goal.
A = [1, 0, 1;
1
2, 3, 1;
0, 1, 1;];
b = [1, 2, 3];
b = b'; % transpose the row vector into a column vector
rrefmat = rref([A, b]);
% I_soln should be a 3x1 vector consisting of the solution to the matrix equation Ax = b
I_soln = rrefmat(:,end)
I_soln = 3×1
-1.2500
0.7500
2.2500
Loops
Complete the code below such that the code will run without error and display the specified output.
Expected Output:
10
19
28
37
46
v_out = [];
for v = 10:9:46
disp(v)
v_out(end+1) = v;
end
10
19
28
37
46
Matrix Indexing
% Consider the following matrix
2
M = [1, 2, 3, 4, 5;
7, 8, 9, 10, 11;
12, 13, 14, 15, 16;
17, 18, 19, 20, 21;
22, 23, 24, 25, 26;];
% Retrieve 13, 14, and 15 from matrix M
a = M(3, 2:4)
a = 1×3
13 14 15
% Retrieve 1, 7, and 12 from matrix M
b = M(1:3, 1)
b = 3×1
1
7
12
% Retrieve 2 and 23 from matrix M
c = M([1, 5], 2)
c = 2×1
2
23
Plotting 3
Based on a physics experiment, we've modelled the position and velocity of a hypothetical particle. Using
this model, we've computed the particle's position and velocity for you and stored it into variables x and v,
respectively.
Plot the position (x) and velocity (v) as a function of time (t) for the particle together (eg. overlaid over top each
other) on a single plot and axis.
Format your plot as follows:
• Position – solid line plot with * markers at every data point
• Speed – no line, with + markers at every data point
Additionally:
• Set the x-axis label to 'Time (sec)'.
syms T
fx = sin(T)^2 + T;
fv = diff(fx, T);
t = [0:0.1:4]';
3
x = double(subs(fx, t));
v = double(subs(fv, t));
hf = figure;
plot(t, x, '-*'); % line plot with * markers
hold on; % keep existing plot on screen and overlay next plot
plot(t, v, '+'); % speed plot with only markers, no line
xlabel('Time (sec)') % set x label
Functions from above
function y = decisionbranching(x)
%Write your conditional statement here
if x == 0
y = 'zero';
elseif x > 0
y = 'positive';
elseif x < 0
y = 'negative';
end
4
end