# Derivatives of the coordinates when using NBodySimulation?

**URL:** <https://community.wolfram.com/t/derivatives-of-the-coordinates-when-using-nbodysimulation/19575>\
**Category:** Questions\
**Tags:** physics, calculus, graphics-and-visualization\
**Created:** [February 9, 2022, 12:13am UTC](https://community.wolfram.com/t/derivatives-of-the-coordinates-when-using-nbodysimulation/19575 "2022-02-09T00:13:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rxjensen](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@rxjensen](https://community.wolfram.com/u/rxjensen)\
**Post date:** [February 9, 2022, 12:13am UTC](https://community.wolfram.com/t/derivatives-of-the-coordinates-when-using-nbodysimulation/19575/1 "2022-02-09T00:13:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [February 9, 2022, 11:08pm UTC](https://community.wolfram.com/t/derivatives-of-the-coordinates-when-using-nbodysimulation/19575/2 "2022-02-09T23:08:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![robertnowak](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/robertnowak/32/35992_2.png) [@robertnowak](https://community.wolfram.com/u/robertnowak)\
**Post date:** [February 10, 2022, 1:13pm UTC](https://community.wolfram.com/t/derivatives-of-the-coordinates-when-using-nbodysimulation/19575/3 "2022-02-10T13:13:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rxjensen](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@rxjensen](https://community.wolfram.com/u/rxjensen)\
**Post date:** [February 10, 2022, 7:02pm UTC](https://community.wolfram.com/t/derivatives-of-the-coordinates-when-using-nbodysimulation/19575/4 "2022-02-10T19:02:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [February 10, 2022, 8:11pm UTC](https://community.wolfram.com/t/derivatives-of-the-coordinates-when-using-nbodysimulation/19575/5 "2022-02-10T20:11:38Z")

</div>

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

```

 ![NBody.png](https://global.discourse-cdn.com/wolfram/original/3X/8/b/8babd70366bdf32c02855580df6d730a7e81dc73.png)

or likewise:

```
Manipulate[ParametricPlot[{xx[t], yy[t], zz[t]}, {t, 0, tt}, 
  PlotStyle -> {Red, Green, Blue}, PlotLabels -> Automatic, 
  PerformanceGoal -> "Quality"], {tt, .1, 4}]

```
