0
$\begingroup$

I have been using Mathematica to solve a second order differential equation using the second order Verlet method. My code looks like:

x[0]=x0;
y[0]=y0;
vx[0]=vx0;
vy[0]=vy0;

x[n_] := x[n] = x[n - 1] + h (vx[n - 1] - h/2 (x[n - 1]/((x[n - 1])^2 + (y[n - 1])^2)^(3/2)))
y[n_] := y[n] = y[n - 1] + h (vy[n - 1] - h/2 (y[n - 1]/((y[n - 1])^2 + (y[n - 1])^2)^(3/2)))
vx[n_] := vx[n] = vx[n - 1] - h/2 (x[n - 1]/(x[n - 1]^2 + y[n - 1]^2)^(3/2) + x[n]/(x[n]^2 + y[n]^2)^(3/2))
vy[n_] := vy[n] = vy[n - 1] - h/2 (y[n - 1]/(x[n - 1]^2 + y[n - 1]^2)^(3/2) + x[n]/(x[n]^2 + y[n])^(3/2))

I tried using Table but x[2] cannot be evaluated until vx[1] is evaluated. Does someone have a direction as to which function would store the local variables for later use?

$\endgroup$
  • 1
    $\begingroup$ I'm confused by your question. This setup should work fine (although I recommend using numbers in place of the initial conditions, because the expressions will get very large very quickly). If you're interested in what, say, x does, just do Scan[x, Range[0, 3]]. This will automatically evaluate and set the values of x[0] through x[3] and will do the same for whatever quantities those values depend on. You can see that by evaluating ?x afterward. $\endgroup$ – march Jan 19 '16 at 5:31
  • $\begingroup$ x[2] evaluated fine for me. The result is ugly, but Mathematica will calculate it. $\endgroup$ – barrycarter Jan 19 '16 at 16:25

Your Answer

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

Browse other questions tagged or ask your own question.