What is the basic purpose of the plot() function in MATLAB?
The plot() function is used to create 2D line plots to visualize data points connected by lines.
Click to reveal answer
beginner
How do you plot the points (1,2), (2,4), and (3,6) using plot()?
Use plot([1 2 3], [2 4 6]) to plot x-coordinates [1 2 3] against y-coordinates [2 4 6].
Click to reveal answer
intermediate
What does the command plot(x, y, 'r--') do?
It plots the data points with a red dashed line connecting them. 'r' means red color, '--' means dashed line style.
Click to reveal answer
beginner
How can you add labels to the x-axis and y-axis in a plot?
Use xlabel('label') and ylabel('label') to add text labels to the x-axis and y-axis respectively.
Click to reveal answer
intermediate
What happens if you call plot() multiple times without hold on?
Each new plot() call clears the previous plot, so only the last plot is visible unless you use hold on.
Click to reveal answer
Which command plots a blue solid line connecting points (1,1), (2,4), and (3,9)?
Aplot([1 2 3], [1 4 9], 'k.')
Bplot([1 2 3], [1 4 9], 'r--')
Cplot([1 2 3], [1 4 9], 'b-')
Dplot([1 2 3], [1 4 9], 'g:')
✗ Incorrect
The 'b-' option means blue solid line. Other options specify different colors or line styles.
What does plot(x, y) do if x is omitted and only plot(y) is called?
APlots y against its index values starting at 1
BThrows an error
CPlots y against zeros
DPlots y against random values
✗ Incorrect
If only y is given, MATLAB plots y values against their indices (1, 2, 3, ...).
How do you keep multiple plots visible on the same figure?
AUse <code>hold on</code>
BUse <code>hold off</code>
CCall <code>plot()</code> multiple times without any command
DUse <code>clear</code>
✗ Incorrect
hold on keeps the current plot so new plots add to it instead of replacing.
Which command adds a title to a plot?
Axlabel('My Plot')
Blegend('My Plot')
Cylabel('My Plot')
Dtitle('My Plot')
✗ Incorrect
title() adds a title above the plot.
What is the output of plot([1 2 3], [4 5 6], 'ko')?
ARed dashed line connecting points
BBlack circle markers at points without connecting lines
CBlack solid line connecting points
DBlue dotted line connecting points
✗ Incorrect
'k' means black color, 'o' means circle markers, no line style means no connecting lines.
Explain how to create a simple 2D line plot in MATLAB using the plot() function.
Think about how you give x and y points to <code>plot()</code>.
You got /3 concepts.
Describe how to customize the color and style of a plot line using the plot() function.
Use a string like 'r--' to set red dashed lines.
You got /3 concepts.
Practice
(1/5)
1. What does the plot() function do in MATLAB?
easy
A. It calculates the sum of two numbers.
B. It clears the command window.
C. It saves data to a file.
D. It creates a graph to visually display data points.
Solution
Step 1: Understand the purpose of plot()
The plot() function is used to draw graphs in MATLAB, which helps visualize data points.
Step 2: Compare with other options
Options A, C, and D describe unrelated functions like summing numbers, saving files, or clearing the window, which plot() does not do.
Final Answer:
It creates a graph to visually display data points. -> Option D
Quick Check:
plot() = graph display [OK]
Hint: Remember: plot() always draws graphs [OK]
Common Mistakes:
Confusing plot() with data calculation functions
Thinking plot() saves files
Assuming plot() clears the screen
2. Which of the following is the correct syntax to plot y versus x in MATLAB?
easy
A. plot(x:y)
B. plot(y, x)
C. plot(x, y)
D. plot(x + y)
Solution
Step 1: Recall the syntax of plot()
The correct syntax to plot y versus x is plot(x, y), where x and y are vectors of the same length.
Step 2: Check other options
plot(y, x) reverses the order, plotting x versus y, which is incorrect. plot(x:y) uses a colon operator incorrectly. plot(x + y) tries to plot the sum, which is invalid.
Final Answer:
plot(x, y) -> Option C
Quick Check:
plot(x, y) = correct syntax [OK]
Hint: Always put x first, then y in plot() [OK]
Common Mistakes:
Swapping x and y arguments
Using colon operator inside plot()
Trying to plot sums instead of vectors
3. What will be the output of the following MATLAB code?
x = 1:3; y = [2 4 6]; plot(x, y)
medium
A. A plot with points (1,2), (2,4), (3,6) connected by lines.
B. A plot with points (2,1), (4,2), (6,3) connected by lines.
C. An error because x and y have different lengths.
D. A plot with points (1,1), (2,2), (3,3) connected by lines.
Solution
Step 1: Analyze vectors x and y
x is [1 2 3] and y is [2 4 6], both have length 3, so they can be plotted against each other.
Step 2: Understand plot output
The plot will connect points (1,2), (2,4), and (3,6) with lines.
Final Answer:
A plot with points (1,2), (2,4), (3,6) connected by lines. -> Option A
Quick Check:
plot(x, y) = points (1,2), (2,4), (3,6) [OK]
Hint: Match x and y points by position in vectors [OK]
Common Mistakes:
Mixing up x and y coordinates
Assuming error due to vector length mismatch
Confusing point coordinates
4. Identify the error in this MATLAB code:
x = 1:5; y = [2 4 6]; plot(x, y)
medium
A. Vectors x and y have different lengths causing an error.
B. plot() function is misspelled.
C. x and y must be column vectors, not row vectors.
D. No error; the plot will display correctly.
Solution
Step 1: Check lengths of x and y
x has length 5, y has length 3. plot() requires vectors of equal length.
Step 2: Understand consequence
Because lengths differ, MATLAB will produce an error when trying to plot.
Final Answer:
Vectors x and y have different lengths causing an error. -> Option A
Quick Check:
Equal length vectors needed [OK]
Hint: Ensure x and y vectors have same length before plotting [OK]
Common Mistakes:
Ignoring vector length mismatch
Thinking row vs column vector matters here
Assuming plot() spelling error
5. 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
A. x = -3:0.1:3; y = x.^2; plot(x, y, 'blue-')
B. x = -3:0.1:3; y = x.^2; plot(x, y, 'r--')
C. x = -3:0.1:3; y = x^2; plot(y, x, 'r--')
D. x = -3:3; y = x^2; plot(x, y, 'red-dash')
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]
Hint: 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
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.