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:
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.
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.
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.
Final Answer:
x = -3:0.1:3; y = x.^2; plot(x, y, 'r--') -> Option B
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
Master "2D Plotting" in MATLAB
9 interactive learning modes - each teaches the same concept differently