Vous êtes sur la page 1sur 7

Visualizing Electrostatic Phenomena Using Mathematica

Eric Mayes
Department of Computer Science, Mathematics and Physics
Arkansas State University
State University, AR 72467
Abstract

A set of packages for visualizing electrostatic phenomena was developed using Mathematica as a programming language.
These packages allow users to plot potential fields, equipotential lines, 2-D and 3-D vector fields in order to gain a visual
understanding of electrostatic charges. They would be useful in accompanying undergraduate physics labs pertaining to
electrostatics, as they would enable students to connect experiment with mathematics through open-ended visual exploration.

Introduction

Mathematica (Wolfram, 1991) is a symbolic mathematics computer program that was first released in 1988. It
can perform symbolic calculations, produce beautiful
graphics, and give very accurate results limited only by
the system that it is run on. Its most powerful feature,
though, is that it is also a programming language. The
purpose of this project was to visualize electrostatic phenomena and Mathematical easily utilized, yet powerful,
graphics abilities made it a perfect choice. The project is
open-ended, in that others can easily create new change
distributions and view them.

through further manipulation, can be displayed on the


screen or output in several forms, including printing to
Post Script based laser printers.

Electrostatics Package
The first package (Listing 1), named "Electrostatics,"
is loaded into Mathematica Version 2.0 as:

In [l]:=ElectroStatics

There are four basic commands in this package. Two


prompt the user for the charge distribution, and the

Theory

To produce the images of the electrostatic potentials


and the electric fields, explicitly forms of the electric field
and potential were used (Wangsness, 1986). The electric
field is give by:

E(r) =

g4^R|

or more explicitlyby:

_A

[(x - xj)k + (y - yj)y + (z - zj)z]


- y + (y yi y {z- z{y]l
Xi
0 [(x

- +
h
Similarly the scalar potential is given explicitly by:
E(r)
{r)

gi

ti47re0 [(x - xtf + (y - ytf + (* - s,) 2]*r

<(r) = Y,

These forms of the equations are easily manipulated


using Mathematical symbolic processing. A "Do Loop"
adds up the contributions from each source point. For
the electric field, a vector field is created; and for the electrostatic potential, a scalar field is created. These fields,

other two accept list data types. The first command is


"MakePField." This command is executed at the prompt
in the form:
In[2]:=MakePField[N, {x, xmin, xmax}, {y, ymin, ymax},
(other opts)]

where "N" is the number of charger's in the distribution,


"xmin, xmax, ymin, and ymax" are the bounds on the
graph to be created and "other opts" specify other standard Mathematica graphics options. This command will
return a 3-D surface. The Z-axis of the surface represents
the magnitude of the electrostatic potential. Positive Zaxis values represent positive potentials, as negative values represent negative potentials. Note that Mathematica
willchop off the tops and bottoms of charge's potentials
as they diverge at the charge's locations. As an example:
In[2]:=MakePField[2, {x, -1, 2}, {y,-1, 2}, PlotPoints -> 40]

willprompt the user with:


Charge #1
Charge: ?

Proceedings Arkansas Academy of Science, Vol.48, 1994


116

The user can then enter the magnitude. The magnitude is


entered in electrostatic units instead of Coulombs to keep
the calculations simpler and faster. The user also enters
the position of each charge up to "N"charges.
The second command is "MakeEField." This command takes the same form as the first, only it returns a
plot of the electric field instead of the potential.
The third and fourth commands perform the same
tasks as the first two, only they do not prompt the user
for the magnitude and position. They expect to receive
data of this form:

called with xO and yO being the center, will create a


Mathematica list representing a ring of radius "radius" and
approximated by "points" number of charges.
Figure 5 uses the third function, PlotPField with a list
representing a ring of radius 3 centered at {0,0} approximated by 20 points. Figure 6 uses the fourth function,
PlotEField with the same list.

{charge, x coord, y coord, ...chargeN, xN coord, yN coord,


N}

As an example:
In[3]:= PlotPField[{-l,1,1, 1,-1, 1,2}, {x,-2,2}, {y,-2,2},
PlotPoints -> 40]

These two commands were made so that the user could


graph a function that would specify a specific charge distribution.

Figures la and lb were created with the MakePField


command, with N = 2. Figures 2a and 2b were created by
displaying the result of la and lb as

contour

plots using

the standard Mathematica command:


Show [ContourGraphicsf'la or lb"], ContourShading ->
False}

These plots are the equipotential lines for the charge disributions. Figures 3a and 3b were created for the same
charge distribution with the MakeEField command.
Figures 4a and 4b are combinations of 2a, 2b and 3a, 3b
howing the electric field lines perpendicular to the
equipotential lines. The third and fourth list input commands allow the user to display charge configurations
generated by a function, rather then entering each charge
individually.
As one way to utilize these commands, we can introduce a function like this:
RingOCharge[{x0_, y0_}, radius_, pointsj
Block[{i, theta, charges

Fig. 1. a) Potential Energy Field for a Dipole [Inputs -1 at


{-1,0} and 1 at {1,0}},b) Potential Energy Field for a Pair
of Like Charges.

{}},

Do[AppendTo[charges, 1];
AppendTo[charges, (radius*Cos[theta] + x0)];
AppendTo[charges, (radius*Sin[theta] +y0)];
{theta, 0, 2*Pi, (2*Pi/points)}];
AppendTo[charges, points]; charges]

It should be noted that the RingOCharge function sets


the magnitude of each charge to 1. This function, when

Electrostatics;}

D Package

The second package, "ElectroStatics3D," is loaded in


the same manner:
In[l]:=ElectroStatics3D

There are only

two commands in this

package, one for

Proceedings Arkansas Academy of Science, Vol. 48, 1994


117

user input and the other for list input. Both commands
create a 3-D plot of the electric field surrounding a set of
charges. The first command, MakeEField3D, is called in
almost the same manner as the MakeEField command,
with the addition of a Z-coordinate:

In[2]:=MakeEFields3D[N, {x,xmin, xmax}, {y, ymin, ymax},


{z, zmin, zmax}, (other

opts)]

Figure 7 is an example of this command with four co-planar charges.


Figure 8 demonstrates the second command in this
package along with a slightly modified RingOCharge
function in which the Z-coordinate was kept constant.
This graph was produced from a list and the
PlotEField3D command.

Fig. 3. a) Electric field vectors for a dipole, b) Electric


field vectors for a pair of like charges.

Fig. 2. a) Equipotential lines for a dipole, b)


Equipotential lines for a pair of like charges.

Proceedings Arkansas Academy of Science, Vol. 48, 1994

Fig. 5. Potential energy field for a ring of charge approximated by 20 point charges.

Fig. 4. Combination of equipotential lines and electric


field vectors for two systems of charge, a) Dipole, b) Pair
oflike charges.

Fig. 6. Electric field vectors for a ring of charge approximated by 20 point charges.

Proceedings Arkansas Academy of Science, Vol. 48, 1994


119

Discussion

Fig. 7. 3-D Electric field vectors for a system of four coplanar charges [inputs 2 at {0,0,0}, -1 at {0,1,0}, {-0.866,
0.5, 0} where the -1 charges are separated by 120].

These packages (See Listing 1 for Electrostatics and


Listing 2 for ElectroStatics3D) can be of educational value
if combined with undergraduate physics labs pertaining
to electrostatics. For labs that require the experimental
mapping of equipotential lines, students could also model
the lines on a computer using Mathematica and compare
results. The packages may make learning electrostatics
fun: making interesting charge distributions just to see
what happens can be entertaining. Programming in
Mathematica (Maeder, 1990) and Mathematica, a System for
Doing Mathematics by Computer (Wolfram, 1991) are good
references to help with such a lab.
Modeling complex distributions is time intensive. A
Sun SPARC-IPC, with a SPARC RISC processor running
at 15.8 MIPS and 1.7 MFLOPS, took about 35 minutes to
complete Fig. 8. Unfortunately, Mathematica packages are
not stand-alone. Mathematica has its own kernel which
must be present in order to run the packages; so economic concerns could limit the packages' use, as Mathematica
is rather expensive.
Future work on this project willutilize Mathematica
2.0s ability to animate graphics. Hopefully a test charge
moving through a charge distribution can be modeled in
a reasonable amount of time.
Listing 1. The Code for the "Electrostatics" Package.
(*:Version: Mathematica 2.0*)
(*:Name: Electrostatics 1*)
(*:Title: Electric Potential and Field Plots forStatic Charges *)
(*:Author:
Eric Mayes
Arkansas State University
emayes@quapaw.astate.edu *)
(*:Summary: This package allows the user to input a set ofstatic
charges on the x-y plane. The results, depending on the selected
function, will display either a graph of the magnitude of the electric
potential over a region; or it will display a 2-D vector plot of the
electric field. *)
BeginPackage["ElectroStatics'", "Graphics'PlotField"']
MakePField::usage = "MakePField[N, {x.xmin.xmax},{y,ymin,ymax},
(other opts)] returns a 3-D graph of the potential field of a set ofN
charges specified by the user.";
MakeEField::usage "MakeEField[N, {x,xmin,xmax},{y.ymin.ymax},
(other opts)] returns a graph of the electric field of a set of N
charges specified by the user.";
PlotPField::usage = "PlotPFieldflist, {x.xmin.xmax},{y.ymin.ymax},
(other opts)] returns a 3-D graph of the potential field of a list ofN
charges that conform to the format {chargel,xl,yl...,chargeN,xN,yN
,N}.";

Fig. 8. 3-D electric field vectors for a ring of charge in the


X-Z plane approximated by 40 point charges.

PlotEField::usage = "PlotEField[list, {x.xmin.xmax}, {y,ymin,ymax},


(other opts)] returns a graph of the electric field of a list of N
charges that conform to the format

Proceedings Arkansas Academy of Science, Vol.48, 1994


120

Block[{i,vectorplit, dotsplot, dots = {},field = 0},


Do[
field = field + ((Partfchargelist.i]*
({(xO Part[chargelist, (i+1)]),
(yO Partfchargelist, (i+2)])}))/
((xO (Part[chargelist,(i+l)]+.0001)) A2 +
(yO - (Part[chargelist,(i+2)]+.000 1)^2)^(3/2)),
{i,1,(Last[chargelist]*3),3 }];
=
vectorplot PlotVectorField[field, {xO, xl, x2}, {yO, yl, y2}, opts,
ScaleFunction->(Log[Log[Log[Log[Log[Log[#+ 1]+1]+1]+1]+1]+1]&),
ScaleFactor -> .2, DisplayFunction -> Identity];

L{chargel,xl,yl. .,chargeN,xN,yN,N}.";
n["'Private'"]

rePField[num_,

{x0_, xl_, x2_}, {y0_, yl_, y2_} opts


=
=
Block[{i,chargelist {},potential 0},
Do[
Print [""];
Print["Charge #",ToString[i]];
"];
Print["
answer Inputf'Charge: "];
(

] :-

AppendTo[chargelist,answer];
= Input["XCoord: "];
AppendTo[chargelist,answer];

answer
answer

Do[AppendTo[dots,Point[{ Partfchargelist, (i+1)], Part {chargelist,


(i+2)]}]],{i,l,(Last[chargelist]*3),3}];
dotsplot =Graphics[{PointSize[0.02], dots), DisplayFunction ->
Identity];

Input["Y Coord: "];

AppendTo[chargelist,answer],
{i.num}];

Show[vectorplot,dotsplot,DisplayFunction -> $DisplayFunction]]

AppendTo[chargelist,num];

End [](* ElectroStatics'Private'

Do[

potential = potential + (Part[chargelist,il/


((xO Part[chargelist, (i+l)])*2+
(yO - Partfchargelist, (i+2)])A2)A.5),
{i,l,(Lastfchargelist]*3),3}];
Plot3D[potential, {xO,xl, x2}, {yO, yl, y2}, opts]]
x2_}, {y0_, yl_, y2J,
reEField[num_,
Blockf vectorplot, dotsplot, chargelist {},dots
{x0_,xl_,

{i,

Dof

Protect[MakePField, MakeEField, PlotPField, PlotEField]


EndPackage[] (* Electrostatics* *)

opts

Listing 2. The Code for the "ElectroStatics3D" Package.

] :=

= {},

field

= 0},

(*:Version: Mathematica 2.0 *)

Printf" "];

(*:Name: ElectroStatics3D* *)

Printf'Charge #",ToString[i]];
Print ["
"];
answer = Inputf'Charge: "];
AppendTofchargelist, answer];
answer = Inputf'X Coord: "];
AppendTofchargelist, answer];
answer Input["Y Coord: "];
AppendTofchargelist, answer],
{i,num}];
AppendTofchargelist, num);
Do[(field = field + ((Partfchargelist.i]*
({(xO- Partfchargelist, (i+1)]),
(yO - Partfchargelist, (i+2)])}))/
((xO - (Part[charge!ist, (i+l)]+.0001))^2 +

(*:Title: 3-D Electric Field Plots for Static Charges *)


(*:Author:
Eric Mayes
Arkansas State University
emayes@quapaw.astate.edu

*)

BeginPackage["ElectroStatics3D'",

PlotVectorFieldffield, {xO,xl, x2}, {yO, yl, y2}, opts,

ScaleFunction->(Log[Log[Log[Log[Log[Log[#+l]+l]+l]+l]+l]+l]&),
ScaleFactor -> .2, DisplayFunction > Identity};

(i+2)]}]],{i,l,(Last[chargelist]*3),3}];
dotsplot = Graphics[{PointSize[0.02], dots}, Display Function^
Identity];
Show[vectorplot,dotsplot,DisplayFunction -> $Display Function]]

Dof

] :=

Dof

Printf" "];

Printf'Charge #",ToStrinfi]];
"];

Inputf'Charge: "];
AppendTofchargelist.answer];
answer = Inputf'X Coord: "];
AppendTofchargelist.answer];
answer = Inputf'Y Coord: "];
AppendTofchargelist.answer];
answer = Inputf'Z Coord: "];
AppendTofchargelist.answer],
answer

{i,l,(Last[chargelist]*3),3}];
{xO, xl, x2}, {yO, yl, y2}, opts]]
EField[chargelist_, {xO_, xl_,x2_,}, {yO_, yl_, y2_}, opts

MakeEField3D[num_, {xO_, xl_, x2_}, {yO_, yl_, y2_}, {zO_, zl_, z2_},
] :=
opts
Block[{i, vectorplot, dotsplot, dots {},chargelist = {},field = 0},

Printf" =

potential potential + (Partfchargelist.i]/


((xO Part[chargelist,(i+1)]) A2 +
(yO Part[chargelist,(i+2)]) A2)A.5),

IPlot3D[potential,

Begin["'Private'"]

Do[AppendTo[dots,Point[{ Partfchargelist, (i+1)], Part [chargelist,

ItPField[chargelist_,

PlotEField3D::usage ="PlotEField3Dflist, {x,xmin,xmax} {y,ymin,ymax},


{z.zmin.zmax}, (other opts)] returns a 3-D graph of the electric
field of a list of N charges that conform to the format
{chargel xl,yl,zl...,chargeN,xN,yN,zN,N}."
(

{i,l,(List[chargelist]*3),3}];

{x0_, xl_, x2_}, {y0_, yl_, y2_}, opts


Blockffi, potential =0},

"Graphics'PlotField3D"']

MakeEField3D::usage="MakeEField3D[N, {x,xmin,xmax},{y,ymin,ymax},
{z.zmin.zmax}, (other opts)] returns a 3-D graph of the electric
field of a set of N charges specified by the user."

(yO (Part[chargelist, (i+2)]+.0001)) A2)A(3/2))),


=

*)

(*:Summary: This packages allows the user to input a set of static


charges in 3-space. The result willbe displayed in a 3-D vector field plot.

(*.0001 Added To Protect From Division By Zero *)

vectorplot

*)

] :=

Proceedings Arkansas Academy of Science, Vol. 48, 1994


121

{i.num}];

Maeder, R. 1990. Programming inMathematica.

AppendTo[chargelist,num];
Do[

Addison-Wesley, Redwood City, California.


Wolfram, S. 1991. Mathematica, a System for Doing

field field + ((Partfchargelist.i]*


({(xO Part[chargelist, (i+1)}),
(yO Partfchargelist, (i+2)]),
(zO Part[chargelist, (i+3)])}))/
((xO (Part[chargelist,(i+l)]+.0001))
=

Mathematics by Computer. Addison-Wesley, Redwood


City, California.
A2 +

(*.0001 Added To Protect From Division By Zero *)


(yO (PartfchargelistXi^JJ+.OOOl)^ +
(zO (Part[chargelist,(i+3)]+.0001)) A2)A(3/2)),
{i,l,(Last[chargelist]*4),4}];
vectorplot = PlotVectorField3D[field, {xO, xl, x2}, {yO, yl, y2},
{zO, zl, z2} opts, ScaleFunction > (Log[Log[Log[Log[Log[Log[#+
1]+1]+1]+1]+1]+1]&), ScaleFactor -> .2, DisplayFunction -> Identity];
(

Do[AppendTo[dots,Point[{Part[chargelist,(i+l)],Part[chargelist, (i+2)],
Partfchargelist, (i+3]}]] {i,l,(Last[chargelist]*4),4}];
dotsplot = Graphics3D[{PointSize[0.02], dots},DisplayFunction ->
Identity];
Show[vectorplot,dotsplot,DisplayFunction -> $DisplayFunction]]
(

PlotEField3D[chargelist_, {xO_, xl_,x2_}, {yO_, yl_, y2_}, {zO_, zl_, z2_},


] :=
opts
Block[{i,vectorplot, dotsplot, dots = {},field = 0},
Do[
field = field + ((Partfchargelist.i]*
({(xO Partfchargelist, (i+1)]),
(yO Part[chargelist, (i+2)]),
(zO Part[chargelist, (i+3)])}))/
((xO (Partfchargelist, (i+l)]+.0001))^2 +
(yO (Part[chargelist,(i+2)]+.0001)) A2 +
(zO (Part[chargelist,(i+3)]+.000 1)^2)^(3.2)),
{i,l,(Last[chargelist]*4),4}];
vectorplot = PlotVectorField3D[field, {xO,xl, x2}, {yO, yl, y2},
{zO, zl, z2}, opts, ScaleFunction -> (Log[Log[Log[Log[Log[Log[#+
1]+1]+1]+1]+1]+1]&), ScaleFactor -> .2, DisplayFunction -> Identify];

Do[AppendTo[dots,Point[{ Partfchargelist, (i+1)], Partfchargelist,


(i+2)], Partfchargelist, (i+3)]}]],{i,l,(Last[chargelist]*4),4}];
=
dotsplot Graphics3D[{PointSize[0.02], dots}, DisplayFunction ->
Identity];
Showfvectorplot.dotsplot.DisplayFunction -> $DisplayFunction]]
End [](*ElectroStatics3D'Private*

E)tect[MakeEField3D,

*)

PlotEField3D]

dPackagef] (*ElectroStatics3D* *)

Acknowledgements
This work acknowledges Charles A. Hughes for his
ssistance and the Department of Computer Science,
and Physics at Arkansas State University for
e use of their facilities.

I
Kathematics,

Literature Cited

rangsness,
John

R.K. 1986, Electromagnetic Fields, 2nd ed.


Wiley & Sons, Inc., New York, Pp. 51-82.

Proceedings Arkansas Academy of Science, Vol. 48, 1994


122

Vous aimerez peut-être aussi