Legal Notice, Disclaimer, and Book Ordering Information:

(* 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. 

 *)

Chapter 2, Section 3, Problem 2:   
Uniformly Charged Sphere

<<mainDir:subDir:ch2init.m  (* Mac  Version *)

<<mainDir/subDir/ch2init.m  (* Unix Version *)

Solution

Clear["Global`*"];

eq1 = { 4 Pi r^2 Eoutside == totalCharge    (*r>R*),
        4 Pi r^2 Einside  == enclosedCharge (*r<R*)};

totalCharge    = 4 Pi R^3/3 density;
enclosedCharge = 4 Pi r^3/3 density;

sol= Solve[eq1,{Einside,Eoutside}][[1]]

efield[rr_,RR_]:=(Eoutside/.sol/.{r->rr,R->RR}) /; rr > RR;
efield[rr_,RR_]:=(Einside /.sol/.{r->rr,R->RR}) /; rr <=RR;

p1=Plot[efield[r,1]/.density->1,{r,10^-6,3}];

medium1={GrayLevel[.7],Polygon[{{1,.51},{1,0},{3,0},{3,.51}}]};
medium2={GrayLevel[.5],Polygon[{{0,.51},{0,0},{1,0},{1,.51}}]};

Show[ {Graphics[{medium1,medium2}] 
      ,Graphics[{ Text["Inside ",{.5,.3}], 
                  Text["Outside",{2,.3}] }]
      ,p1 
      } ,AxesLabel-> {"Distance ", "Electric Field"}
        ,Axes->True
    ];

potoutside[r_,R_]= 
(  -Integrate[ Eoutside/.sol, {r,rMax,r}]
       // Limit[#,rMax->Infinity]& 
)

potinside[r_,R_]=
(potoutside[R,R] - Integrate[Einside/.sol,{r,R,r}]
   )//Simplify

potential[r_,R_]:= If[r>R,potoutside[r,R]
                         ,potinside[ r,R]];

p2=Plot[potential[r,1]/.density->1,{r,10^-6,3}];

Show[ {Graphics[{medium1,medium2}]
      ,Graphics[{ Text["Inside ",{.5,.3}], 
                  Text["Outside",{2,.3}] }]
      ,p2
      } ,AxesLabel-> {"Distance ", "Potential Field"}
        ,Axes->True
    ];

Clear[pot];
pot[x_,y_,z_:0,R_:1,density_:1]:=
 If[(x^2+y^2+z^2)<R^2
        ,(density (-(x^2+y^2+z^2) + 3 R^2))/6
        ,(density R^3)/(3 Sqrt[x^2+y^2+z^2])
   ]

potPlot=  
ContourPlot[ pot[x,y] ,{x,-2,2},{y,-2,2}
         ,ContourShading->False
         ,ContourSmoothing->True];

ePlot= 
PlotGradientField[-pot[x,y] ,{x,-2,2},{y,-2,2}];

Show[potPlot,ePlot];

ePlot3D=
PlotGradientField3D[-pot[x,y,z] ,{x,0,2},{y,0,2},{z,0,2}
      ,VectorHeads->True];

point[r_,R_:1]:= {r Sin[theta] Cos[phi]
                 ,r Sin[theta] Sin[phi]
                 ,r Cos[theta]}

potPlot3D[r_]:=  
ParametricPlot3D[  Evaluate[ point[r] ]
             ,{theta,0,Pi/2},{phi,0, Pi/2}
             ,PlotPoints->12  ]

Show[ePlot3D
    ,Table[potPlot3D[r],{r,1/2,2,1/2}]
    ,PlotRange->{0,1} ];