PHYS 2063 Wave Physics:
Vector Calculus and Field Visualization

Gradient, Divergence, and Curl

Remove["Global`*"]

Define a scalar field and a vector field, and then play with them

ϕ=x y^2 z^3;
v={x y, y^2z,z^2};

w=Grad[ϕ,{x,y,z}]

Div[v,{x,y,z}]

Curl[v,{x,y,z}]

The curl of a divergence is always zero

Curl[w,{x,y,z}]

Apply this to the electric field from a point charge

Remove["Global`*"]

Use SI Units and Cartesian coordinates. First write down the electric potentil

ϕ=1/(4 Pi ϵ0)(q/r)/.r->Sqrt[x^2+y^2+z^2]

The electric field is the negative gradient of the potential

e=-Grad[ϕ,{x,y,z}]

Find the divergence of the electric field

Div[e,{x,y,z}]

Of course, the divergence is zero if we are anywhere except (x,y,x)=(0,0,0).

Simplify[%,x>0]

Gauss’ Law

Show that you get the right integral for the point charge using a Gaussian surface that is a box of side 2a symmetric about the origin

$Assumptions=a>0;
xHat={1,0,0};yHat={0,1,0};zHat={0,0,1};

First just do the side at x=+q, parallel to the yz plane

Integrate[e.xHat/.x->a,{y,-a,a},{z,-a,a}]

By symmetry, all six sides give the same answer, so multiply by six, and you get the right answer, but for completeness...

Integrate[e.xHat/.x->a,{y,-a,a},{z,-a,a}]+
Integrate[e.yHat/.y->a,{x,-a,a},{z,-a,a}]+
Integrate[e.zHat/.z->a,{x,-a,a},{y,-a,a}]+
Integrate[e.(-xHat)/.x->-a,{y,-a,a},{z,-a,a}]+
Integrate[e.(-yHat)/.y->-a,{x,-a,a},{z,-a,a}]+
Integrate[e.(-zHat)/.z->-a,{x,-a,a},{y,-a,a}]

Magnetic Field and Ampere’s Law

Current i in the z-direction, so need the “phi” unit vector in the xy plane

ϕHat={-y,x}/Sqrt[x^2+y^2];
b=μ0 i/(2Pi)(ϕHat/r)/.r->Sqrt[x^2+y^2];

Now use a square of size length 2a for the Amperian loop

Integrate[(b.yHat[[1;;2]])/.x->a,{y,-a,a}]+
Integrate[(b.(-xHat[[1;;2]]))/.y->a,{x,-a,a}]+
Integrate[(b.(-yHat[[1;;2]]))/.x->-a,{y,-a,a}]+
Integrate[(b.xHat[[1;;2]])/.y->-a,{x,-a,a}]

Visualizing the Magnetic Field

Two options are available, VectorPlot and StreamPlot

bPlot=b/.{μ0->2 Pi,i->1}

StreamPlot shows the field direction, but has no information about the field magnitude

StreamPlot[bPlot,{x,-1,1},{y,-1,1}]

VectorPlot is tricky to use if the field changes magnitude by a lot, but you can fix that using the options

VectorPlot[bPlot,{x,-1,1},{y,-1,1},
VectorScale->{Small,0.5,Function[If[#5>5,None,#5]]}]

Using Contours and Fields

Remove["Global`*"]

These are just some mathematics examples of plotting a scalar field and its (negative) gradient

sField=Exp[-0.1((x-3)^2+y^2)]+0.5Exp[-0.1((x+3)^2+y^2)];
vField=-Grad[sField,{x,y}];

xLim=8;yLim=5;

ContourPlot[sField,{x,-xLim,xLim},{y,-yLim,yLim},
AspectRatio->yLim/xLim]

StreamPlot[vField,{x,-xLim,xLim},{y,-yLim,yLim},
AspectRatio->yLim/xLim]

VectorPlot[vField,{x,-xLim,xLim},{y,-yLim,yLim},
AspectRatio->yLim/xLim]

Show[
ContourPlot[sField,{x,-xLim,xLim},{y,-yLim,yLim},
AspectRatio->yLim/xLim,ContourShading->None],
VectorPlot[vField,{x,-xLim,xLim},{y,-yLim,yLim},
AspectRatio->yLim/xLim]]

Fancy example: The Electric Dipole

Remove["Global`*"]

Two charges on the z-axis

d=1;
rp={x,y,z-d/2};
rn={x,y,z+d/2};
v=1/Sqrt[rp.rp]-1/Sqrt[rn.rn];
e=-Grad[v,{x,y,z}];

Make the plot in three dimensions

Show[
VectorPlot3D[e,{x,-3,3},{y,-3,3},{z,-3,3},
VectorPoints->7,
VectorScale->{Small,0.5,3&}],
ContourPlot3D[v,{x,-3,3},{y,-3,3},{z,-3,3},
Contours->{-0.5,-0.1,0,0.1,0.5},
ContourStyle->Opacity[0.25],Mesh->None]]