(*^
::[     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,  12, "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, w282, h287,  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 = subsubsection; inactive; Cclosed; preserveAspect; startGroup]
Legal Notice, Disclaimer, and Book Ordering Information:
:[font = input; inactive; preserveAspect; endGroup]
(* Legal Notice, Disclaimer, and Book Ordering Information:

Last Modified: FIO January 1, 1995

Mathematica For Physics
Robert L. Zimmerman and Fredrick I. Olness
MathSource Number: 0206-862 

ISBN 0-201-53796-6
For ordering information, call 1-800-822-6339
Addison-Wesley Publishing Company

Communication with the authors:
Fredrick I. Olness:  olness@mail.physics.smu.edu
Robert L. Zimmerman: bob@zim.uoregon.edu

Copyright 1995, Addison-Wesley Publishing Company, Inc. The material in
this file may be distributed freely  so long as the content remains
unchanged, and the copyright and  reference notices are included. The
publisher grants permission for the noncommercial use of these programs
and program segments.  All other uses require the prior written consent
of the publisher.    

All rights are reserved with regard to the  material in the
"Mathematica For Physics" text, and may not be reproduced, stored in a
retrieval system, or transmitted,  in any form or by any means,
electronic, mechanical, photocopying,  recording, or otherwise, without
the prior written permission  of the publisher. 

The programs and applications presented in this book have been included
for  their instructional value. They have been tested with care but are
not  guaranteed for any particular purpose. The publisher does not
offer any  warranties or representations, nor does it accept any
liabilities with  respect to the programs or applications. 

 *)

:[font = title; inactive; initialization; preserveAspect]
5. Electrostatics
:[font = input; initialization; preserveAspect]
*)
(* Some "Mathematica for Physics" Initializations *)
$DefaultFont={"Courier",8};
Off[General::spell];
Off[General::spell1];
PageWidth->70;
(*
:[font = input; initialization; preserveAspect]
*)
Off[General::spell ]; 
Off[General::spell1];
(*
:[font = input; initialization; preserveAspect]
*)
(* Needs["Algebra`SymbolicSum`"]  obsolete *)
(* Needs["Algebra`Trigonometry`"] obsolete *)
Needs["Calculus`VectorAnalysis`"]
Needs["Graphics`Graphics3D`"] 
Needs["Graphics`ParametricPlot3D`"] 
Needs["Graphics`PlotField`"] 
Needs["Graphics`PlotField3D`"]

(*
:[font = subsection; inactive; initialization; preserveAspect]
User-defined Procedures
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
1.Operator:  TrigToY     
:[font = input; initialization; preserveAspect]
*)
TrigToY::usage="The  user defined operator TrigToY[expression,n] 
will expand  an expression of \[Theta] and \[Phi] to order n in spherical harmonics.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
TrigToY[expression_,terms_:2]:= 
Module[{el,m,result}, 
  result=
    Sum[
       Integrate[ (-1)^(m) SphericalHarmonicY[el,-m,\[Theta],\[Phi]]
                  * expression  Sin[\[Theta]]
                ,{\[Theta],0,Pi},{\[Phi],0,2 Pi}]
    * Y[el,m,\[Theta],\[Phi]]
    ,{el,0,terms},{m,-el,el}];
  Return[result]
];
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
2. Operator:   trigtoP     
:[font = input; initialization; preserveAspect]
*)
TrigToP::usage="The  user defined operator TrigToP[expression,n] 
will expand  an expression of \[Theta] to order n in Legendre Functions.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
TrigToP[expression_,terms_:2]:= 
Module[{el,result}, 
  result=
    Sum[ Integrate[ LegendreP[el,Cos[\[Theta]]] expression  Sin[\[Theta]]
                ,{\[Theta],0,Pi}]
        * (2 el+1)/2 P[el,Cos[\[Theta]]]
    ,{el,0,terms}];
  Return[result]
];
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
3. Monopole
:[font = input; initialization; 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.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
Monopole[q_:1,r0_:{0,0,0},r_:{x,y,z}]:= 
   q/Sqrt[ Sum[(r0[[i]]-r[[i]])^2,{i,1,Length[r0]}] ]; 
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
4. PotentialExpansion
:[font = input; initialization; preserveAspect]
*)
PotentialExpansion::usage=
"potentialExpansion[potential,norder] takes an 
electrostatic potential, converts from Cartesian 
coordinates {x,y,z} to spherical coordinates
{r,\[Theta],\[Phi]}, and then performs a series expansion in
1/r to order norder as r->Infinity.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
PotentialExpansion[potentialX_,order_:3]:=
    Module[{potentialR,series,x2rRule},
      x2rRule= Thread[{x,y,z}->
              {r Cos[\[Phi]] Sin[\[Theta]],
               r Sin[\[Phi]] Sin[\[Theta]],
               r          Cos[\[Theta]]}];
      potentialR=potentialX //.x2rRule //Simplify;
      series=(Series[potentialR,{r,Infinity,order}]
               //Normal
               //ExpandAll
               //Simplify
               //Collect[#,r]&  
            );
      Return[series]
   ];
(*
:[font = subsubsection; inactive; initialization; preserveAspect]
4. VEPlot
:[font = input; initialization; preserveAspect]
*)
VEPlot::usage= 
"VEPlot[potential,xlim:{x,-1,1},ylim:{y,-1,1}] 
plots the electric field and equipotential surfaces 
xlim and ylim.";
(*
:[font = input; initialization; preserveAspect]
*)
VEPlot[potential_,xlim_:{x,-1,1.1},ylim_:{y,-1,1.1}]:=
Module[{plot1,plot2},

  plot1=PlotGradientField[-potential 
            ,xlim,ylim
            ,Graphics`Common`GraphicsCommon`ScaleFunction->(1&)
            ,DisplayFunction->Identity];
            
  plot2=ContourPlot[potential 
            ,xlim,ylim
            ,ContourShading->False
            ,ContourSmoothing->True
            ,DisplayFunction->Identity];
            
  Return[  Show[plot1,plot2
               ,DisplayFunction->$DisplayFunction]
         ]
];
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
6.   MultipoleSH
:[font = input; initialization; preserveAspect]
*)
MultipoleSH::usage=
"The procedure MultipoleSH[distribution,rlimit,n] 
takes a charge  distribution of {r,\[Theta],\[Phi]} 
and returns a potential expanded 
to n terms in spherical harmonics denoted by Y[l,m,\[Theta],\[Phi]]. 
The radial extension of the charge density is given by
rlimit. To convert to trigonometric functions let 
Y->SphericalHarmonicY.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
Clear[MultipoleSH];
MultipoleSH[distribution_,rlimit_,n_]:= 
 Module[{q,V}, 
   q:=Integrate[
          (Integrate[ distribution r^2,{r,0,rlimit}])*
          (-1)^(m) SphericalHarmonicY[el,-m,\[Theta],\[Phi]]*
           Sin[\[Theta]] 
        ,{\[Theta],0,Pi},{\[Phi],0,2 Pi}];
   V=Sum[ 4 Pi /(2 el+1) q/r  (rlimit/r)^(el) *
             Y[el,m,\[Theta],\[Phi]] 
          ,{el,0,n},{m,-el,el}];
   Return[V]
 ];
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
7.   MultipoleP
:[font = input; initialization; preserveAspect]
*)
MultipoleP::usage=
"The procedure multipoleP[distribution,rlimit,n] 
takes a charge  distribution that is 
expressed in spherical coordinates {r,\[Theta],0} (symemtric in \[Phi])
and returns a potential. The result is expanded 
to n terms in Legendre polynomials denoted by P[el,Cos[\[Theta]]]. 
The radial extension of the charge density is given by
rlimit. To convert to trigonometric functions let 
P->LegendreP.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
MultipoleP[distribution_,rlimit_,n_]:= 
 Module[{q,pot}, 
   q:=Integrate[ 2 Pi * 
          (Integrate[ distribution r^2,{r,0,rlimit}])*
          Sqrt[(2 el+1)/(4 Pi)] LegendreP[el,Cos[\[Theta]]] 
        * Sin[\[Theta]]
        ,{\[Theta],0,Pi}];
   pot=Sum[ Sqrt[4 Pi/(2 el+1)] q/r (rlimit/r)^(el) 
            * P[el,Cos[\[Theta]]] 
          ,{el,0,n}];
   Return[pot]
 ];
(*
:[font = subsection; inactive; initialization; preserveAspect; startGroup]
Protect User-defined Procedures
:[font = input; initialization; preserveAspect; endGroup]
*)
Protect[TrigToY,TrigToP,Monopole,PotentialExpansion
       ,VEPlot,MultipoleSH,MultipoleP];
(*
^*)
