In this example I will be showing you how you can do 3-D plotting in MATLAB, lets start:
Three-dimensional plots typically display a surface defined by a function in two variables, z = f(x,y) .
To evaluate z, first create a set of (x,y) points over the domain of the function using meshgrid.
Write the following code :
[X,Y] = meshgrid(-2:.2:2);
Z = X .* exp(-X.^2 - Y.^2);
Then create a surface plot, using this code:
surf(X,Y,Z)
The code should look this:
Both the surf function and its companion mesh display surfaces in three dimensions. surf displays both the connecting lines and the faces of the surface in color. mesh produces wireframe surfaces that color only the lines connecting the defining points.
Press the "RUN" button highlighted in the top panel of the software to display the 3-D Plotted figure, and the final result should look like this:
You can display multiple plots in different subregions of the same window using the subplot function.
The first two inputs to subplot indicate the number of plots in each row and column. The third input specifies which plot is active. For example, create four plots in a 2-by-2 grid within a figure window.
Use the following code:
t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(4*cos(t));
subplot(2,2,1); mesh(X); title('X');
subplot(2,2,2); mesh(Y); title('Y');
subplot(2,2,3); mesh(Z); title('Z');
subplot(2,2,4); mesh(X,Y,Z); title('X,Y,Z');
To see the following result:
P.S: Screenshots are taken by the author.
Comments
Post a Comment