Introduction
A 3D surface plot helps you visualize how data changes across two variables. In this tutorial, you will learn how to plot the sinc function in MATLAB. You will also learn how to make it look smooth and colourful using built-in functions.
Step 1: Open MATLAB
Start MATLAB and create a new script file. You can also use the command window.
Step 2: Create a Grid
Use the meshgrid command to define the X and Y coordinates.
[x, y] = meshgrid(-8:0.5:8, -8:0.5:8);
This creates a grid that covers both X and Y axes.
Step 3: Define the sinc Function
The sinc function is given as sin(r)/r. Use the code below:
r = sqrt(x.^2 + y.^2) + eps; % eps avoids division by zero
z = sin(r)./r;
Here, r is the distance from the origin.
Step 4: Plot the Surface
Use the surf command to create the 3D plot.
surf(x, y, z);
Step 5: Improve the Look
Make the surface smooth and colorful.
shading interp; % Smooth shadingcolormap(jet); % Color gradientcolorbar; % Display color scale
Step 6: Label and Title
Add labels and a title for clarity.
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');title('3D Surface Plot of sinc(x, y)');grid on;
Step 7: Run the Script
Run your script and see the 3D surface plot. You will see a colorful peak in the center with smooth waves around it.
Conclusion
You have created a 3D surface plot in MATLAB using the sinc function. This technique helps in understanding how functions behave in three dimensions. You can try different functions or grid sizes to explore more visualization options.
Full Script
clc;clear;close all;% Create grid[x,y] = meshgrid(-8:0.5:8, -8:0.5:8);% Define sinc functionr = sqrt(x.^2+y.^2)+eps; % eps avoids division by zeroz = sin(r)./r;% Plot 3D surfacesurf(x, y, z);% Plot from top viewcontourf(x, y, z, 20);% Stylingshading interp; % Smooth colour shadingcolormap (jet); % Blue to red Gradientcolorbar; % Show color scaletitle('3D Surface Plot of Sinc(X,Y)');xlabel ('X-axis');ylabel('Y-axis');zlabel('Z-axis');grid on;
Result


Check this course related to M-Scripting
- Introduction to MATLAB Scripting for Automobile Engineering
- MATLAB Scripting with Model-Based Development Basics
- MATLAB Simulink and Scripting Foundations for Automotive MBD
