undefined undefined undefined
logo for matrixlab-examples.com
leftimage for matrixlab-examples.com undefined undefined undefined undefined

3D plot – Part 2

Modeling Surfaces, Meshes and 3D variations

The 3D plot functions intended for plotting meshes and surfaces ' mesh' and ' surf', and their several variants ' meshc', ' meshz', ' surfc', and ' surfl', take multiple optional input arguments, the most simple form being ' mesh(z)' or ' surf(z)', where z represents a matrix.
 
Usually, tridimensional curves are represented by the values of z-coordinates samples on a grid of (x,y) values.

Thus, to create a surface or 3D plot we first need to generate a grid of (x,y) coordinates and find the height (z-coordinate) of the surface at each of the grid points. Matlab provides the function ' meshgrid' to create a grid of points over a specified range.


Meshgrid
Suppose that you want to plot the function z =  x 2 – 10y + 2 over the domain 0 ≤ x ≤ 4 and 0 ≤ y ≤ 4. To do so, we first take several points in the domain, say 25 points, as shown in this Fig.:

necessary grid to produce 3D plots

We can create two matrices x and y, each of size 5 x 5, and write the xy-coordinates of each point in these matrices. We can then evaluate z with the command z = x.^2 – 10*y + 2.

However, creating the two matrices x and y is much easier with the meshgrid command.



% creates vectors x and y, from 0 to 4
vx = 0 : 4
vy = vx
% creates meshgrid to be used in 3D plot
[x,y] = meshgrid(vx,vy)



The commands shown above generate the 25 points shown in the figure. All we need to do is generate two vectors, vx and vy, to define the region of interest and distribution or density of our grid points. Also, the two vectors need not be either same sized or linearly spaced. It is very important to understand the use of meshgrid.

Matlab response is as follows:



vx =
     0     1     2     3     4

vy =
     0     1     2     3     4

x =
     0     1     2     3     4
     0     1     2     3     4
     0     1     2     3     4
     0     1     2     3     4
     0     1     2     3     4

y =
     0     0     0     0     0
     1     1     1     1     1
     2     2     2     2     2
     3     3     3     3     3
     4     4     4     4     4




See the columns of x and the rows of y. When a surface is plotted with the ' mesh(z)' command (where z is a matrix), the tickmarks on the x and y axes do not indicate the domain of z but the row and column indices of the z-matrix. Typing ' mesh(x,y,z)' or ' surf(x,y,z)' (where x and y are vectors used by 'meshgrid' to create a grid), result in the surface plot of z, with x and y values shown along the respective axes.


The folowing script could be an example of how tho use the ' meshgrid', ' plot3', ' meshc', and ' surfc' commands.

We'll 3D plot the following surface:

equation to be 3D-plotted

with this script:



% clears command window, clears variables and closes figures
clc; clear; close all

% defines vectors x and y
vx = -4 : 0.2: 4;
vy = -3 : 0.2: 3;

% calculates the necessary grid
[x,y] = meshgrid(vx, vy);

% calculates z and avoids a null denominator adding 'eps'
% (eps is the least possible number in Matlab)
z = x .* y .* (x.^2 - y.^2) ./ (x.^2 + y.^2 + eps);

% generates the first figure using 'plot3'
figure
plot3(x,y,z)
grid on
undefined
undefined% generates the second figure using 'meshc' to include theundefined
undefined% contour in the figure, and rotates the figure with 'view'undefined
figureundefined
meshc(x,y,z)undefined
view(-37, 15)undefined
undefined
undefined% generates the third 3D figure using 'surfc' to include theundefined
undefined% contour in the image, and also rotates the figure with 'view'undefined
figureundefined
surfc(x,y,z)undefined
view(-47, 25)undefined
undefined
undefined
undefined
And the generated results are:undefined
undefined
undefined
undefined
undefined3D plot using linesundefined
undefined
undefined
undefined3D graphics using the mesh instructionundefined
undefined
undefined
undefined
undefined3D graphs using surfaces functionundefined
undefined
undefined
Do you like it?undefined
undefined
Use the instruction 'undefinedrotate3d on' to manipulate the view angle of your plot. Include it in your script or type it in the command window to change the view with your mouse over the figure...undefined
undefined
 undefinedFrom '3D Plot part 2' to homeundefined
undefined
 undefinedFrom '3D Plot part 2' to 3D Mainundefined
 undefined
undefined undefined undefined undefined undefined undefined undefined undefined
undefined undefined undefined undefined undefined undefined undefinedTopundefined
undefined
undefinedPart 1 - undefinedview, subplotundefinedundefinedundefined
undefined
undefinedPart 3 - undefinedplot3, sphereundefinedundefinedundefined
 undefined
 undefined
undefinedPart 4 - undefinedsurfaces, meshundefinedundefinedundefined
undefined
undefined3D animationundefined
undefined
undefined
undefined
undefined
undefined
undefined undefined
undefined undefined undefined

undefinedfooter for 3D Plots and Graphics pageundefinedundefined
undefined
undefined

undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined undefined undefined undefined undefined undefined
undefined
undefined undefined undefinedundefined undefined undefined undefined undefined undefined