Line styles, markers, and colors in MATLAB - Time & Space Complexity
We want to understand how the time to draw a plot changes when we add line styles, markers, and colors.
How does adding these features affect the work MATLAB does as the plot size grows?
Analyze the time complexity of the following MATLAB code that plots points with different styles.
x = 1:n;
y = rand(1, n);
plot(x, y, '-o', 'Color', 'r');
This code plots n points connected by a red line with circle markers.
Look for repeated actions in the code.
- Primary operation: Drawing each point and connecting lines with styles and markers.
- How many times: Once for each of the n points.
The time to draw grows as we add more points.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 drawing steps |
| 100 | About 100 drawing steps |
| 1000 | About 1000 drawing steps |
Pattern observation: The work grows directly with the number of points.
Time Complexity: O(n)
This means the time to draw increases in a straight line as you add more points.
[X] Wrong: "Adding colors and markers makes the drawing time grow faster than the number of points."
[OK] Correct: Colors and markers add a small fixed cost per point, but the main time still grows linearly with the number of points.
Understanding how drawing time grows helps you write efficient plotting code and explain performance in real projects.
"What if we added a loop to plot multiple lines with different styles? How would the time complexity change?"