field lines
PDF · 3 pages · 43.5 KB
Open PDF file
Printed Mathematica notebook (field_lines.nb, dated 9/9/06) written as optional supplementary material for a course on electromagnetism, filed with Phil's transmission lines overhaul of Feb 2014. It describes the field line algorithm of stepping along E, then the code: a point-charge field routine, a normalized field direction, NDSolve integration with event stopping at charges, and a dipole example plot.
AI-written summary; may contain errors.
Extracted text (machine-read; may contain errors)
Field lines
This is a description of the code behind the calculati ons of field lines presented in class. This is not r equired material for
the course, but it may be of interest to those with a bent for computer modeling. The details of the code are de finitely
specific to the Mathematica language, so some of what is presented below may be opa que. The sequence of steps,
however, is typical of what is needed to do calculati ons of this type. For those with the inclination to follow up, developing
a modeling code usually forces you to come to a better understanding of your material.
üIllustration of field lines
At each point in space there is an electric field E. This is a vector quantity with direction and magni tiude. One can make a
map of the electric field by laying out an array of ve ctors. An alternative way to visualize the field is to specify field lines.
The prescription for finding the field line which goe s through a point p is,
1. Let the position along the field line be x
2. Start at position x=p
3. Find EHxL
4. Take a small step of length „t in the direction of E
5. Assign the new position as xnew Øxold +dx=xold +E „t
6. Repeat 3-5 until done
This specification is equivalent to saying the field line is determined by the differential equation „xÅÅÅÅÅÅÅ„t=E, where x and E
are both vectors. There are many numerical methods fo r solving such equations, some of which are built into
Mathematica . Thus, the steps for producing a field line illustratio n are
1. Select the charges qi and their positions, xi.
2. Select the points pj through which the field lines will pass
3. Provide a routine for calculating EHqi,xi,pjL
4. Give the charges and points to the routine which do es the solution of „xÅÅÅÅÅÅÅ„t=E producing one field line for each point p.
5. Make an illustration of the field lines.
The code below gives an example of how this may be done in practice.
ücode
Almost any program needs to be initialized and provide access to a library of methods for performing useful t asks.
Similarly, it is usually necessary to write a few util ity methods which perform essential tasks, but are not the focus of the
problem at hand. I'll hide both these items.
üinit
üutilitiesfield_lines.nb: 9/9/06::0:57:13 1
üelectric field
We need to calculate the electric field from a collect ion of charges. This is typically done by finding the electric potential
and taking its gradient E=—F, but at this point in the course we haven't yet introd uced potentials, so the code here
calculates E the old fashioned way, by finding E=S
iEi as a sum over the contribution from each charge in th e collection.
Here is the method for calculating the electric fiel d at x2 due to charge q at x1. In this code (x1, x2, dx) are all vectors.The
norm function produces a vector of unit length in the d irection dx. The returned field is seen to be E=qÅÅÅÅÅr2 r`.
In[7]:= el2 @8q_, x1_ <, x2_ D:=Block @8<,
dx =x2 −x1;
qêHdx.dx Lnorm @dx DD
The next method produces a normalized electric field d irection at point x2 due to a collection of charges qs. The syntax
may be obscure, but you can see that the electric field m ethod el2 is accessed, and the result from many charg es is added
by the Plus function. The result is normalized, for a c ouple of reasons. First, many differential equation so lvers work
better if the value of the derivative is not varying by large values. Second, if the direction vector is n ormalized, then the
length of the field line is easily calculated during the integration. A disadvantage is that if one was interested in the line
integral ŸEÿ„l, then one would want the direction vector to have t he magnitude of the electric field.
In[8]:= ehat @qs_, x2_ D:=norm @Plus @@Hel2 @#, x2 D&ê@qs LD
The next method solves the differential equation „xÅÅÅÅÅÅÅ„t=E` for a field line which passes through the pont xx0. It s tops
integrating if the field line hits a charge or esca pes the test volume. The method is set to extend the l ine in both directions
from xx0. The field line is returned as three functions which specify the three coordinates as a function of position along
the field line. The function "check" is true if the end condition has been achieved.
In[9]:= eline @qs_, xx0_ D:=Block @8<,
8x0, y0, z0 <=xx0;
sol =NDSolve @8x$'@tD==ehat @qs,8x$ @tD, y$ @tD, z$ @tD<D@@ 1DD,
y$'@tD==ehat @qs,8x$ @tD, y$ @tD, z$ @tD<D@@ 2DD,
z$'@tD==ehat @qs,8x$ @tD, y$ @tD, z$ @tD<D@@ 3DD, x$ @0D==x0,
y$ @0D==y0, z$ @0D==z0 <,8x$, y$, z$ <,8t,−100, 100 <, Method −>
8"EventLocator", "Event" : >check @qs,8x$ @tD, y$ @tD, z$ @tD<D<D;
8x$, y$, z$ <ê. sol @@1DDD
Finally, the method to find the field lines for a n umber of points.
In[10]:= elines @qs_, pnts_ D:=H8x@#D, y@#D, z@#D<=eline @qs, #DL&ê@pnts;
Next, there are some more utilities to make illustrations, which I will also hide.
üUtilities to make select points make plots etc field_lines.nb: 9/9/06::0:57:13 2
àCases
üdipole
Define list of charges
In[18]:= q1 =81,80, 2, 0<<;
q2 =8−1,80,−2, 0<<;
qs =8q1, q2 <;
Get initial points to start field lines.
In[21]:= pnts =Flatten @getpnts @qs, .1 D, 1D;
Do calculations including turning field lines into an ilustration.
In[22]:= Timing @p@1D=makeelineplot @qs, pnts D;D
Out[22]= 80.771 Second, Null <
And show the result
In[25]:= Show @p@1D, show D;
-4
-2
0
2
4-4 -2 024
-4 -2 024
-4
-2
0
2
4-4 -2 024
More examples follow, but I'll just show the results using Mathematica 's web interface. field_lines.nb: 9/9/06::0:57:13 3