0
0
MATLABdata~5 mins

mesh and surf for surfaces in MATLAB

Choose your learning style9 modes available
Introduction

We use mesh and surf to draw 3D surfaces. They help us see how values change over two directions, like hills or waves.

To visualize the shape of a mountain or terrain in 3D.
To show how temperature changes over an area and height.
To display the surface of a mathematical function with two inputs.
To compare different 3D surfaces side by side.
To explore patterns in data that depend on two variables.
Syntax
MATLAB
mesh(X, Y, Z)
surf(X, Y, Z)

X and Y are grids of coordinates, usually made by meshgrid.

Z is the height or value at each (X, Y) point.

Examples
This draws a wireframe bowl shape using mesh.
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);
This shows a smooth wave surface using surf.
MATLAB
x = -3:0.1:3;
y = -3:0.1:3;
[X, Y] = meshgrid(x, y);
Z = sin(sqrt(X.^2 + Y.^2));
surf(X, Y, Z);
Sample Program

This program creates two plots for the surface defined by Z = X^2 - Y^2. The first is a wireframe mesh, and the second is a colored surface.

MATLAB
x = -2:0.2:2;
y = -2:0.2:2;
[X, Y] = meshgrid(x, y);
Z = X.^2 - Y.^2;

figure;
mesh(X, Y, Z);
title('Mesh plot of Z = X^2 - Y^2');

figure;
surf(X, Y, Z);
title('Surf plot of Z = X^2 - Y^2');
OutputSuccess
Important Notes

mesh shows a grid wireframe without colors.

surf shows a colored surface that helps see height differences better.

You can rotate the 3D plot with the mouse to see it from different angles.

Summary

mesh draws a wireframe 3D surface.

surf draws a colored 3D surface.

Both need X, Y, and Z grids to show height values.