how to create 3d surface plot in matlab

To create a 3D surface plot in Matlab, you can use the surf function. The surf function requires three matrices as input arguments, which represent the x, y, and z coordinates of the points to be plotted.

Here's an example code snippet that generates a 3D surface plot:

main.m
% Define the x and y coordinates of the points
x = -5:0.1:5;
y = -5:0.1:5;

% Define the function to be plotted
[X,Y] = meshgrid(x,y);
Z = sin(sqrt(X.^2 + Y.^2)) ./ sqrt(X.^2 + Y.^2);

% Generate the surface plot
surf(X,Y,Z)

% Add labels and title
xlabel('x')
ylabel('y')
zlabel('z')
title('3D Surface Plot of sin(sqrt(x^2+y^2))/sqrt(x^2+y^2)')
346 chars
17 lines

In this example, we define the x and y coordinates using the : operator to create an array of values ranging from -5 to 5 with a step size of 0.1. We then use the meshgrid function to create matrices X and Y that represent all possible combinations of x and y values.

Next, we define the function to be plotted (in this case, sin(sqrt(x^2+y^2))/sqrt(x^2+y^2)) using element-wise operations on the X and Y matrices. We compute the Z matrix to store the values of the function at each (x, y) point.

Finally, we use the surf function to create the 3D surface plot of the function. We also add labels and a title to the plot using the xlabel, ylabel, zlabel, and title functions.

gistlibby LogSnag