Bird
Raised Fist0
MATLABdata~10 mins

mesh and surf for surfaces in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - mesh and surf for surfaces
Define X, Y grid points
↓
Calculate Z values for each (X,Y)
↓
Call mesh(X,Y,Z) to draw wireframe
↓
Call surf(X,Y,Z) to draw colored surface
↓
View 3D plot with grid or color shading
First, create grid points X and Y, then compute Z values. Use mesh to draw a wireframe surface and surf to draw a colored surface.
Execution Sample
MATLAB
x = -2:0.5:2;
y = -2:0.5:2;
[X,Y] = meshgrid(x,y);
Z = X.^2 + Y.^2;
mesh(X,Y,Z);
surf(X,Y,Z);
This code creates a grid from -2 to 2, calculates Z as X squared plus Y squared, then draws a wireframe mesh and a colored surface.
Execution Table
StepActionVariablesResult/Output
1Create vector x from -2 to 2 step 0.5xx = [-2 -1.5 -1 -0.5 0 0.5 1 1.5 2]
2Create vector y from -2 to 2 step 0.5yy = [-2 -1.5 -1 -0.5 0 0.5 1 1.5 2]
3Create grid matrices X and Y using meshgrid(x,y)X, YX and Y are 9x9 matrices with grid coordinates
4Calculate Z = X.^2 + Y.^2 element-wiseZZ is 9x9 matrix with squared sums
5Call mesh(X,Y,Z)PlotWireframe 3D plot appears showing surface shape
6Call surf(X,Y,Z)PlotColored 3D surface plot appears showing surface shape
7End of script-Plots displayed, execution stops
💡 All commands executed, plots rendered, script ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
xundefined[-2 -1.5 -1 -0.5 0 0.5 1 1.5 2][-2 -1.5 -1 -0.5 0 0.5 1 1.5 2][-2 -1.5 -1 -0.5 0 0.5 1 1.5 2][-2 -1.5 -1 -0.5 0 0.5 1 1.5 2][-2 -1.5 -1 -0.5 0 0.5 1 1.5 2]
yundefinedundefined[-2 -1.5 -1 -0.5 0 0.5 1 1.5 2][-2 -1.5 -1 -0.5 0 0.5 1 1.5 2][-2 -1.5 -1 -0.5 0 0.5 1 1.5 2][-2 -1.5 -1 -0.5 0 0.5 1 1.5 2]
Xundefinedundefinedundefined9x9 matrix grid9x9 matrix grid9x9 matrix grid
Yundefinedundefinedundefined9x9 matrix grid9x9 matrix grid9x9 matrix grid
Zundefinedundefinedundefinedundefined9x9 matrix of sums9x9 matrix of sums
Key Moments - 3 Insights
Why do we use meshgrid before calculating Z?
meshgrid creates matrices X and Y that hold all grid points, so Z can be calculated element-wise for each (X,Y) pair, as shown in step 3 and 4 of the execution_table.
What is the difference between mesh and surf plots?
mesh draws a wireframe grid showing the surface shape (step 5), while surf draws a colored surface with shading (step 6), making it easier to see height differences.
Why does Z use element-wise power .^ instead of ^?
Because X and Y are matrices, .^ squares each element individually (step 4). Using ^ would try matrix multiplication, causing an error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what does Z represent?
AA single number representing total sum
BA vector of x values squared
CA matrix of sums of squares of X and Y elements
DA matrix of random values
💡 Hint
Check step 4 in execution_table where Z is calculated as X.^2 + Y.^2 element-wise
At which step does the wireframe mesh plot appear?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the 'Action' column in execution_table for mesh(X,Y,Z) call
If we change the step size in x and y from 0.5 to 1, how does variable_tracker for x change after step 1?
Ax becomes [-2 -1 0 1 2]
Bx becomes [-2 -1.5 -1 -0.5 0 0.5 1 1.5 2]
Cx becomes [-2 -1 0 1 2 3]
Dx remains undefined
💡 Hint
Check how x is created in step 1 with range and step size
Concept Snapshot
meshgrid(x,y) creates X,Y grids for coordinates
Z = f(X,Y) computes surface heights element-wise
mesh(X,Y,Z) draws wireframe surface
surf(X,Y,Z) draws colored shaded surface
Use .^ for element-wise power on matrices
Plots show 3D surface shapes clearly
Full Transcript
This example shows how to create 3D surface plots in MATLAB using mesh and surf. First, vectors x and y are created from -2 to 2 with step 0.5. Then meshgrid creates matrices X and Y holding all grid points. Next, Z is calculated as the sum of squares of X and Y elements. The mesh function draws a wireframe plot of the surface, while surf draws a colored surface plot. The code uses element-wise power .^ to square each element of X and Y. The execution table traces each step, showing variable values and plot actions. Key moments clarify why meshgrid is needed, the difference between mesh and surf, and the importance of element-wise operations. The visual quiz tests understanding of Z's meaning, when plots appear, and how changing step size affects variables. This helps beginners see how MATLAB builds and displays 3D surfaces step-by-step.

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

  1. Step 1: Understand the function outputs

    mesh draws a grid-like wireframe of the surface without filling colors.
  2. Step 2: Compare with surf

    surf draws a colored surface that fills the area between grid lines, showing height with color.
  3. Final Answer:

    mesh creates a wireframe surface, while surf creates a colored surface. -> Option A
  4. Quick Check:

    Wireframe vs colored surface = A [OK]
Hint: Remember: mesh = wireframe, surf = colored surface [OK]
Common Mistakes:
  • Thinking mesh colors the surface
  • Confusing 2D and 3D plotting
  • Assuming mesh only works with vectors
2. Which of the following is the correct syntax to create a mesh plot of matrices X, Y, and Z in MATLAB?
easy
A. mesh(X, Y);
B. mesh(X, Y, Z);
C. mesh(X, Z, Y);
D. meshplot(X, Y, Z);

Solution

  1. Step 1: Recall mesh function syntax

    The correct syntax for mesh requires three inputs: X, Y, and Z matrices representing coordinates.
  2. Step 2: Check options

    Only mesh(X, Y, Z); matches the correct order and function name. mesh(X, Y); misses Z, B uses invalid function meshplot, C swaps Y and Z incorrectly.
  3. Final Answer:

    mesh(X, Y, Z); -> Option B
  4. Quick Check:

    Correct function and argument order = D [OK]
Hint: Syntax: mesh(X, Y, Z) with all three matrices [OK]
Common Mistakes:
  • Using wrong function name like meshplot
  • Swapping Y and Z arguments
  • Omitting one of the required inputs
3. What will be the result of the following MATLAB code?
X = 1:3; Y = 1:3; [X, Y] = meshgrid(X, Y); Z = X.^2 + Y.^2; mesh(X, Y, Z);
medium
A. A colored surface plot with smooth shading.
B. A flat 2D grid with no height variation.
C. An error because meshgrid is used incorrectly.
D. A 3D wireframe surface showing a paraboloid shape.

Solution

  1. Step 1: Understand meshgrid and Z calculation

    meshgrid creates matrices X and Y for grid points. Z is calculated as sum of squares, forming a paraboloid shape.
  2. Step 2: Understand mesh plotting

    mesh plots a wireframe 3D surface of Z over X and Y, showing the paraboloid shape as a grid.
  3. Final Answer:

    A 3D wireframe surface showing a paraboloid shape. -> Option D
  4. Quick Check:

    meshgrid + paraboloid + mesh = wireframe paraboloid [OK]
Hint: meshgrid creates grids; mesh plots wireframe surface [OK]
Common Mistakes:
  • Thinking mesh creates colored surface
  • Assuming meshgrid returns vectors, not matrices
  • Expecting 2D plot instead of 3D
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

  1. 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.
  2. 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.
  3. Final Answer:

    Z must be a matrix matching sizes of X and Y, not vectors. -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Step 3: Plot with surf

    Use surf(X, Y, Z) to plot the colored surface correctly.
  4. 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
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard