Plot the electric field inside of a charged semi cirle.


First I built up the geometry with a set of point charges.

Then I define points within the semi-circle.

Electric field at a single point in 2D space:

I calculate the electric field vectors produced by each individual point charges along the semi circle.

I then sum up the vectors to receive my electric field vector for that specific point in space. (see formula further down)

In the end, I do this calculation for many different points in 2D space and will therefore receive an approximation of the total electric field.

Plot of the electric field.

That's where It beginns to be diffiult... I would like to use ListVectorPlot, but I do not get the desired result. I define a list:

dataVectorPlot = {{x,y}, {totalElectricFieldComponent[x], toatalElectricFieldComponent[y]}}

Where {x,y}: Point location in 2D Space and {totalElectricFieldComponent[x], toatalElectricFieldComponent[y]}: Electric field vector at point {x,y}.

I would like to have the electric field vectors corresponding to the chosen points in space to start at those points. But the result is not like an electric field of a charged semi-circle.

So here is the code:

Definition of constants:

ϵ = 8.8541878176*10^−12;
q = 5;
n = 20; (* Number of point charges *)
aStep = Pi/(2*n); (* Angle Step Every n angle there is a point charge *)
R = 1; (* Radius of half circle *)

Definition of the geometry:

rX = Table[R*Sin[Pi/2 - a], {a, -Pi/2, Pi/2, aStep}];
rY = Table[R*(1 + Cos[Pi/2 - a]), {a, -Pi/2, Pi/2, aStep}];
r = Transpose[{rX, rY}];
geometry = ListPlot[r, AspectRatio -> Automatic, PlotRange -> {{0, R}, {0,   2*R}}]

Calculation of the electric field at every point {x,y} in 2D space:

enter image description here

$r_i$ is the vector of the point charge; $r$ is the vector to the point in 2D (or also 3D) space where we want to calculate the electric field.

Clear[x, y, i] (*local variables*)
np = 5; (*Number of points to evaluate the electric field*)
eX[x_] := 1/(4*Pi*ϵ)*q*Table[(x - rX[[m]])/(Norm[x - rX[[m]] ])^3, {m, 1, n}];
eY[y_] := 1/(4*Pi*ϵ)*q*Table[(y - rY[[m]])/(Norm[y - rY[[m]] ])^3, {m, 1, n}];

The x- and y component of the electric fields produced by each point charge q at (rX,rY), at point (x,y)in 2D space, are put into a list.

totalElectricFieldComponentX[x_] := N[Sum[Part[eX[x], i], {i, 1, n}]]; 
totalElectricFieldComponentY[y_] := N[Sum[Part[eY[y], i], {i, 1, n}]]; 

Sum of the Electric fields produced by each point charge at point point (x,y).

pointsX = {0.01, 0.2, 0.5, 0.7, 0.8};
pointsY = {0.01, 0.5, 1, 1.5, 1.7};

Points where the electric field should be evealuated; Chosen by hand for now.

dataVectorPlot =  Table[{{pointsX[[i]], pointsY[[i]]}, {totalElectricFieldComponentX[i], totalElectricFieldComponentY[i]}}, {i, 1, np}];

Finally, this generates the plot and highlights the problem:

Show[geometry, ListVectorPlot[dataVectorPlot]]

enter image description here

  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Mar 16, 2016 at 12:46
  • $\begingroup$ Instead of formatting code as quotations, you can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this this meta Q&A helpful $\endgroup$
    – Michael E2
    Mar 16, 2016 at 12:46
  • $\begingroup$ Okay, thank you very much! $\endgroup$
    – henry
    Mar 16, 2016 at 13:09
  • $\begingroup$ Thank you @MarcoB for editing my code in a proper way! :) $\endgroup$
    – henry
    Mar 16, 2016 at 13:20
  • $\begingroup$ @DoHe I believe the way you separate the electric field into components is incorrect. $\endgroup$ Mar 16, 2016 at 14:15

1 Answer 1


I couldn't identify the actual source of errors in your code, but I got a few singularities while trying to use Table and Sum together. Below is an alternative way, which I think looks prettier.

Note that I omitted Coulomb's constant.

q = 5; n = 20; R = 1;
pts = Table[
   R {Sin[Pi/2 - a], 1 + Cos[Pi/2 - a]}, {a, -Pi/2, Pi/2, Pi/(n - 1)}];
eField[x_, y_] := 
 q Sum[({x, y} - pts[[i]])/Norm[{x, y} - pts[[i]]]^3, {i, n}]

 ListPlot[pts, PlotStyle -> {Red, PointSize[0.02]}],
 StreamPlot[eField[x, y], {x, -0.3, 1.5}, {y, -.8, 2.8}, 
  StreamStyle -> LightGray],
 PlotRange -> All, AspectRatio -> Automatic, Axes -> False, 
 Background -> Black

enter image description here

  • $\begingroup$ Wow !! The code is very elegant and the output is pretty and meaningful! Thanks a lot !! @thedude $\endgroup$
    – henry
    Mar 16, 2016 at 17:47

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.