2
$\begingroup$

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}}]}]

enter image description here

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[1234567]

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

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

1 Answer 1

9
$\begingroup$
SeedRandom[1234567];
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}];
f = -Grad[phi, {x, y}]

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}]]]

fig1

$\endgroup$
4
  • $\begingroup$ thank you very much! looks great! $\endgroup$
    – TomKerr
    May 6, 2019 at 21:33
  • $\begingroup$ @Tom You're welcome! $\endgroup$ May 6, 2019 at 22:11
  • $\begingroup$ @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. $\endgroup$
    – noir1993
    Jun 24, 2020 at 16:59
  • $\begingroup$ @noir1993 With this normalization we have 0 total charge, so at large scale there is clear dipole electric field - see right picture. $\endgroup$ Jun 24, 2020 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.