1
$\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$
  • $\begingroup$ What is the part that you don't know how to do in mathematica? $\endgroup$ – rhermans May 5 at 11:23
  • $\begingroup$ just how to translate this problems to code to visualize the Electric Field $\endgroup$ – Tom May 5 at 11:29
  • 3
    $\begingroup$ Does this help supermath.info/ElectricFieldsfromPtCharges.pdf? $\endgroup$ – Moo May 5 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$ – Tom May 5 at 19:16
  • $\begingroup$ @Moo I don´t get it. Could you show how to visualize c) or someone else ? $\endgroup$ – Tom May 6 at 12:15
7
$\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$
  • $\begingroup$ thank you very much! looks great! $\endgroup$ – Tom May 6 at 21:33
  • $\begingroup$ @Tom You're welcome! $\endgroup$ – Alex Trounev May 6 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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