Worksheet 7

PHY201, 2000
Donev Aleksandar

Electrostatic potential of a system of charges

This problem is quite interesting and can be solved in many ways, some efficient and well-structured, others quick and sloppy. I will follow the recommendations given in the assignment, which make this look similar to the corresponding Fortran 90 code.

First we choose positions for the 4 charges and their magnitudes, and enter them as matrices (nested lists in Mathematica):

q = {1, -2,  .5, -1};
r = {{1, 2}, {-1, 
            0}, {- .5, -1}, {2, -1}};

The electrostatic potential at a given point with position R is simply the sum of the potentials from the four individual charges. Each of the individual forces is given with U(R)=(KqQ/Abs)[R - r]. I define the potential as a function of the position R where the potential is measured, using the standard Mathematica constructor: 
Function[x_]:=Expression with x
Just to make sure there are no numerical singularities, we add a small number to the denominator:

mag[r_] := (Sqrt[Abs])[r . r]
U[R_] := (∑_(i = 1)^4) q[[i]]/mag[R - r[[i]]]
Ef[R_] := (∑_(i = 1)^4)(( q[[i]] (R - r[[i]]))/mag)[R - r[[i]]]^3

Now we can plot this function by setting R={x,y}:

Plot3D[U[{x, y}], {x, -3, 3}, {y, -3, 3}, 
      PlotPoints -> 25]

Notice that the potential becomes singular near the four charges, but is otherwise fairly smooth in-between. So it is probably better to plot the coutours of the potential surface:

ContourPlot[U[{x, y}], {x, -3, 3}, {y, -3, 3}, 
      PlotPoints -> 50]

And here is the electric field:

<< Graphics`PlotField`
PlotVectorField[Ef[{x, y}], {x, -3, 3}, {y, -3, 3}, 
      PlotPoints -> 20, MaxArrowLength -> 2.5, 
      ScaleFactor -> 8.5]