# Electric Field Plot

I want to show an electric field of several arrangements of point charges in xy-plane. I wrote a routine that plots an electric field of a charge:

r0 = {a, b};
r1 = {-a, b};
r2 = {-a, -b};
r2 = {-a, -b};
pot[r_] := q/Norm[r - r0] + q/Norm[r - r2] - q/Norm[r - r1] - q/Norm[r - r3]
fld[r_] := (q*(r - r0)/Norm[r - r0]^3 + q*(r - r2)/Norm[r - r2]^3 - q*(r - r1)/Norm[r - r1]^3 - q*(r - r3)/Norm[r - r3]^3)
a = 2.5;
b = 2.5;
q = 1;
StreamPlot[fld[{x, y}], {x, 0, 5}, {y, 0, 5},PlotRangePadding -> None, FrameLabel -> "electric field",Epilog -> {Red, Disk[r0, 0.07], Blue , Line[{{0, 5.5}, {0, 0}, {5.5, 0}}]}] Now I want to show:

a) 2 charges with inverted sign

b) 4 charges on edges of a cuboid in xy-plane (edges connect charges with inverted sign)

c) 6 randomly distributed charges with vanishing total charge by using RandomReal and initialize random generator with SeedRandom

Could someone help me out with a,b,c ? Thank you very much!

• What is the part that you don't know how to do in mathematica? – rhermans May 5 '19 at 11:23
• just how to translate this problems to code to visualize the Electric Field – Tom May 5 '19 at 11:29
• Does this help supermath.info/ElectricFieldsfromPtCharges.pdf? – Moo May 5 '19 at 12:41
• this helps a lot, thank you! The only thing that is not clear how to visualise c) by using SeedRandom and RandomReal on charges.. – Tom May 5 '19 at 19:16
• @Moo I don´t get it. Could you show how to visualize c) or someone else ? – Tom May 6 '19 at 12:15

SeedRandom;
q = RandomReal[{-1, 1}, 6];
r0 = RandomReal[{-3, 3}, {6, 2}]
q = q - Total[q]/6;
phi = Sum[
q[[i]]/Sqrt[({x, y} - r0[[i]]).({x, y} - r0[[i]])], {i, 1, 6}];

Show[StreamPlot[Evaluate[f], {x, -4, 4}, {y, -4, 4},
StreamColorFunction -> "Rainbow",
StreamColorFunctionScaling -> False],
Graphics[Table[
If[q[[i]] < 0, {Blue, PointSize[.1*Abs[q[[i]]]],
Point[r0[[i]]]}, {Red, PointSize[.1*Abs[q[[i]]]],
Point[r0[[i]]]}], {i, 1, 6}]]]


and on a large scale

Show[StreamPlot[Evaluate[f], {x, -40, 40}, {y, -40, 40},
StreamColorFunction -> "Rainbow",
StreamColorFunctionScaling -> False],
Graphics[Table[
If[q[[i]] < 0, {Blue, PointSize[.1*Abs[q[[i]]]],
Point[r0[[i]]]}, {Red, PointSize[.1*Abs[q[[i]]]],
Point[r0[[i]]]}], {i, 1, 6}]]] • thank you very much! looks great! – Tom May 6 '19 at 21:33
• @Tom You're welcome! – Alex Trounev May 6 '19 at 22:11
• @AlexTrounev Can you please tell why did you add the statement q = q - Total[q]/6; in your code? It seems to work fine without it. – noir1993 Jun 24 at 16:59
• @noir1993 With this normalization we have 0 total charge, so at large scale there is clear dipole electric field - see right picture. – Alex Trounev Jun 24 at 20:01