contour plots in MATLAB - Time & Space Complexity
When we create contour plots in MATLAB, the program calculates many points to draw lines showing levels of a surface.
We want to understand how the time to draw these plots grows as the data size increases.
Analyze the time complexity of the following code snippet.
% Create grid points
[x, y] = meshgrid(linspace(-2, 2, n), linspace(-2, 2, n));
% Define function values on grid
z = x.^2 + y.^2;
% Draw contour plot
contour(x, y, z, 10);
This code creates a grid of points, computes values for each point, and draws contour lines for 10 levels.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calculating values for each point in the n-by-n grid.
- How many times: Once for each of the n² points.
As the grid size n grows, the number of points grows by n squared, so the work grows quickly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 100 |
| 100 | 10,000 |
| 1000 | 1,000,000 |
Pattern observation: Doubling n makes the work about four times bigger because we have n by n points.
Time Complexity: O(n^2)
This means the time to create the contour plot grows roughly with the square of the grid size.
[X] Wrong: "The time grows linearly with n because we just loop once over the data."
[OK] Correct: The data is a grid with n rows and n columns, so the total points are n times n, not just n.
Understanding how plotting time grows helps you explain performance when working with large data sets or visualizations.
"What if we increased the number of contour levels from 10 to 100? How would the time complexity change?"
Practice
Solution
Step 1: Understand contour plot purpose
A contour plot shows curves that connect points with equal values on a grid.Step 2: Compare with other plot types
3D surface plots show height as a surface, bar charts show bars, scatter plots show points, so they differ.Final Answer:
Lines connecting points with the same value -> Option BQuick Check:
Contour plot = lines of equal value [OK]
- Confusing contour plots with 3D surface plots
- Thinking contour plots show individual points
- Mixing contour plots with bar charts
Solution
Step 1: Recall MATLAB contour syntax
The function to create contour plots is contour(X, Y, Z).Step 2: Eliminate incorrect functions
plot() is for 2D plots, contourplot() is not a MATLAB function, surface() creates 3D surfaces.Final Answer:
contour(X, Y, Z) -> Option AQuick Check:
Use contour() for contour plots [OK]
- Using plot() instead of contour()
- Typing contourplot() which is invalid
- Using surface() which makes 3D plots
X = 1:3; Y = 1:3; [Xg,Yg] = meshgrid(X,Y); Z = Xg.^2 + Yg.^2; contour(Xg, Yg, Z, 3);
Solution
Step 1: Understand meshgrid and Z calculation
meshgrid creates grids Xg and Yg; Z is sum of squares, a smooth surface.Step 2: Interpret contour call with 3 levels
contour with 3 as last argument draws 3 contour lines at different Z levels.Final Answer:
A contour plot with 3 contour lines representing levels of Z -> Option DQuick Check:
contour with level count = contour lines count [OK]
- Thinking contour creates 3D surface plots
- Believing contour requires vectors, not matrices
- Confusing contour with scatter plots
X = 1:5; Y = 1:5; Z = X + Y; contour(X, Y, Z);
Solution
Step 1: Check dimensions of X, Y, Z
X and Y are vectors; Z is vector sum, not a matrix grid.Step 2: Understand contour input requirements
contour needs Z as matrix matching X, Y grid sizes; here Z is vector, causing error.Final Answer:
Z must be a matrix matching grid size, not a vector -> Option CQuick Check:
Z size mismatch causes error [OK]
- Passing vector Z instead of matrix
- Assuming contour accepts vector inputs
- Ignoring dimension mismatch errors
Solution
Step 1: Recall contour level specification syntax
To specify exact contour levels, pass a vector of levels as the fourth argument.Step 2: Evaluate options for specifying levels
contour(X, Y, Z, [5 10 15]) uses vector [5 10 15] correctly; contour(X, Y, Z, 3) sets number of lines, not levels; options C and D use invalid syntax.Final Answer:
contour(X, Y, Z, [5 10 15]) -> Option AQuick Check:
Use vector of levels as 4th argument [OK]
- Using number instead of vector for levels
- Trying unsupported name-value pairs
- Confusing level count with specific levels
Ready to prove it?
Pass the test and this pattern moves unknown
