I'm using Mathematica for the first time and I believe I am having trouble with the syntax.

I'm trying to plot a two point electric field and the first step in getting an electric field plot would be to write the equation. In this case I have the following equations(these equations aren't written in mathematica code, just text):


I want to end up with the following:

Efield= {Ex,Ey}

To do this I have tried the following:

r1 = (x - x1)^2 + (y - y1)^2;
r2 = (x - x2)^2 + (y - y2)^2;
Efieldx = (q1 (x - x1))/r1^(3/2) + (q2 (x - x2))/r2^(3/2)
Efieldy = (q1 (y - y1))/r1^(3/2) + (q2 (y - y2))/r2^(3/2)
Efield = {Efieldx , Efieldy}

1 Answer 1


For a first time user you are jumping, if not into the deep end, then at lest into the middle of the pool. Your electric field is a function of two variables and four parameters. This can be expressed in Mathematica by

parametricField[q1_, q2_, {x1_, y1_}, {x2_, y2_}][x_, y_] :=
  Module[{r1, r2},
    r1 = (x - x1)^2 + (y - y1)^2;
    r2 = (x - x2)^2 + (y - y2)^2;
    {(q1 (x - x1))/r1^(3/2) + (q2 (x - x2))/r2^(3/2),
     (q1 (y - y1))/r1^(3/2) + (q2 (y - y2))/r2^(3/2)}]

To get the specific field for a given set of parameters, derive a function of two variables from parametricField with the parameters specified.

Block[{x, y},
  specificField[x_, y_] = parametricField[1, -1, {-1, 0}, {1, 0}][x, y]];

Now the field can be plotted.

StreamPlot[specificField[x, y], {x, -2, 2}, {y, -2, 2}]



  1. Now you know that you need to look up Module, Block, and StreamPlot in the documentation.
  2. You should also look at this tutorial on defining functions..
  3. Module is used here to localize the variables r1 and r2.
  4. The Block wrapper is used here to keep the variables x and y unevaluated even if they have values at top-level (i.e., the Global context).
  • 1
    $\begingroup$ Thanks for the in depth comment, I'll look into that $\endgroup$
    – thefreeman
    Feb 5, 2017 at 9:35
  • $\begingroup$ @thefreeman. If you like this answer you might show your appreciation by accepting it. That is, by clicking on the check mark to the left of the answer. $\endgroup$
    – m_goldberg
    Feb 5, 2017 at 17:14
  • $\begingroup$ Imo, this is really a very good and to the point answer, I particularly like the Notes section of this answer: pointing the OP to areas of the documentation ( or other sources ) for further study. $\endgroup$ Aug 12, 2018 at 19:15

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.