0
0
MATLABdata~20 mins

plot() function basics in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this plot command?
Consider the following MATLAB code snippet:
x = 1:5;
y = x.^2;
plot(x, y)

What does this plot show?
MATLAB
x = 1:5;
y = x.^2;
plot(x, y)
AA line graph with points (1,1), (2,4), (3,9), (4,16), (5,25) connected by straight lines
BA scatter plot with points (1,1), (2,4), (3,9), (4,16), (5,25) but no lines connecting them
CA bar chart with bars at x=1 to 5 with heights 1,4,9,16,25
DA pie chart showing the values 1,4,9,16,25 as slices
Attempts:
2 left
💡 Hint
The plot() function connects points with lines by default.
Predict Output
intermediate
2:00remaining
What color and marker style does this plot use?
Look at this MATLAB code:
x = 0:0.5:2;
y = sin(pi*x);
plot(x, y, 'ro--')

What does the plot look like?
MATLAB
x = 0:0.5:2;
y = sin(pi*x);
plot(x, y, 'ro--')
ABlack stars connected by dash-dot lines
BBlue squares connected by solid lines
CGreen triangles connected by dotted lines
DRed circles connected by dashed lines
Attempts:
2 left
💡 Hint
The format string 'ro--' means red color, circle marker, dashed line.
🔧 Debug
advanced
2:00remaining
Why does this plot command cause an error?
Examine this MATLAB code:
x = 1:5;
y = [2, 4, 6];
plot(x, y)

What error occurs and why?
MATLAB
x = 1:5;
y = [2, 4, 6];
plot(x, y)
ANo error, plot shows points with missing values
BError because y contains even numbers only
CError because x and y vectors have different lengths
DError because x is not a column vector
Attempts:
2 left
💡 Hint
Check if x and y have the same number of elements.
🧠 Conceptual
advanced
2:00remaining
What happens if you call plot with a single vector?
In MATLAB, what does the command plot([3 1 4 1 5]) do?
MATLAB
plot([3 1 4 1 5])
APlots the vector values on the y-axis against their indices on the x-axis
BPlots the vector values on the x-axis against their indices on the y-axis
CRaises an error because two arguments are required
DPlots a horizontal line at y=3
Attempts:
2 left
💡 Hint
When only one vector is given, MATLAB uses indices as x values.
Predict Output
expert
3:00remaining
What is the output of this multi-line plot command?
Consider this MATLAB code:
x = 0:pi/4:pi;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, '-b', x, y2, ':r')

What does the plot show?
MATLAB
x = 0:pi/4:pi;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, '-b', x, y2, ':r')
AOnly sin(x) plotted with a blue dashed line
BA blue solid line for sin(x) and a red dotted line for cos(x)
CA red solid line for sin(x) and a blue dotted line for cos(x)
DOnly cos(x) plotted with a red solid line
Attempts:
2 left
💡 Hint
The format '-b' means blue solid line, ':r' means red dotted line.