Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm kind of new to Mathematica but I'm getting the hang of it slowly. Anyways, I'm trying to do a project on the electric field of two particles so my professor helped me with this code (sorry in advanced for the length)

Clear[x, y, lof, Δθ];
V = -1/Sqrt[(x + 1)^2 + y^2] + 1/Sqrt[(x - 1)^2 + y^2];
Grad[V, {x, y, z}] // Simplify;
Ex[x_, y_] = -Grad[V, {x, y, z}][[1]];
Ey[x_, y_] = -Grad[V, {x, y, z}][[2]];

Emag[x_, y_] = 
  Sqrt[Ex[x, y]^2 + Ey[x, y]^2] // Simplify // PowerExpand;
lof = {};
Δθ = 10 °;

For[θ = 0 °, θ < 360 °,
 x0 = 1 + .2*Cos[θ];
 y0 = .2*Sin[θ];
 Δs = .01;
 pts = {};
 nsteps = 1000;
 AppendTo[pts, {x0, y0}];
 For[n = 1, n <= nsteps,
  Δx = Ex[x0, y0]/Emag[x0, y0]*Δs;
  Δy = Ey[x0, y0]/Emag[x0, y0]*Δs;
  x = x0 + Δx;
  y = y0 + Δy;
  If[Sqrt[(x - 1)^2 + y^2] > .2, AppendTo[pts, {x, y}]];
  x0 = x;
  y0 = y;
  n = n + 1;
  ];
 AppendTo[lof, Line[pts]];
 θ = θ + Δθ;
 ]
For[ θ = 0 °, θ < 360 °,
 x0 = -1 + .2*Cos[θ];
 y0 = .2*Sin[θ];
 Δs = .01;
 pts = {};
 nsteps = 1000;
 AppendTo[pts, {x0, y0}];
 For[n = 1, n <= nsteps,
  Δx = -Ex[x0, y0]/Emag[x0, y0]*Δs;
  Δy = -Ey[x0, y0]/Emag[x0, y0]*Δs;
  x = x0 + Δx;
  y = y0 + Δy;
  If[Sqrt[(x - 1)^2 + y^2] > .2, AppendTo[pts, {x, y}]];
  x0 = x;
  y0 = y;
  n = n + 1;
  ];
 AppendTo[lof, Line[pts]];
 θ = θ + Δθ;
 ]
ContourPlot[V, {x, -3, 3}, {y, -3, 3},
 Contours -> Table[z, {z, -2, 2, .2}],
 ContourShading -> False,
 ContourStyle -> {{Black, Dashing[{.01}]}},
 Frame -> False, 
 Epilog -> {AbsolutePointSize[7], Blue, Point[{-1, 0}], Red, 
   Point[{1, 0}], Yellow, lof}, Background -> Gray, PlotRange -> All]

The output should be:

enter image description here

What I'm trying to do is do a manipulate function. One that will just move the position of the left particle. So in the code I just want to change the three-starred one (***1***) into a variable for the manipulate function.

 V = -1/Sqrt[(x + ***1***)^2 + y^2] + 1/Sqrt[(x - 1)^2 + y^2];

But I can't figure out the correct syntax. Could someone lend a helping hand? Thank you!

share|improve this question
1  
The code you have is pretty slow, but fortunately the interactivity you are looking for is also very easily realizable by simply pre-rendering a Table of plots in which the charge location is varied step-wise. That list of images can then be quickly manipulated, most easily by using ListAnimate. – Jens Apr 20 '14 at 5:27
1  
You definitely should write your code as a Module, making sure to end lines with ;. Once you've wrapped it inside a function with all variables localized, the displacement of the charge can be introduced as a function parameter. – Jens Apr 20 '14 at 5:50
    
This wolfram activity might also provide some helpful guidance. – bobthechemist Apr 20 '14 at 21:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.