Visualizing 3D Surfaces with mesh and surf in MATLAB
📖 Scenario: You are working as a data analyst and need to visualize a simple 3D surface to understand how values change over two variables. You will use MATLAB's mesh and surf functions to create clear 3D plots.
🎯 Goal: Build a MATLAB script that creates a grid of points, calculates a surface function over that grid, and then displays the surface using both mesh and surf plots.
📋 What You'll Learn
Create two vectors for X and Y coordinates using linspace
Generate a grid of points using meshgrid
Calculate Z values using the function Z = sin(X) .* cos(Y)
Plot the surface using mesh
Plot the surface using surf
💡 Why This Matters
🌍 Real World
Scientists and engineers often visualize 3D surfaces to understand how two variables affect a result, such as temperature over an area or elevation on a map.
💼 Career
Knowing how to create 3D surface plots in MATLAB is useful for data analysis, simulation, and presenting complex data clearly in engineering and research jobs.
Progress0 / 4 steps
1
Create X and Y coordinate vectors
Create two vectors called x and y using linspace. Set x from 0 to 2*pi with 30 points, and y from 0 to 2*pi with 30 points.
MATLAB
Hint
Use linspace(start, end, number_of_points) to create evenly spaced points.
2
Generate grid of points with meshgrid
Use meshgrid with the vectors x and y to create matrices X and Y.
MATLAB
Hint
meshgrid creates matrices for X and Y coordinates to evaluate functions over a grid.
3
Calculate Z values for the surface
Calculate matrix Z using the formula Z = sin(X) .* cos(Y).
MATLAB
Hint
Use element-wise multiplication with .* to combine sin(X) and cos(Y).
4
Plot the surface using mesh and surf
Use mesh(X, Y, Z) to create a wireframe plot, then use figure to open a new window and surf(X, Y, Z) to create a colored surface plot. Add shading interp after surf for smooth colors.
MATLAB
Hint
Use figure to open a new plot window before surf. shading interp smooths the colors on the surface.
Practice
(1/5)
1. What is the main difference between mesh and surf functions in MATLAB when plotting 3D surfaces?
easy
A. mesh creates a wireframe surface, while surf creates a colored surface.
B. mesh plots 2D graphs, while surf plots 3D graphs.
C. mesh only works with matrices, surf only works with vectors.
D. mesh colors the surface, surf shows only edges.
Solution
Step 1: Understand the function outputs
mesh draws a grid-like wireframe of the surface without filling colors.
Step 2: Compare with surf
surf draws a colored surface that fills the area between grid lines, showing height with color.
Final Answer:
mesh creates a wireframe surface, while surf creates a colored surface. -> Option A
4. Identify the error in this MATLAB code snippet:
X = 0:0.5:2; Y = 0:0.5:2; Z = X.^2 + Y.^2; surf(X, Y, Z);
medium
A. Z must be a matrix matching sizes of X and Y, not vectors.
B. surf requires only two inputs, not three.
C. X and Y must be row vectors, not column vectors.
D. The operator .^ is invalid for vectors.
Solution
Step 1: Check dimensions of X, Y, and Z
X and Y are vectors, but Z is calculated element-wise and is also a vector, not a matrix matching grid points.
Step 2: Understand surf input requirements
surf expects X, Y, Z matrices of the same size, usually created by meshgrid. Here, Z is not a matrix matching X and Y.
Final Answer:
Z must be a matrix matching sizes of X and Y, not vectors. -> Option A
Quick Check:
surf needs matching matrix sizes = C [OK]
Hint: Use meshgrid to create X, Y matrices before surf [OK]
Common Mistakes:
Passing vectors instead of matrices to surf
Misunderstanding element-wise operations
Thinking surf accepts vector inputs directly
5. You want to plot the surface of the function z = sin(x) * cos(y) over the range x = -pi to pi and y = -pi to pi. Which MATLAB code correctly creates a colored surface plot using surf?
hard
A. x = -pi:pi; y = -pi:pi; Z = sin(x) * cos(y); surf(x, y, Z);
B. x = linspace(-pi, pi, 50); y = linspace(-pi, pi, 50); Z = sin(x) .* cos(y); surf(x, y, Z);
C. x = linspace(-pi, pi, 50); y = linspace(-pi, pi, 50); [X, Y] = meshgrid(x, y); Z = sin(X) .* cos(Y); surf(X, Y, Z);
D. X = -pi:pi; Y = -pi:pi; Z = sin(X) + cos(Y); mesh(X, Y, Z);
Solution
Step 1: Create grid points for x and y
Use linspace to create vectors from -pi to pi with enough points, then use meshgrid to create matrices X and Y.
Step 2: Calculate Z values element-wise
Calculate Z as sin(X) .* cos(Y) using element-wise multiplication to get matrix Z matching X and Y.
Step 3: Plot with surf
Use surf(X, Y, Z) to plot the colored surface correctly.
Final Answer:
x = linspace(-pi, pi, 50); y = linspace(-pi, pi, 50); [X, Y] = meshgrid(x, y); Z = sin(X) .* cos(Y); surf(X, Y, Z); -> Option C
Quick Check:
meshgrid + element-wise .* + surf = B [OK]
Hint: Use meshgrid and element-wise .* for surface plots [OK]
Common Mistakes:
Using * instead of .* for element-wise multiplication
Not using meshgrid to create X and Y matrices
Using vectors directly in surf without matching sizes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.