Bird
0
0

What will the legend display after running this code?

medium📝 Predict Output Q5 of 15
MATLAB - 2D Plotting
What will the legend display after running this code?
x = 1:3;
y1 = [2 4 6];
y2 = [1 3 5];
plot(x, y1, x, y2);
legend('Line A', 'Line B');
ALegend with 'Line A' for y1 and 'Line B' for y2
BLegend with 'Line B' for y1 and 'Line A' for y2
CNo legend displayed
DError due to incorrect legend syntax
Step-by-Step Solution
Solution:
  1. Step 1: Understand plot with multiple lines

    plot(x, y1, x, y2) plots two lines: first y1, then y2.
  2. Step 2: Match legend labels to plotted lines

    legend('Line A', 'Line B') assigns 'Line A' to first line (y1), 'Line B' to second line (y2).
  3. Final Answer:

    Legend with 'Line A' for y1 and 'Line B' for y2 -> Option A
  4. Quick Check:

    Legend order matches plot order = 'Line A' then 'Line B' [OK]
Quick Trick: Legend labels follow the order of plotted lines [OK]
Common Mistakes:
  • Swapping legend labels order
  • Forgetting legend() call
  • Syntax errors in legend()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes