0
0
MATLABdata~10 mins

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

Choose your learning style9 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.