Message Boards Message Boards

Derivatives of the coordinates when using NBodySimulation?

Posted 2 years ago

I am using NBodySimulation and want to find derivatives of the coordinates. What I have done looks like this:

data  = NBodySimulation["InverseSquare", <|.....|>

xx[t_] := Evaluate[data["body1", "Position", t]][[1]]  
yy[t_] := Evaluate[data["body1", "Position", t]][[2]]  
zz[t_] := Evaluate[data["body1", "Position", t]][[3]]  
vxx[t_] := Evaluate[data["body1", "Velocity", t]][[1]]  
vyy[t_] := Evaluate[data["body1", "Velocity", t]][[2]]  
vzz[t_] := Evaluate[data["body1", "Velocity", t]][[3]]  
dvxx[t_] := vxx'[t]  
dvyy[t_] := vyy'[t]  
dvzz[t_] := vzz'[t]

The coordinates xx, yy and zz seem to work properly for body1, but the last six functions do not provide correct results. How can I plot the first and second derivatives of the position coordinates?

POSTED BY: Richard Jensen
4 Replies

Hi Richard

The derivative of the coordinates ("Position") should be equal to the "Velocity"

(data[All, "Position", t]) // D[#, {t, 1}] &
(data[All, "Position", t]) // D[#, {t, 2}] &

Robert

POSTED BY: Robert Nowak

Richard,

you did not specify your NBodySimulation, so I cannot really see what your Evaluate[data["body1", "Position", t]][[1]] here means. I suspect the syntax is not quite correct.

data["Position"] gives an Association containing the respective InterpolatingFunction. Using a NBodySimulation from the examples in the documentation I would do it like so:

data = NBodySimulation["InverseSquare", {
    <|"Mass" -> 1, "Position" -> {0, 0}, "Velocity" -> {0, .5}|>,
    <|"Mass" -> 1, "Position" -> {1, 1}, "Velocity" -> {0, -.5}|>,
    <|"Mass" -> 1, "Position" -> {0, 1}, "Velocity" -> {0, 0}|>}, 4];
{xx, yy, zz} = Values@data["Position"];
{vxx, vyy, vzz} = Values@data["Velocity"];
{dvxx, dvyy, dvzz} = Derivative[1] /@ {vxx, vyy, vzz};

Does that help?

POSTED BY: Henrik Schachner
Posted 2 years ago

It's a great help. Thanks. It returns the coordinates of all three bodies together. Now I would like to plot the coordinates of the individual bodies.

POSTED BY: Richard Jensen

Now I would like to plot the coordinates of the individual bodies.

For this you can use the function ParametricPlot (with the slightly modified code from above):

data = NBodySimulation[
   "InverseSquare", {<|"Mass" -> 1, "Position" -> {0, 0}, 
     "Velocity" -> {0, .5}|>, <|"Mass" -> 1, "Position" -> {1, 1}, 
     "Velocity" -> {0, -.5}|>, <|"Mass" -> 1, "Position" -> {0, 1}, 
     "Velocity" -> {0, 0}|>}, 4];
{xx, yy, zz} = data[All, "Position"];
{vxx, vyy, vzz} = data[All, "Velocity"];
{dvxx, dvyy, dvzz} = Derivative[1] /@ {vxx, vyy, vzz};
ParametricPlot[{xx[t], yy[t], zz[t]}, {t, 0, 4}, PlotStyle -> {Red, Green, Blue}, PlotLabels -> Automatic]

enter image description here

or likewise:

Manipulate[ParametricPlot[{xx[t], yy[t], zz[t]}, {t, 0, tt}, 
  PlotStyle -> {Red, Green, Blue}, PlotLabels -> Automatic, 
  PerformanceGoal -> "Quality"], {tt, .1, 4}]
POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract