0
0
MATLABdata~20 mins

Multiple plots (hold on) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MATLAB 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 MATLAB code with hold on?

Consider the following MATLAB code that plots two lines using hold on. What will be the final plot display?

MATLAB
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, '-r');
hold on;
plot(x, y2, '-b');
hold off;
AOnly the red sine wave is shown because hold on does not work without hold off.
BOnly the blue cosine wave is shown because the second plot overwrites the first.
CA single plot showing both a red sine wave and a blue cosine wave.
DAn error occurs because hold on and hold off cannot be used together.
Attempts:
2 left
💡 Hint

Think about what hold on does in MATLAB plotting.

Predict Output
intermediate
2:00remaining
What happens if you omit hold on when plotting multiple lines?

Given this MATLAB code, what will the plot show?

MATLAB
x = 1:5;
y1 = x;
y2 = x.^2;
plot(x, y1, '-g');
plot(x, y2, '-m');
ABoth green linear and magenta quadratic curves are shown on the same plot.
BOnly the magenta quadratic curve is shown because the second plot overwrites the first.
CAn error occurs because hold on is missing.
DThe plot shows a green line and a magenta line overlapping exactly.
Attempts:
2 left
💡 Hint

Remember what happens when you call plot twice without hold on.

🔧 Debug
advanced
2:00remaining
Why does this MATLAB code plot only one line despite using hold on?

Examine the code below. It is intended to plot two lines on the same figure, but only one line appears. What is the cause?

MATLAB
x = 0:0.5:5;
plot(x, x, '-k');
hold on
plot(x, x.^2, '-r');
hold off;
AThe code runs fine and both lines appear; the problem is elsewhere.
BMissing semicolon after <code>hold on</code> causes the second plot to overwrite the first.
CThe first plot command is overwritten because <code>hold on</code> was not called before it.
DThe <code>hold on</code> command is missing parentheses, so it is not executed properly.
Attempts:
2 left
💡 Hint

Check the syntax of the hold on command.

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in MATLAB when using hold on?

Identify the code snippet that will cause a syntax error in MATLAB.

Aplot(x, y1) hold on plot(x, y2) hold off
Bplot(x, y1); hold on; plot(x, y2);
Chold on; plot(x, y1); plot(x, y2); hold off;
Dplot(x, y1); hold on; plot(x, y2); hold off;
Attempts:
2 left
💡 Hint

Look for missing semicolons or missing command separators.

🚀 Application
expert
3:00remaining
How many lines are visible after running this MATLAB code?

Consider this MATLAB code snippet:

x = 1:4;
plot(x, x, '-o');
hold on;
plot(x, x.^2, '-x');
hold off;
plot(x, x.^3, '-s');

How many lines will be visible on the final figure?

ATwo lines: linear and quadratic only.
BTwo lines: quadratic and cubic only.
CThree lines: linear, quadratic, and cubic.
DOne line: cubic only.
Attempts:
2 left
💡 Hint

Remember what hold off does after plotting.