8
$\begingroup$

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):

Ex=(k*q1(x+1))/(((x+1)^2+y^2)^(3/2))+(k*q2(x-1))/(((x-1)^2+(y-2)^2)^(3/2))
Ey=(k*q1*y)/(((x+1)^2+y^2)^(3/2))+(k*q2*y)/(((x-1)^2+(y-2)^2)^(3/2))

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}
$\endgroup$
11
$\begingroup$

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

plot

Notes

  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).
$\endgroup$
  • $\begingroup$ Thanks for the in depth comment, I'll look into that $\endgroup$ – thefreeman Feb 5 '17 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 '17 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$ – nilo de roock Aug 12 '18 at 19:15

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.