M3 Graph3D

advertisement
Graphing in 3D:
Let's find out about 3D graphics by examining the electric potential of a dipole. The
dipole potential is just the sum of the potentials from two point charges separated by
some distance. We’ll consider a pair of point charges +/-q located at x = 0, y = +/- d.
kq
kq
V ( x, y ) 

2
2
2
x  (y  d)
x  ( y  d )2
1
Recall that k 
. To see what is happening we will just let k=1, q=1, and d=1, the
40
answer won't depend in an important way on these choices.
First, we have to make the 2D function V(x,y). 2D functions are more complicated than
1D functions. We need a 2D array corresponding to (x,y) points covering the whole x-y
plane. The meshgrid function creates the right x and y arrays.
% make the x-y grid to cover the plane -2<x<2 and -2<y<2
dx = 0.075; dy = 0.075;
[x,y] = meshgrid(-2:dx:2,-2:dy:2);
% make the function, V(x,y)
k=1; q=1; d=1;
V=(k*q./(x.^2+(y-d).^2).^(1/2)) - ...
(k*q./(x.^2+(y+d).^2).^(1/2));
% plot the surface
surf(x,y,V);
Modifying surfaces - rotation and display options
--Click on the rotate symbol on the toolbar. Then put the cursor back over the graph and
rotate it so you can see underneath, from the side, etc.
--Edit the colormap and pick your favorite color scheme. To do this you’ll need to click
edit, choose colormap and then in the colormap window, click on tools and look at the
standard colormaps that are available. Many of them are quite nice.
--Try using shading to show the function instead of the mesh. (one line at a time) Zoom
in if you can’t see what’s really going on.
shading interp
shading flat
shading faceted
--Try out the various buttons on the toolbar and examine the menu choices.
--Try plotting the same information using different methods. (one line at a time).
mesh(x,y,V)
pcolor(x,y,V); shading interp
ribbon(V)
contour3(x,y,V,20)
106733537 2/13/16
Page 1 of 3
surfl(x,y,V); colormap gray; shading interp; material metal
Contour plot
Lets also try making contour plots. This is accomplished with Matlab’s contour
method.
% plot the contours at specified levels.
levels = [8 4 2 1 0.5 0.25 0 -0.25 -0.5 -1 -2 -4 -8];
[C,h]= contour(x,y,V,levels);
% add labels to the contours.
clabel(C,h)
The contour command returns C, the contour data, and h, a handle (identifying label) for
the contour plot.
There are some interesting options to investigate.
--Try contour(x,y,V) to see the default contours.
--Try contourf(x,y,V,levels) to see the filled contours.
--Take a look at the 3D representation of the surface with surf(x,y,V).
-- And finally, change the color maps using colormap pink. Possible color maps
are: jet, autumn, pink, grey, cool.
Meshgrid
Whoa! These plots are cool but there’s a trick to them. Let’s take a step back to
understand what we did. The function was defined using element by element
algebra on arrays x and y.
V=(k*q./(x.^2+(y-d).^2).^(1/2)) - ...
(k*q./(x.^2+(y+d).^2).^(1/2));
But what are these x and y arrays? Lets make some little grids that are easily investigated.
dx = 0.5; dy = 0.5;
[x,y] = meshgrid(-1:dx:1,-1:dy:1)
This should produce:
x=
y=
-1.0000 -0.5000
0 0.5000 1.0000
-1.0000 -1.0000 -1.0000 -1.0000 -1.0000
-1.0000 -0.5000
0 0.5000 1.0000
-0.5000 -0.5000 -0.5000 -0.5000 -0.5000
-1.0000 -0.5000
0 0.5000 1.0000
0
0
0
0
0
-1.0000 -0.5000
0 0.5000 1.0000
0.5000 0.5000 0.5000 0.5000 0.5000
-1.0000 -0.5000
0 0.5000 1.0000
1.0000 1.0000 1.0000 1.0000
1.0000
In the function, V(x,y), each element of x gets used with the same element of y. That is,
xn,m is used with yn,m. Notice that as long as n is the same, x will have a constant value
and as long as m is the same, y will have a constant value. This is Matlab’s special way
of using element by element algebra to make functions of two variables.
106733537 2/13/16
Page 2 of 3
Assignment: M3_Graph3D
1. Make a 3-D plot of the Gaussian function G( x, y)  e(  x  y ) over the range -6<(x,y)<6
using 51 bins in x and 51 bins in y. Test the function G(26,26) and at G(26,20). Be sure
to record the values of your test in your report.
2
2
2. Determine the electric potential for a quadrupole consisting of q= -1.0 nC at both (x =
+/- 2, y=0), and q = +1.0 nC at both (x=0, y=+/- 2). Make a surface plot and a contour
plot on the range -4 < x,y < 4 using 101 bins in x and 101 bins in y. Test the function at
V(20,30) and record the values in your report..
3. Plot your electric quadrupole potential using meshes with
A: dx=0.3, dy=0.3
B: dx=0.5, dy=0.5
C: dx=0.05, dy=0.05
Include the plots in your report. Explain why they look different and decide which best
represents the physics.
106733537 2/13/16
Page 3 of 3
Download