A. Two plots side by side: first with points (1,2),(2,4),(3,6), second with (1,1),(2,3),(3,5).
B. One plot with both data sets overlaid.
C. Error because subplot indices are incorrect.
D. Two plots stacked vertically.
Solution
Step 1: Understand subplot grid
subplot(1,2,1) creates a grid with 1 row and 2 columns, selecting the first panel.
Step 2: Plot data in each panel
The first plot shows points (1,2), (2,4), (3,6). The second subplot(1,2,2) plots points (1,1), (2,3), (3,5).
Final Answer:
Two plots side by side: first with points (1,2),(2,4),(3,6), second with (1,1),(2,3),(3,5). -> Option A
Quick Check:
subplot(1,2,p) = two side-by-side plots [OK]
Hint: subplot(1,2,p) means two plots side by side [OK]
Common Mistakes:
Thinking plots overlay in one panel
Assuming vertical stacking instead of horizontal
Misreading subplot indices
4. Identify the error in this MATLAB code snippet:
subplot(2,2,5);
plot(rand(5));
medium
A. plot function cannot be used inside subplot.
B. Panel index 5 is invalid for a 2x2 grid.
C. Missing semicolon after subplot command.
D. rand(5) is incorrect syntax.
Solution
Step 1: Check subplot grid size
A 2x2 grid has only 4 panels numbered 1 to 4.
Step 2: Validate panel index
Using 5 as the panel index is invalid and causes an error.
Final Answer:
Panel index 5 is invalid for a 2x2 grid. -> Option B
Quick Check:
Panel number must be ≤ rows*columns [OK]
Hint: Panel number must not exceed total grid cells [OK]
Common Mistakes:
Using panel number larger than grid size
Thinking semicolon affects subplot indexing
Believing plot can't be inside subplot
5. You want to create a 2x2 grid of plots showing sine waves with different frequencies. Which code correctly sets up the subplots and plots the waves?
hard
A. subplot(2,2);
for k = 1:4
plot(sin(k*x));
end
B. for k = 1:4
subplot(4,1,k);
x = linspace(0,2*pi,100);
plot(x, sin(k*x));
end
C. for k = 1:4
subplot(2,2,k);
x = linspace(0,2*pi,100);
plot(x, sin(k*x));
end
D. for k = 1:4
subplot(2,2,k+1);
x = linspace(0,2*pi,100);
plot(x, sin(k*x));
end
Solution
Step 1: Set up correct subplot grid
subplot(2,2,k) correctly creates a 2x2 grid and selects each panel from 1 to 4.
Step 2: Plot sine waves with increasing frequency
Inside the loop, x is defined and sin(k*x) is plotted in each subplot panel.
Step 3: Check other options for errors
for k = 1:4
subplot(4,1,k);
x = linspace(0,2*pi,100);
plot(x, sin(k*x));
end uses 4x1 grid, not 2x2. subplot(2,2);
for k = 1:4
plot(sin(k*x));
end calls subplot once without panel index. for k = 1:4
subplot(2,2,k+1);
x = linspace(0,2*pi,100);
plot(x, sin(k*x));
end uses k+1 which causes invalid panel 5.
Final Answer:
for k = 1:4
subplot(2,2,k);
x = linspace(0,2*pi,100);
plot(x, sin(k*x));
end -> Option C
Quick Check:
Loop with subplot(2,2,k) plots 4 panels correctly [OK]
Hint: Use subplot(2,2,k) inside loop k=1 to 4 [OK]
Common Mistakes:
Using wrong subplot grid size
Not specifying panel index in subplot
Using invalid panel numbers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.