(*^
::[	Information =

	"This is a Mathematica Notebook file.  It contains ASCII text, and can be
	transferred by email, ftp, or other text-file transfer utility.  It should
	be read or edited using a copy of Mathematica or MathReader.  If you 
	received this as email, use your mail application or copy/paste to save 
	everything from the line containing (*^ down to the line containing ^*)
	into a plain text file.  On some systems you may have to give the file a 
	name ending with ".ma" to allow Mathematica to recognize it as a Notebook.
	The line below identifies what version of Mathematica created this file,
	but it can be opened using any other version as well.";

	FrontEndVersion = "Macintosh Mathematica Notebook Front End Version 2.2";

	MacintoshStandardFontEncoding; 
	
	fontset = title, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e8,  24, "Times"; 
	fontset = subtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e6,  18, "Times"; 
	fontset = subsubtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, italic, e6,  14, "Times"; 
	fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, grayBox, M22, bold, a20,  18, "Times"; 
	fontset = subsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, blackBox, M19, bold, a15,  14, "Times"; 
	fontset = subsubsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, whiteBox, M18, bold, a12,  12, "Times"; 
	fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  14, "Times"; 
	fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  10, "Times"; 
	fontset = input, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeInput, M42, N23, bold, L-5,  12, "Courier"; 
	fontset = output, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-5,  12, "Courier"; 
	fontset = message, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, R65535, L-5,  12, "Courier"; 
	fontset = print, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-5,  12, "Courier"; 
	fontset = info, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, B65535, L-5,  12, "Courier"; 
	fontset = postscript, PostScript, formatAsPostScript, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeGraphics, M7, l34, w192, h195,  12, "Courier"; 
	fontset = name, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic,  10, "Geneva"; 
	fontset = header, inactive, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = leftheader, inactive, L2,  12, "Times"; 
	fontset = footer, inactive, noKeepOnOnePage, preserveAspect, center, M7,  12, "Times"; 
	fontset = leftfooter, inactive, L2,  12, "Times"; 
	fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  10, "Times"; 
	fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = completions, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	paletteColors = 128; currentKernel; 
]
:[font = section; inactive; Cclosed; preserveAspect; cellOutline; backColorRed = 58981; backColorGreen = 58981; backColorBlue = 58981; startGroup]
Setup:
:[font = input; preserveAspect]
Clear["Global`*"];
:[font = input; preserveAspect; endGroup]
Needs["Calculus`VectorAnalysis`"]
Needs["Graphics`PlotField`"] 
Needs["Graphics`PlotField3D`"]
:[font = section; inactive; Cclosed; preserveAspect; cellOutline; backColorRed = 58981; backColorGreen = 58981; backColorBlue = 58981; startGroup]
Example 1: Dipole
:[font = section; inactive; preserveAspect; cellOutline; startGroup]
Example 1:
Consider  a dipole charge distribution with charge +/-q located at +/-d: 

a) Compute the potential field for this charge distribution. 
b) Graph the equipotential lines  and electric field lines in 2-dimensions. 
c) Find the potential  to the lowest order in the expansion of  1/r. 
;[s]
1:0,1;295,-1;
2:0,19,14,Times,1,18,0,0,0;1,16,12,Times,0,14,0,0,0;
:[font = input; preserveAspect]
Clear["Global`*"];
:[font = subsection; inactive; preserveAspect; cellOutline]
a) Compute the potential field for this charge distribution. 
;[s]
1:0,1;62,-1;
2:0,16,12,Times,1,14,0,0,0;1,16,12,Times,0,14,0,0,0;
:[font = text; inactive; preserveAspect]
Let's start by creating a potential for a dipole field. 
(I'll start in 2-dimensions, but it is easy to generalize to 3-D.)
:[font = input; preserveAspect]
dipole[q_,d_]= +q/Sqrt[(x-d)^2+y^2] -q/Sqrt[(x+d)^2+y^2] 
:[font = subsection; inactive; preserveAspect; cellOutline]
b) Graph the equipotential lines  and electric field lines in 2-dimensions. 
;[s]
1:0,1;77,-1;
2:0,16,12,Times,1,14,0,0,0;1,16,12,Times,0,14,0,0,0;
:[font = text; inactive; preserveAspect]
Let's look at this  plot. 
:[font = input; preserveAspect]
Plot3D[ dipole[1,1] ,{x,-2,2},{y,-2,2}];
:[font = text; inactive; preserveAspect]
Notice how ContourPlot uses almost identical syntax to  Plot3D.
:[font = input; preserveAspect]
vPlot= ContourPlot[ dipole[1,1] ,{x,-2,2},{y,-2,2}
,ContourShading->False];
:[font = text; inactive; preserveAspect]
The electric field is the NEGATIVE of the gradient. 
:[font = input; preserveAspect]
PlotGradientField[-dipole[1,1] ,{x,-2,2},{y,-2,2}];
:[font = text; inactive; preserveAspect]
We can use the ScaleFunction to make the length of the vectors uniform. 
Note, the ScaleFunction is shadowed, so we must specify the full path. 
(This is fixed in version 3.0)
:[font = input; preserveAspect]
Context[ScaleFunction]
:[font = text; inactive; preserveAspect]
The plot of the E-field is:
:[font = input; preserveAspect]
ePlot= PlotGradientField[-dipole[1,1] ,{x,-2,2},{y,-2,2}
   ,Graphics`PlotField`ScaleFunction->(1&)
            ];
:[font = text; inactive; preserveAspect]
We can overlay the plots: 
:[font = input; preserveAspect]
Show[vPlot,ePlot];
:[font = subsection; inactive; preserveAspect; cellOutline]
c) Find the potential  to the lowest order in the expansion of  1/r. 
;[s]
1:0,1;70,-1;
2:0,16,12,Times,1,14,0,0,0;1,16,12,Times,0,14,0,0,0;
:[font = text; inactive; preserveAspect]
Now let's expand the potential in Spherical coordinates. 
First we must convert from Cartesian to Spherical coordinates. 
Here is a cute trick to make a conversion rule for any coordinate system. 
:[font = input; preserveAspect]
x2rRule= 
   Thread[{x,y,z}->
           CoordinatesToCartesian[{r,theta,phi},Spherical]
         ]
:[font = text; inactive; preserveAspect]
Here is the dipole potential in Spherical coordinates. 
:[font = input; preserveAspect]
dipoleR= dipole[q,d] /. x2rRule
:[font = text; inactive; preserveAspect]
Expanding in a power serise, we note that the expansion starts at 1/r^2. 
(Compare with later potential distributions.)
Note that we can expand about Infinity as easily as zero. 
:[font = input; preserveAspect; endGroup; endGroup]
Series[dipoleR,{r,Infinity,4}] //Simplify //PowerExpand
:[font = section; inactive; preserveAspect; cellOutline; backColorRed = 58981; backColorGreen = 58981; backColorBlue = 58981; startGroup]
Problem 2: Linear Quadrupole
:[font = text; inactive; preserveAspect; fontColorRed = 65535; endGroup]
Copy and modify from above. 
:[font = section; inactive; preserveAspect; cellOutline; backColorRed = 58981; backColorGreen = 58981; backColorBlue = 58981; startGroup]
Problem 3: Square Quadrupole
:[font = text; inactive; preserveAspect; fontColorRed = 65535; endGroup]
Copy and modify from above. 
:[font = section; inactive; preserveAspect; cellOutline; backColorRed = 58981; backColorGreen = 58981; backColorBlue = 58981; startGroup]
Problem 4: Triangle
:[font = text; inactive; preserveAspect; fontColorRed = 65535; endGroup]
Copy and modify from above. 
:[font = section; inactive; preserveAspect; cellOutline; backColorRed = 58981; backColorGreen = 58981; backColorBlue = 58981; startGroup]
Example functions:
;[s]
2:0,1;18,0;19,-1;
2:1,19,14,Times,1,18,0,0,0;1,19,14,Times,3,18,0,0,0;
:[font = subsection; inactive; Cclosed; preserveAspect; startGroup]
Sample: Monopole
:[font = special5; inactive; preserveAspect]
     Complex charge distributions can always be represented by a sum of point charge distributions. Since we use this superposition principle repeatedly, it is useful to define a function to compute the potential distribution of a charge in a general manner. The Monopole function will do this in any dimension.
;[s]
3:0,0;263,1;272,0;312,-1;
2:2,13,9,Times,0,12,0,0,0;1,13,10,Courier,1,12,0,0,0;
:[font = input; preserveAspect]
Monopole::usage=
"Monopole[q,{x0,y0,z0},{x,y,z}] computes the potential 
due to a monopole of charge q 
located at position {x0,y0,z0} in {x,y,z} coordinates.";


Monopole[q_:1,r0_:{0,0,0},r_:{x,y,z}]:= 
   q/Sqrt[ Sum[(r0[[i]]-r[[i]])^2,{i,1,Length[r0]}] ]; 
   
Protect[Monopole];
:[font = subsubsection; inactive; preserveAspect]
Example: 
:[font = special5; inactive; preserveAspect]
A simple 2-dimensional example with specified coordinates.
:[font = input; preserveAspect]
Monopole[charge,{1,2},{x1,x2}]
:[font = special5; inactive; preserveAspect]
A dipole is easily formed from 2 monopoles. 
:[font = input; preserveAspect; endGroup]
dipole= ( Monopole[+1,{0,0,-a/2}] + 
          Monopole[-1,{0,0,+a/2}] )
:[font = subsection; inactive; Cclosed; preserveAspect; startGroup]
Sample: VEPlot
:[font = special5; inactive; preserveAspect]
We find it useful to view the equipotential lines and the electric field lines on the same plot. 
:[font = input; preserveAspect]
Clear[VEPlot];

VEPlot::usage= 
"VEPlot[potential,xlim:{x,-1,1},ylim:{y,-1,1}] 
plots the electric field and equipotential surfaces 
xlim and ylim.";

VEPlot[potential_,xlim_:{x,-1,1.1},ylim_:{y,-1,1.1}]:=
Module[{plot1,plot2},

  plot1=PlotGradientField[-potential 
            ,xlim,ylim
            ,Graphics`PlotField`ScaleFunction->(1&)
            ,DisplayFunction->Identity];
            
  plot2=ContourPlot[potential 
            ,xlim,ylim
            ,ContourShading->False
            ,ContourSmoothing->True
            ,DisplayFunction->Identity];
            
  Return[  Show[plot1,plot2
               ,DisplayFunction->$DisplayFunction]
         ]
];

Protect[VEPlot];
:[font = special5; inactive; preserveAspect]
VEPlot simply combines the plot of the equipotential lines obtained from ContourPlot with the electric field obtained from PlotGradientField.   We set DisplayFunction to Identity to suppress plots, and to $DisplayFunction to show plots. 
;[s]
12:0,1;6,0;73,1;85,0;123,1;140,0;151,1;166,0;170,1;178,0;205,1;221,0;238,-1;
2:6,13,9,Times,0,12,0,0,0;6,13,10,Courier,1,12,0,0,0;
:[font = special5; inactive; preserveAspect]
Caution: We set the default limits of PlotGradientField slightly asymmetric to try and avoid hitting any singularities. If the VEPlot function  fails, it is most likely that  PlotGradientField was unable to compute the numerical gradients necessary. If this happens, try using PlotGradientField and ContourPlot separately to isolate the problem. 
;[s]
11:0,0;38,1;55,0;127,1;133,0;175,1;192,0;277,1;294,0;299,1;310,0;347,-1;
2:6,13,9,Times,0,12,0,0,0;5,13,10,Courier,1,12,0,0,0;
:[font = subsubsection; inactive; preserveAspect]
Example:  Equipotential surface and electric field of two point charges
:[font = special5; inactive; preserveAspect]
Consider the equipotential surfaces and electric field of a dipole:
:[font = input; preserveAspect; endGroup; endGroup]

dipole= ( Monopole[+1,{0,-1/2}] + 
          Monopole[-1,{0,+1/2}] );

VEPlot[dipole];
^*)
