Beruflich Dokumente
Kultur Dokumente
Three-Dimensional
Plots
Three-dimensional (3-D) plots can be a useful way to present data that consists of
more than two variables. MATLAB provides various options for displaying three
dimensional data. They include line and wire, surface, mesh plots, and many oth
ers. The plots can also be formatted to have a specific appearance and special
effects. Many of the three-dimensional plotting features are described in this chap
ter. Additional information can be found in the Help Window under Plotting and
Data Visualization.
In many ways this chapter is a continuation of Chapter 5, where two-dimen
sional plots were introduced. The 3-D plots are presented in a separate chapter
because not allMATLAB users use them. In addition, new users ofMATLAB will
probably find it easier to practice 2-D plotting first and learn the material in Chap
ters 6-9 before attempting 3-D plotting. It is assumed throughout the rest of this
chapter that the reader is familiar with 2-D plotting.
323
www.it-ebooks.info
324 Chapter 10: Three-Dimensional Plots
• The three vectors with the coordinates of the data points must have the same
number of elements.
• The line specifiers, properties, and property values are the same as in 2-D plots
(see Section 5.1).
For example, if the coordinates x,y, and z are given as a function of the parameter
t by
x = /tsin(2t)
y = /tcos(2t)
z = 0.5t
a plot of the points for 0 :S; t :S; 61t can be produced by the following script file:
t=0:0.1:6*pi;
x=sqrt(t).*sin(2*t);
y=sqrt(t).*cos(2*t);
Z=O.S*t;
plot3(x,y,z,'k','linewidth',l)
grid on
The plot shown in Figure 10-1 is created when the script is executed.
10
,,
8 -
'
....._'
I
..._I
6 -
I
N 4 -
..._ ..._ ...... :
'- ...... ....._ , :
2 -
''
I
I
'
I
0 "'-....._....._ I
5
5
y 0
0 X
-5 -5
Mesh and surface plots are three-dimensional plots used for plotting functions of
the form z = f(x,y) where x and y are the independent variables and z is the
dependent variable. It means that within a given domain the value of z can be cal
culated for any combination of x andy. Mesh and surface plots are created in three
www.it-ebooks.info
10.2 Mesh and Surface Plots 325
steps. The flrst step is to create a grid in the x y plane that covers the domain of the
function. The second step is to calculate the value of z at each point of the grid.
The third step is to create the plot. The three steps are explained next.
Figure 10-2: A grid in thexyplane for the domain -1 �x� 3 and 1 �y� 4 with
spacing of 1.
the distance between the points is one unit. The points of the grid can be defmed
by two matrices, X and Y. Matrix X has the x coordinates of all the points, and
[=! �] [:::::�
matrix Yhas they coordinates of all the points:
X= �!�
-10123
and Y=
22222
-10123 11111
The X matrix is made of identical rows since in each row of the grid the points
have the samex coordinate. In the same way the Ymatrix is made of identical col
umns since in each column of the grid they coordinate of the points is the same.
MATLAB has a built-in function, called meshgrid, that can be used for
www.it-ebooks.info
326 Chapter 10: Three-Dimensional Plots
creating the X and Ymatrices. The form of the meshgrid function is:
In the vectors x and y the first and last elements are the respective boundaries of
the domain. The density of the grid is determined by the number of elements in
the vectors. For example, the mesh matrices X and Ythat correspond to the grid in
Figure 10-2 can be created with the meshgrid command by:
>> X=-1:3;
>> y=1: 4 ;
>> [X,Y]=meshgrid(x,y)
X =
-1 0 1 2 3
-1 0 1 2 3
-1 0 1 2 3
-1 0 1 2 3
y =
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
Once the grid matrices exist, they can be used for calculating the value of z at each
grid point.
www.it-ebooks.info
10.2 Mesh and Surface Plots 327
z
-0.5000 0 0.5000 0.4000 0.3000
-0.8000 0 0.8000 1.0000 0.9231
-0.9000 0 0.9000 1.3846 1.5000
-0.9412 0 0.9412 1.6000 1.9200
Once the three matrices have been created, they can be used to plot mesh or sur
face plots.
X=-1:0.1:3;
y=1:0.1:4;
[X,Y]=meshgrid(x,y);
Z=X.*Y.A2./(X.A2+Y.A2);
Note that in the program above the vectors x andy have a much smaller spacing
than the spacing earlier in the section. The smaller spacing creates a denser grid.
The figures created by the program are:
I� ' -r
2 I
I '
�
I
I 1
"�
-
I � I
I
'
N I N
I 0 -
- 1 -1
4 4
4 4
X
1 -2 1 -2
www.it-ebooks.info
328 Chapter 10: Three-Dimensional Plots
• The plots that are created have colors that vary according to the magnitude of z.
The variation in color adds to the three-dimensional visualization of the plots.
The color can be changed to be a constant either by using the Plot Editor in the
Figure Window (select the edit arrow, click on the figure to open the Property
Editor Window, then change the color in the Mesh Properties list), or by using
the colormap (C) command. In this command Cis a three-element vector in
which the first, second, and third elements specify the intensity of Red, Green,
and Blue (RGB) colors, respectively. Each element can be a number between 0
(minimum intensity) and 1 (maximum intensity). Some typical colors are:
There are several additional plotting commands that are similar to the mesh
and surf commands that create plots with different features. Table 10-1 shows
a summary of the mesh and surface plotting commands. All the examples in the
. -!5)x2+y2 .
table are p1ots of the functlon z = 1.8 · sm(x)cos(0.5y) over the domain
-3:5:x:5:3 and -3:5:y:5:3.
y=-3:0.25:3;
mesh(X,Y,Z)
xlabel(1X1); ylabel ( 1Y 1)
zlabel(1 z 1 )
www.it-ebooks.info
10.2 Mesh and Surface Plots 329
y=-3:0.25:3;
Function format: 0.4 -
'" : __ ..- �: -- '"
I
[X,Y]=meshgrid(x,y);
1_. -I
-
surf(X,Y,Z) -
0.2-� - - I I
4
I surf(X,Y,Z)
--
- - �_=:�-;�
� -.... --.. l I
�
�
--.:..___;��
xlabel( 1X1); ylabel ( 1Y1)
�
0 X
zlabel( 1 z1)
-4 ---4
zlabel ( 1 z1)
meshc(X,Y,Z)
Function format:
xlabel( 1X1); ylabel ( 1Y1)
meshc(X,Y,Z)
zlabel ( 1 z1)
surf c(X,Y,Z)
Function format:
xlabel( 1X1); ylabel ( 1Y 1)
surfc(X,Y,Z)
zlabel ( 1 z1)
www.it-ebooks.info
330 Chanter 10: Three-Dimensional Plots
X=-3:0.25:3;
y=-3:0.25:3;
[X,Y]=meshgrid(x,y) ;
Z=l.B.A(-1.5 * sqrt(X.A 2+
y. A2)).*cos(0.5*Y) *sin(X);
•
surfl(X,Y,Z)
zlabel( 1 z1)
Function format:
waterfal l(X,Y,Z)
water-
xlabel( 1X1); ylabel( 1Y 1)
fall (X, Y,Z)
y 0
-2
--' -..,..,.. __--
-� - X
zlabel( 1 z1)
2
-4 -4
y=-3:0.25:3;
Function format: [X,Y]=meshgrid(x,y) ;
contour3(X,
Z=l.B.A(-1.5 * sqrt(X.A 2+
Y,Z,n) y. A2)).*cos(0.5*Y) *sin(X);
•
contour3(X,Y,Z,l5)
n is the number
xlabel( 1X1); ylabel( 1Y1)
of contour levels
zlabel( 1 z1)
(optional)
Function format:
contour(X,Y,Z,15)
contour
xlabel( 1X1); ylabel( 1Y1)
(X, Y,Z,n)
zlabel( 1 z1)
n is the number
of contour levels -� -2 -1
X
(optional)
www.it-ebooks.info
10.3 Plots with Soecial Graohics 331
MATLAB has additional functions for creating various types of special three
dimensional plots. A complete list can be found in the Help Window under Plot
ting and Data Visualization. Several of these 3-D plots are presented in Table 10-
2. The examples in the table do not show all the options available with each
1 -
-1 -1
t=1inspace(O,pi,2)
0 ;
r=1s
+ in(t);
1 -
[X,Y,Z]=cy1inder(r);
,,
0.8 - ' I
' c surf(X,Y,Z)
0.6-- '
I
I
"I
axis square
0.4·-- ' I
• .I
0.2- -
www.it-ebooks.info
332 Chapter 10: Three-Dimensional Plots
Jill
markers and ver- 30 �
z=t."'l.S;
tical lines from N 20 �
stem3(x,y,z,1fill1)
the x y plane) 10 �
grid on
0
1
Function format: 10 xlabel ( 1X1);
Function format:
scatter3(X,
Y,Z)
y 0
5 X xlabel ( 1X1);
-1 0
ylabel ( 1Y1)
zlabel ( 1 z1)
explode=[O 0 1 0];
plot type. More details on each type of plot can be obtained in the Help
Window, or by typing help command name in the Command Window.
Polar coordinates grid in the x y plane:
_A 3-D plot of a function in which the value of z is given in polar coordinates (for
example z = re) can be done by following these steps:
• Create a grid of values of e and r with the meshgrid function_
www.it-ebooks.info
10.4 The view Command 333
• Convert the polar coordinates grid to a grid in Cartesian coordinates. This can
be done with MATLAB's built-in function pol2cart (see example below).
• Make a 3-D plot using the values of z and the Cartesian coordinates.
For example, the following script creates a plot of the function z = re over the
domain 0 � e � 360° and 0 � r � 2.
[th,r]=meshgrid((0:5:360)*pi/180,0:.1:2);
Z=r.*th;
[X,Y] = pol2cart(th,r);
I
- �
� -- - I -... ..._
- - "" -- I
-�--
I
'I
N N
I
' 'I
I
ox
-2 -2
The view command controls the direction from which the plot is viewed. This is
done by specifying a direction in terms of azimuth and elevation angles, as seen in
Figure 10-3, or by defming a point in space from which the plot is viewed. To set
the viewing angle of the plot, the view command has the form:
• el is the angle of elevation (in degrees) from the x y plane. A positive value
corresponds to opening an angle in the direction of the z axis.
www.it-ebooks.info
334 Chapter 10: Three-Dimensional Plots
z
y
As an example, the surface plot from Table 10-1 is plotted again in Figure 10-4,
with viewing angles az =zoo and el = 35°.
x=-3:0.25:3;
y=-3:0.25:3;
[X,Y]•meshgrid(x,y);
Z•1.8.A(-1.5*sqrt(X.A2+
Y. A2)).*cos (0.5*Y).*sin(X) 1
view(20,35)
• With the choice of appropriate azimuth and elevation angles, the view com
mand can be used to plot projections of 3-D plots on various planes according
to the following table:
xy (top view) 0 90
x z (side view) 0 0
y z (side view) 90 0
An example of a top view is shown next. Figure 10-5 shows the top view of the
function that is plotted in Figure 10-1. Examples of projections onto the x z andy z
planes are shown next, in Figures 10-6 and 10-7, respectively. The figures show
mesh plot projections of the function plotted in Table 10-1.
www.it-ebooks.info
10.4 The view Command 335
5 ----------------- t=0:0.1:6*pi;
4
x=sqrt(t).*sin(2*t);
3
2 y=sqrt(t).*cos(2*t);
z=0.5*t;
,.., 0
plot3(x,y,z, 1k1,11inewidth1,1)
-1
-2 view(0,90)
-3
grid on
-4
- 5 L_��--���-�-�� xlabel ( 1X1); ylabel (1Y1)
-5 -4 -3 -2 -1 0 2 3 4 5
X zlabel (1 z1)
u ---·----------- - - - - - - - - ·
x=-3:0.25:3;
y=-3:0.25:3;
[X,Y]=meshgrid(x,y);
Z=1.8.A(-1.5*sqrt(X.A2+
y. A2)).*cos(0.5*Y).*sin(X);
mesh(X,Y,Z)
view(O,O)
x=-3:0.25:3;
0-" -- - �--- I - -- T --- - -- -�----
1 1
I I I I I
y=-3:0.25:3;
0.3 --- --- -
../ -r -- -�-- - -,
1
I
-,
I .--·· --......� I
0.2 ·· ---1--- --1 [X,Y]=meshgrid(x,y);
I I
Z=1.8.A(-1.5*sqrt(X.A2+
y. A2)).*cos(0.5*Y).*sin(X);
/ surf(X,Y,Z)
/
/,1
I I
-- -�-- - -,
I I I
view(90,0)
- 1- ---1- - - -I
www.it-ebooks.info
336 Chapter 10: Three-Dimensional Plots
view ( 2) sets the default to the top view, which is a projection onto the
x-y plane with az = oo, and el= 90°.
view ( 3) sets the default to the standard 3-D view with az = -37.5° and
el= 30° .
• The viewing direction can also be set by selecting a point in space from which
the plot is viewed. In this case the view command has the form
view ( [x, y, z]), where x, y, and z are the coordinates of the point. The direc
tion is determined by the direction from the specified point to the origin of the
coordinate system and is independent of the distance. This means that the view
is the same with point [6, 6, 6] as with point [ 1 0, 10, 10]. Top view can be set
up with [0, 0, 1]. A side view of the x z plane from the negativey direction can
be set with [0, -1, 0], and so on.
Solution
As shown in the figure, the coordinate system is set up such that the x andy axes
point in the east and north directions, respectively. Then the motion of the projec
tile can be analyzed by considering the vertical direction z and the two horizontal
components x andy. Since the projectile is fired directly north, the initial velocity
v0 can be resolved into a horizontaly component and a vertical z component:
v0Y = v0cos(8) and v0z = v0sin(8)
In addition, due to the wind the projectile has a constant velocity in the negative x
direction, v = -30 m/s.
X
The initial position of the projectile (x0,y0, z0) is at point (3000, 0, 0). In the verti
cal direction the velocity and position of the projectile are given by:
www.it-ebooks.info
10.5 Examples ofMATLAB Applications 337
•
Voz
The time it takes the projectile to reach the highest point (vz = 0) 1s thmax = -
.
g
The total flying time is twice this time, t101 = 2thmax. In the horizontal direction
the velocity is constant (both in the x andy directions), and the position of the pro
jectile is given by:
x = x0+vxt andy= y0+v0yf
The following MATLAB program written in a script file solves the problem by
following the equations above.
X0=3000; VX=-30;
vOz=vO*sin(theta*pi/180);
vOy=vO*cos(theta*pi/180);
t=2*v0z/g;
:
�� �
-
2500
--
-
--
--
--
- �
2000 --
I --t I
I � �J
--r --
1--
� 1500 I
--
"- I
L- �� 1
E 1
--.--.1 I
� 1000 1
--. �-- :
--.
--.
--.
--.----. �I
500 r--
0
6000
4000 6000
4000
y(m) 2000
2000 x (m)
0 0
www.it-ebooks.info
338 Chapter 10: Three-Dimensional Plots
v = __
l q
41tE0 r
where Eo = 8.8541878 x 10-12 ___£_ is the permittivity constant, q is the magni-
Nm2
tude of the charge in coulombs, and r is the distance from the particle in meters.
The electric field of two or more particles is calculated by using superposition.
For example, the electric potential at a point due to two particles is given by
v= __ l (q1 + qil
41tEo r1 r/
where q1, q2, r1, and r2 are the charges of the particles and the distance from the
point to the corresponding particle, respectively.
Two particles with a charge of
q1 = 2 x 10-1° C and q =10-10 C are
3x
2
positioned in the x y plane at points (0.25, 0,
0) and (-0.25, 0, 0), respectively, as shown.
Calculate and plot the electric potential due
to the two particles at points in the x y plane
that are located in the domain -0.2 :5; x :5; 0.2
and -0.2 :5; y :5; 0.2 (the units in the x y plane
are meters). Make the plot such that the x y
plane is the plane of the points, and the z axis is the magnitude of the electric
potential.
Solution
k=1/(4*pi*eps0);
X=-0.2:0.01:0.2;
y=-0.2:0.01:0.2;
www.it-ebooks.info
10.5 Examnles ofMATLAB Annlications 339
V=k* (ql. /rl+q2. /r2); I Calculating the electric potential Vat each grid pointl
mesh(X,Y,V)
�
>
0.2
T=O a
Solution
The temperature distribution, T(x, y) in the plate can be determined by solving the
two-dimensional heat equation. For the given boundary conditions T(x, y) can be
expressed analytically by a Fourier series (Erwin Kreyszig, Advanced Engineer
ing Mathematics, John Wiley and Sons, 1993):
1t n = 1 [
sinh (2n- 1)
1t
:J
A program in a script file that solves the problem is listed below. The program fol
lows these steps:
www.it-ebooks.info
340 Chapter 10: Three-Dimensional Plots
clear T
x=linspace(O,a,na) ;
y=linspace(O,b,nb) ;
[X, Y] =meshgrid(x,y) ; [ ]
Creating a grid in the x y plane.
for i=l: nb First loop, i, is the index of the grid's row. ]
for j=1 :na [ Second loop, j, is the index of the grid's column.]
T(i,j) =O;
end
end
end
mesh(X,Y,T)
xlabel( 'x (m) ') ; ylabel( 'y (m) ') ; zlabel( 'T ( AoC) ')
The program was executed twice, first using five terms (k = 5) in the Fourier series
to calculate the temperature at each point, and then with k = 50. The mesh plots
created in each execution are shown in the figures below. The temperature should
be uniformly 80 oc at y = 4 m. Note the effect of the number of terms (k) on the
accuracy at y = 4 m.
www.it-ebooks.info
10.6 Problems 341
k=50
100 100
80 80
0 60 0 50
, 0
;::- 40 ;::- 40
20 20
0 0
4 4
5
10.6 PROBLEMS
)
x
)
= [(t �O�S + 1 Jsin(3t) y = [(t�O�S + 1 Jcos(0.8t) z = 0.4t<3/2l
..•
.
x = rcos(t) y = rsin(t) z =-
ht
1tn .
where r= ab
./[bcos(t)F + [asin(t)F
, a and ·�·.
y(m) 0
20
-10 -20
b are the semimajor and semiminor axes of
the ellipse, h is the staircase n is the number of revolutions that the
height, and
staircase makes. Make a 3-D plot of the staircase with a = 20m, b = 10m,
h= 18m, and n = 3 . (Create a vector t for the domain 0 to 21tn, and use the
plot3 command.)
www.it-ebooks.info
342 Chapter 10: Three-Dimensional Plots
-3�x�3 and-3�y�3 .
5. Make a 3-D surface plot of the function z = 0.5x 2 + 0.5y 2 in the domain
-2�x�2 and -2�y�2.
-cos(2R)
6. Make a 3-D mesh plot of the function , where R = Jx2 + y 2
e0.2R
z =
9. The van der Waals equation gives a relationship between the pressure p (atm),
volume V, (L), and temperature T (K) for a real gas:
p nRT _n2a
=
V-b V
where n is the number of moles, R = 0.08206 (L atm)/(mol K) is the gas con
stant,and a (L2 atm/mol2), and b (L/mol) are material constants.
Consider 1.5 moles of nitrogen (a = 1.39L2 atm/mol2, b = 0.03913 Ll
mol). Make a 3-D plot that shows the variation of pressure (dependent vari
able, z axis) with volume (independent variable, x axis) and temperature (inde
pendent variable, y axis). The domains for the volume and temperature are
0.3� v� 1.2 Land 273� T�473 K.
10. Molecules of a gas in a container are moving around at different speeds. Max
well's speed distribution law gives the probability distribution P(v) as a func
tion of temperature and speed:
P(v)
( M � 312
2 (-Mv )/(2RT)
2
41t ve
__
=
21tRY:
www.it-ebooks.info
10.6 Problems 343
where M is the molar mass of the gas in kg/mol, R = 8.31 J/(molK ) , is the gas
constant, Tis the temperature in kelvins, and vis the molecule's speed in m/s.
Make a 3-D plot of P( v) as a function of v and T for 0:5: v:5: 1000 m/s and
70:5: T:5: 320 K for oxygen (molar mass 0.032 kg/mol).
cl
E= _ :..._
----:' .._
c2 (m��
l
A e"'r-1 ]
4 2
where C1 = 3.742 X 108 WJlm /m , C2 = 1.439 X 104 Jl.IDK, Tis the tempera
ture in degreesK, and A is the wave length in Jl.ID. Make a 3-D plot (shown in
the figure) of E as a function of A (0.1:5: A:5: 10 Jl.ID) and T for
100:5: T:5: 2000K. Use a logarithmic scale for A. This can be done with the
command: set ( gca I 1 xscale 1 1 1 log 1 ) •
12. The flow Q (m3/s) in a rectangular channel is given by the Manning's equa
tion:
Q=---
kdw ( � wd 21 3
JS
n w+2
where d is the depth of water (m), w is the width of the channel (m), S is the
slope of the channel (m/m), n is the roughness coefficient of the channel
walls, and k is a conversion constant (equal to 1 when the units above are
used). Make a 3-D plot of Q (z axis) as a function of w (x axis) for 0:5: w:5: 8 m,
and a function of d (y-axis) for 0:5: d:5: 4 m. Assume n = 0.05 and
S = 0.001 m/m.
where R and C are the resistance of the resistor and capacitance of the
www.it-ebooks.info
344 Chapter 10: Three-Dimensional Plots
b) Make a plot that is a projection on the x z plane. Estimate from this plot
the natural frequency of the circuit (the frequency at which I is maxi-
14. A defect in a crystal lattice where a row of atoms is missing is called an edge
dislocation. The stress field around an edge dislocation is given by:
2 2
0 -Gb y(3x +y)
=
0
r:x:
=
21t(l-v)
Gb
2
21t(l-v) 2+/ 2
2
y(x -y )
2 / 2
(x + )
Edge
dislocation �
yl
X
YY
(x ) :.,L �
2 2
___ ___
't
xy
=
Gb x(x y )
21t(l-v) 2+/ 2
- Plot domain
(x )
where G is the shear modulus, b is the
Burgers vector, and vis Poisson's ratio. Plot the stress components (each in a
separate figure) due to an edge dislocation in aluminum, for which
16. The equation for the streamlines for uniform flow over a cylinder is
'JI(x,y) = y - _L_+
x 2 y2
where 'I' is the stream function. For example, if 'I' = 0, then y = 0. Since the
www.it-ebooks.info
10.6 Problems 345
equation is satisfied for all x, the x axis is the zero ( 'I' = 0) streamline.
Observe that the collection of points where x2 + y2 = 1 is also a streamline.
Thus, the stream function above is for a cylinder of radius 1. Make a 2-D con
tour plot of the streamlines around a cylinder with 1 in. radius. Set up the
domain for x andy to range between -3 and 3. Use 100 for the number of con
tour levels. Add to the figure a plot of a circle with a radius of 1. Note that
MATLAB also plots streamlines inside the cylinder. This is a mathematical
artifact.
18. The Verhulst model, given in the following equation, describes the growth of
a population that is limited by various factors such as overcrowding and lack
of resources:
N
N(t) = �
1+ (N: )
N
-1 e-rt
sus t and N� assuming r 0.1 s-1, and N0 10. Lett vary between 0 and 100
and N� between 100 and 1,000.
· · . . ..
.
T 0.5 , -2 � x � 2, and -0 .5 � z � 0.
=
www.it-ebooks.info
346 Chapter 10: Three-Dimensional Plots
SID COS
3e
For K1 = 300 ksWin plot the stresses (each in a separate figure) in the domain
0 � e � 90° and 0.02 � r � 0. 2 in. Plot the coordinates x andy in the horizontal
plane, and the stresses in the vertical direction.
www.it-ebooks.info