Bird
0
0

You want to plot the function y = x^2 for x from -3 to 3 with a red dashed line. Which code correctly does this?

hard📝 Application Q15 of 15
MATLAB - 2D Plotting
You want to plot the function y = x^2 for x from -3 to 3 with a red dashed line. Which code correctly does this?
Ax = -3:0.1:3; y = x.^2; plot(x, y, 'blue-')
Bx = -3:0.1:3; y = x.^2; plot(x, y, 'r--')
Cx = -3:0.1:3; y = x^2; plot(y, x, 'r--')
Dx = -3:3; y = x^2; plot(x, y, 'red-dash')
Step-by-Step Solution
Solution:
  1. Step 1: Define x and y correctly

    x must be a vector from -3 to 3 with small steps (e.g., 0.1). y = x squared element-wise uses .^2.
  2. Step 2: Use correct plot style

    The style string 'r--' means red dashed line. x = -3:0.1:3; y = x.^2; plot(x, y, 'r--') uses this correctly.
  3. Step 3: Check other options

    x = -3:3; y = x^2; plot(x, y, 'red-dash') uses invalid syntax for y and style. x = -3:0.1:3; y = x^2; plot(y, x, 'r--') swaps x and y. x = -3:0.1:3; y = x.^2; plot(x, y, 'blue-') uses blue solid line, not red dashed.
  4. Final Answer:

    x = -3:0.1:3; y = x.^2; plot(x, y, 'r--') -> Option B
  5. Quick Check:

    Element-wise power + 'r--' = red dashed line [OK]
Quick Trick: Use .^ for element-wise power and 'r--' for red dashed line [OK]
Common Mistakes:
  • Using ^ instead of .^ for vectors
  • Swapping x and y in plot()
  • Wrong line style string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes