A blog to help electrical engineers and students to learn electrical engineering topics. Basic Electrical Engineering, Power Systems, Electrical Machines, tec.. Now special course on MATLAB for Beginners.

Class

Class

Wednesday, April 6, 2016

3D plots in MATLAB



MATLAB provides a lot of graphics to make quick 3-D plots. In this post we will discuss some of these functions.
The common 3D plot functions are:
·         Plot3
·         Waterfall
·         Contour3
·         Mesh
·         Surf

Some Useful Functions

Before discussing the 3D plotting functions, we will discuss some other useful MATLAB functions required to design 3D objects.
Meshgrid:
To define a third axis data, first we have to make a data set of x and y axis data. For each pair of x and y we define value of z. MATLAB provides a useful function called ‘meshgrid’ that can be used generate x-y pair for 3-D plots.
[X,Y] = meshgrid(a,b) will generate a two matrices X and Y, which contain x and y values for the x-y pair , where a and b are vectors that define the range of x and y.
>> a = [-1 0 1];
>> b = [9 10 11 12];
>> [X,Y] = meshgrid(a,b)
X =
    -1     0     1
    -1     0     1
    -1     0     1
    -1     0     1
Y =
     9      9      9
    10    10    10
    11    11    11
    12    12    12
Ndgrid:
ndgrid and meshgrid are similar, the difference is that ndgrid will make a N dimensional grid. The dimension of the output is determined by the number of output arguments.
The syntax is [X1,X2,X3,...] = ndgrid(x1v,x2v,x3v,...)
Contour:
contour(z) draws a contour plot of matrix z in the x-y plane, with the x-coordinates of the vertices corresponding to column indices of z and the y-coordinates corresponding to row indices of z. The contour levels are chosen automatically.
Cylinder:
cylinder(R,N) plots a unit height cylinder of radius R. The cylinder has N points around the circumference. Default value of N is 20.
Sphere:
sphere(N) generates three (N+1)-by-(N+1) matrices which define the points to make a unit sphere and graph the sphere as a SURFACE. Default value of N is 20.
Peaks:
peaks is a function of two variables, obtained by translating and scaling Gaussian distributions, which is useful for demonstrating MESH, SURF, PCOLOR, CONTOUR, etc.

 Plot3

The plot3 command is used to plot the lines and points in a three dimensional space. It is used in the same way as plot function is used. Plot3 will have a third variable to provide the third dimension data. To label the third axis, ‘zlabel’ command is used.
plot3(x,y,z) creates a three dimensional plot where x, y & z are equal sized arrays containing the locations of data points  to be plotted.
>> t = 0:pi/50:10*pi;
>> plot3(sin(t),cos(t),t)

 

Mesh Plot

mesh(x,y,z)command produces wireframe surfaces in three dimensions that color only the lines connecting the defining points.
>> [x,y] = meshgrid(-1:.1:1);
>> z = x.^2+y.^2;
>> mesh(z)

meshz(x,y,z) draws a curtain around the wireframe mesh.

Waterfall

The waterfall function draws a mesh similar to the ‘meshz’ function, but it does not generate lines from the columns of the matrices. This produces a "waterfall" effect.
The waterfall plot for the above example will be:
>> waterfall(z)

Ribbon Plot

The ribbon(z) command plots the curve of z along y axis with ribbons of uniform width and midpoint as elements of x.
>> ribbon(z)


Surface Plots

surf(x,y,z) creates three dimensional plot that colors both the connecting lines and the faces of the surface. The color will be selected from default color map unless it is defined by an argument.
>> [X,Y,Z] = sphere(100);
>> surf(X,Y,Z)

surfc(x,y,z) creates a surface plot and a contour plot under the surface plot.



Ezsurf command can be used to plot a surface plot of a function. The function z=x2+y2 can be plot using the command:
>> ezsurf('x.^2+y.^2', [-1 1])

Contour3

contour(x,y,z)command constructs a three dimensional contour plot. The counters are normally based on the current color map. z defines the heights with respect to the x-y plane. The level of contour lines is chosen automatically.
>> contour3(z)



The color level lines can be controlled by adding one more argument in the command. contour(z,n) command will plot the curve with ‘n’ color levels. The levels at which the lines has to be drawn can be defined by a vector ‘v’ as, contour(z,v). clabel(‘text’) command is used to label the contour lines.

3D Stem Plots

stem3 creates vertical lines terminated with a symbol in x-y plane.
stem3(Z) plots the discrete surface Z as stems from the xy-plane terminated with circles.
stem3(X,Y,Z) plots the surface Z at the values specified in X and Y.
>> theta = 0:.2:2*pi;
>> x=sin(theta);
>> y=cos(theta);
>> z=sin(theta);
>> stem3(x,y,z)
>> hold on
>> plot(x,y)

 


Color Shading of Plots

shading is a MATLAB command that sets the EdgeColor and FaceColor properties of all SURFACE objects in the current axes. It sets them to the correct values that depend upon whether the SURFACE objects are representing meshes or surfaces.  
shading flat sets the shading of the current graph to flat. Color of the line will be same as the Color at the end point of the segment.
shading interp sets the shading to interpolated. Color in each segment or patch varies linearly and interpolates the line color
shading FACETED sets the shading to faceted (default). Flat shading with superimposed black mesh lines.



Changing View Angle

view command is used to change the view angle of the 3D objects.     
view(AZ,EL) or view([AZ,EL]) is the syntax to set the view angle. AZ is the horizontal rotation angle in degree. A positive value indicate counter-clock wise rotation. EL is the vertical elevation in degrees. Positive values of elevation indicate moving above object and negative values move below.
view([X Y Z]) sets the view angle in Cartesian coordinates.
AZ = -37.5, EL = 30 is the default 3-D view. view(3) is used to set the default 3-D view.
AZ = 0, EL = 90 is the values for ‘Top View’. view(2) is used to get the top view.
Example:
>> t = 0:pi/50:10*pi;
>> subplot(1,3,1)
>> plot3(sin(t),cos(t),t)
>> title('default view')
>> subplot(1,3,2)
>> plot3(sin(t),cos(t),t)
>> view(2)
>> title('top view')
>> subplot(1,3,3)
>> plot3(sin(t),cos(t),t)
>> view(90,0)
>> title('front view')
submit to reddit

0 comments:

Post a Comment