3-D Plots and Figure Customization

Customization is done through properties: set(gca, 'FontSize', 12), xlim, ylim, zlim, axis equal, xticks, xticklabels.

Concept

meshgrid(x, y) Grids of x and y coordinates for evaluating f(x, y) surf, mesh Surfaces (filled / wireframe) contour, contourf plot3, scatter3 Lines and points in 3-D imagesc, image Matrices as images (heat maps) colormap, colorbar, caxis/clim view(az, el), rotate3d shading interp, lighting

Customization is done through properties: set(gca, 'FontSize', 12), xlim, ylim, zlim, axis equal, xticks, xticklabels.

Why It Matters

Response surfaces (efficiency maps of motors, engine calibration maps), temperature distributions and 3-D trajectories are routine engineering visuals.

Syntax

[X, Y] = meshgrid(-2:0.1:2);
Z = X .* exp(-X.^2 - Y.^2);
surf(X, Y, Z), colorbar, view(45, 30)

Example

19.1 Surfaces, contours and 3-D lines

[X, Y] = meshgrid(-2:0.5:2, -2:0.5:2);
Z = X .* exp(-X .^ 2 - Y .^ 2);
fprintf('meshgrid: X is %dx%d, Z peak %.4f at X = %g, Y = %g\n', size(X), max(Z(:)), X(Z == max(Z(:))), Y(Z == max(Z(:))))
fig = figure('Visible', 'off');
surf(X, Y, Z)
xlabel('x'), ylabel('y'), zlabel('z'), title('z = x e^{-x^2-y^2}')
colorbar
view(45, 30)
[az, el] = view;
fprintf('view azimuth %g, elevation %g\n', az, el)
fig2 = figure('Visible', 'off');
contour(X, Y, Z, 10)
t = linspace(0, 4 * pi, 200);
fig3 = figure('Visible', 'off');
plot3(cos(t), sin(t), t)
fprintf('created %d figures\n', numel(findobj(0, 'Type', 'figure')))
close all

Expected Result

meshgrid produced 9×9 coordinate matrices. The true maximum of x·exp(−x²−y²) is at x = 1/√2 ≈ 0.707, y = 0; on this coarse 0.5-spaced grid the largest sample is at the nearest grid point, which is why a finer grid is needed for accurate peaks. view returned the angles that were set.

meshgrid: X is 9x9, Z peak 0.3894 at X = 0.5, Y = 0
view azimuth 45, elevation 30
created 3 figures

Common Mistakes

Using * or ^ instead of .*/.^ when computing Z on a grid.

Confusing meshgrid (x varies along columns) with ndgrid (x varies along rows).

Very fine grids that make surfaces slow and memory-hungry.

Best Practices

Choose the grid resolution to match the features of the function.

Add a colorbar and axis labels; use contour when exact values matter more than the 3-D impression.

Use perceptually uniform colormaps (parula, viridis).

Interview Relevance

Low: "what does meshgrid do?", "surf vs mesh?", "how do you visualize a 2-D lookup table?".

Practice Questions

Plot the paraboloid z = x² + y² with surf and contour.

Visualize a 10×10 random matrix with imagesc and a colorbar.

Plot a helix with plot3 and label the axes.

Plot an engine efficiency map (speed × torque) from a small table with contourf.

Quick Quiz

Q1. (Multiple choice) Which function generates coordinate matrices for evaluating f(x, y)? A) linspace B) meshgrid C) repmat D) ndims Answer: B.

Q2. (Predict the output) size(X) for [X, Y] = meshgrid(-2:0.5:2, -2:0.5:2).

Answer: 9 9 (verified).

Q3. (True/False) surf requires Z to be computed with element-wise operators on the grid.

Answer: True (otherwise the matrix operators compute something else or error).

Q4. (Find the bug) Z = X * exp(-X^2 - Y^2); Answer: Use .* and .^: Z = X.* exp(-X.^2 - Y.^2).

Coding Challenge

Evaluate z = sin(x)·cos(y) on a 50×50 grid over [0, 2π]², and print the grid size and the maximum and minimum values of z.

[X, Y] = meshgrid(linspace(0, 2 * pi, 50));
Z = sin(X) .* cos(Y);
fprintf('grid %dx%d, max %.4f, min %.4f\n', size(Z), max(Z(:)), min(Z(:)))