Visualizing Scalar Potentials

Color[q_,fmin_,fmax_]:=If[q==ComplexInfinity,0,Piecewise[{{0.85,q<fmin},{0,q>fmax},{0.85-0.85((q-fmin)/(fmax-fmin)),True}}]]
PlotSection[fieldfn_,xrange_,yrange_,zrange_,numsections_,frange_]:=Block[{zmax,zmin,sectionsep,sectionplot,fmax,fmin,zval,i,seqsectionplot},
fmax=Max[frange[[1]],frange[[2]]];
fmin=Min[frange[[1]],frange[[2]]];
zmax=Max[zrange[[1]],zrange[[2]]];
zmin=Min[zrange[[1]],zrange[[2]]];
sectionsep=(zmax-zmin)/(numsections-1);
zval=zmin+sectionsep*(i-1);
sectionplot=Table[Plot3D[zval,{x,xrange[[1]],xrange[[2]]},{y,yrange[[1]],yrange[[2]]},ColorFunctionScaling->False,ColorFunction->Function[{x,y,z},Hue[Color[fieldfn[x,y,z],fmin,fmax]]]],{i,1,numsections}];
Show[sectionplot,PlotRange->{xrange,yrange,zrange}]
]

PlotSection[fieldfn_,xrange_,yrange_,zrange_,numsections_,frange_]:=Block[{zmax,zmin,sectionsep,sectionplot,fmax,fmin,zval,i,seqsectionplot},
fmax=Max[frange[[1]],frange[[2]]];
fmin=Min[frange[[1]],frange[[2]]];
zmax=Max[zrange[[1]],zrange[[2]]];
zmin=Min[zrange[[1]],zrange[[2]]];
sectionsep=(zmax-zmin)/(numsections-1);
zval=zmin+sectionsep*(i-1);
sectionplot=Table[Plot3D[zval,{x,xrange[[1]],xrange[[2]]},{y,yrange[[1]],yrange[[2]]},ColorFunctionScaling->False,ColorFunction->Function[{x,y,z},Hue[Color[V[x,y,z],fmin,fmax]]]],{i,1,numsections}];
Show[sectionplot,PlotRange->{xrange,yrange,zrange}]
]

PlotMovie[fieldfn_,xrange_,yrange_,zrange_,frange_]:=Animate[Plot3D[h,{x,xrange[[1]],xrange[[2]]},{y,yrange[[1]],yrange[[2]]},ColorFunctionScaling->False,ColorFunction->Function[{x,y,z},Hue[Color[V[x,y,z],frange[[1]],frange[[2]]]]],PlotRange->{xrange,yrange,zrange},PlotPoints->25],{h,-1,0}]

Here are the potentials due to several different simple charge configurations (with ((|q|)/(4πϵ))=1):

1) The potential V around a point charge with Q=4πϵ located at the origin is V= (1/(Sqrt[(x^2)+(y^2)+(z^2)])).

V[x_,y_,z_]:=1/(x^2+y^2+z^2)^(1/2)

2) The potential due to a negative point charge with Q=4πϵ located at (5,0,0) is V= ((-1)/(Sqrt[(((x-.5))^2)+(y^2)+(z^2)]))

V[x_,y_,z_]:=-1/((x-.5)^2+y^2+z^2)^(1/2)

3) The potential due to two positive charges located at (.5,0,0) and (-.5,0,0) is V= (1/(Sqrt[(((x-.5))^2)+(y^2)+(z^2)])) + (1/(Sqrt[(((x+.5))^2)+(y^2)+(z^2)]))

V[x_,y_,z_]:=1/((x-.5)^2+y^2+z^2)^(1/2)+1/((x+.5)^2+y^2+z^2)^(1/2)

4) A dipole.  The potential due to a positive charge at (.5,0,0) and a negative charge at (-.5,0,0) is V= (1/(Sqrt[(((x-.5))^2)+(y^2)+(z^2)])) - (1/(Sqrt[(((x+.5))^2)+(y^2)+(z^2)]))

V[x_,y_,z_]:=1/((x-.5)^2+y^2+z^2)^(1/2)-1/((x+.5)^2+y^2+z^2)^(1/2)

5) The Potential due to four positive charges arranged in a unit square centered on the origin is V= (1/(Sqrt[(((x-.5))^2)+(((y-.5))^2)(z^2)]))+(1/(Sqrt[(((x+.5))^2)+(((y-.5))^2)+(z^2)]))+(1/(Sqrt[(((x-.5))^2)+(((y+.5))^2)(z^2)]))+(1/(Sqrt[(((x+.5))^2)+(((y+.5))^2)(z^2)]))

V[x_,y_,z_]:=1/((x-.5)^2+(y-.5)^2+z^2)^(1/2)+1/((x+.5)^2+(y-.5)^2+z^2)^(1/2)+1/((x-.5)^2+(y+.5)^2+z^2)^(1/2)+1/((x+.5)^2+(y+.5)^2+z^2)^(1/2)

A) Cross-Sections which use color to display the value of the potential.

PlotSection[V,{-1,1},{-1,1},{-1,0},3,{0.1,10}]

B) A horizontal cross-section which shows a contour plot of the values of the potential: (You can change which cross-section you are displaying by changing the value of z)

ContourPlot[V[x,y,0],{x,-1,1},{y,-1,1}]

C) A horizontal cross-section which uses the vertical axis to display the value of the potential: (Notice that you can change which cross-section you are displaying by changing the value of z)

Plot3D[V[x,y,0],{x,-1,1},{y,-1,1},AxesLabel->{"x","y","V(x,y,z=0)"},ColorFunctionScaling->False,ColorFunction->Function[{x,y,z},Hue[Color[z,0.1,10]]],MeshFunctions->{#3 &}]

D) Animated cross-sections which use color to display the value of the potential:

PlotMovie[V,{-1,1},{-1,1},{-1,0},{0.1,10}]

E) Equipotential surfaces: (Notice that you can change the surface you are displaying by changing the value of V in the command)

ContourPlot3D[V[x,y,z]==4,{x,-1,1},{y,-1,1},{z,-1,1}]

By Corinne Manogue and Kerry Browne
Copyright 2007 Corinne A. Manogue