# Extraction of array from NDSolve output

I have got a system of differential equations. The system has $4$ variables. I am trying to plot trajectory of a particle outside a Kerr Black Hole. I have used Mathematica to calculate all the $4$ differential equation and now I am using NDSolve to calculate the trajectory numerically. The code is

kerr = NDSolve[{
eqnr[s] == 0, eqnt[s] == 0, eqnϕ[s] == 0, eqnθ[s] == 0,
r == 5, θ == Pi/Pi^2, t == 0, ϕ == 0,
r' == 0.01, θ' == 0.003,
t' == 0.01, ϕ' == 0.003
},
{r, θ, ϕ, t}, {s, 0, 2}]


Initial conditions are randomly set. What this code gives as output are 4 arrays of coordinate $r$, $\theta$, $\phi$ and $t$. Now I want to covert this output from cartesian to spherical coordinate system. I know there is a command for doing such a thing but that will not be helpful here as to use that I need to extract the array computed by NDSolve and then insert into the function and then plot the parametric plot.

So my questions are: (i) Is there a direct way to do that? (ii) else how can I extract the computed array of coordinates? (The array that are outputted is shown as in the attached image) • The code does not execute. – Henrik Schumacher Jun 24 '18 at 6:50
• Somebody edited the code. Else it would have. Anyways it's not a problem. What I want is a way to extract the arrays produced by by the NDSolve command. Take any differential equation and solve it using NDSolve whatever the output is how to extract it? – シャシュワト Jun 24 '18 at 7:05
• That was me. And it doesn't make a difference. radial, time, phi, and theta are undefined and seemingly not related to the system of ODE. Actually, there are no ODEs at all. – Henrik Schumacher Jun 24 '18 at 7:07
• Yes I agree. That is why I didn't revert it back. :P It was just to give an idea what I am talking about. Anyway? How to extract the array? – シャシュワト Jun 24 '18 at 7:14
• Use NDSolveValue rather than NDSolve – m_goldberg Jul 4 '18 at 4:10

The InterpolatingFunctions are Hermite splines of order 3. That is, their internal data consists of triplets of position, derivative, and second derivative. As an example, here the triples for r Partition[(r /. kerr[])[[4, 3]], 3]. You would have to manipulate not only the positions but also first and second derivatives. It is easier to transform the trajectories to Cartesian coordinates this way. (Note that I use NDSolveValue only for convenience.)

kerr = s \[Function] Evaluate[
NDSolveValue[{
r''[s] == 0, t''[s] == 0, ϕ''[s] == 0, θ''[s] == 0,
r == 5, θ == Pi/Pi^2, t == 0, ϕ == 0,
r' == 0.01, θ' == 0.003,
t' == 0.01, ϕ' == 0.003
},
{r[s], θ[s], ϕ[s], t[s]}, {s, 0, 2}]
];

SphericalToCart = Block[{r, θ, ϕ, t},
{r, θ, ϕ, t} \[Function]
Evaluate[
Append[CoordinateTransform[
"Spherical" -> "Cartesian", {r, θ, ϕ}], t]]
];

curve = s \[Function] SphericalToCart @@ kerr[s];

• In the code above that I have provided, those are not just accelerations but instead (say) $eqn\phi==0$ is a complete equation which I have not shown above. Equations were monstrously large to be shown here. – シャシュワト Jun 24 '18 at 7:54
• @ShashwatSharan If you provide only an idea what you are talking about in the question, you'll get only an idea how to solve the problem as an answer. "In the code above that I have provided" - a crucial part of solving differential equations are the differential equations. So in fact you didn't provide any code at all. You can either 1) use the idea given by Henrik to work out the solution by yourself, 2) provide everything that's necessary to fully reproduce your issue, or 3) fabricate a nontrivial minimal working example. – corey979 Jun 24 '18 at 11:55
• sorry to sound rude. I was just saying because he edited the code not that it was a problem but it doesn't matter. It's not that I didn't acknowledge his answer it's just that I am new to mathematica and programming hence I didn't understand it much. – シャシュワト Jun 25 '18 at 0:36
• @ShashwatSharan I didn't think you were rude. I just wanted to point out, as a general advice, that the more complete information you give on your actual problem, the better answer you'll get. And, if you consider Henrik's answer helpful in solving your problem, consider accepting it. – corey979 Jun 25 '18 at 21:54

As I understand the author from another of his topics, he investigates the motion of particles in the Kerr metric. His question concerns the mapping of the trajectory of motion using NDSolve[] and ParametricPlot3D[]. I will give a sample code using his own code

 \[Rho] = r^2 + a^2 Cos[\[Theta]]^2;
\[CapitalDelta] = r^2 - 2 m r + a^2;

metric = {{-\[Rho]^2/\[CapitalDelta], 0, 0, 0}, {0, -\[Rho]^2, 0,
0}, {0, 0, -(r^2 +
a^2) Sin[\[Theta]]^2 - (2 m r a^2 Sin[\[Theta]]^4)/\[Rho]^2, \
(2 m a r)/\[Rho]^2 Sin[\[Theta]]^2}, {0,
0, (2 m a r)/\[Rho]^2 Sin[\[Theta]]^2, (1 - (2 m r)/\[Rho]^2)}};

inversemetric = Simplify[Inverse[metric]];
coord = {r, \[Theta], \[Phi], t};
christ[i_, j_, k_] :=
christ[i, j, k] =
Simplify[Sum[
1/2 inversemetric[[i, d]] (D[metric[[d, k]], coord[[j]]] +
D[metric[[d, j]], coord[[k]]] -
D[metric[[j, k]], coord[[d]]]), {d, 1, 4}]]
eq = Table[
coord[[i]]'' +
Sum[christ[i, j, k] coord[[j]]' coord[[k]]', {j, 1, 4}, {k, 1,
4}], {i, 1, 4, 1}];
eqs = eq /. {r'' ->
r''[s], \[Theta]'' -> \[Theta]''[s], \[Phi]'' -> \[Phi]''[s],
t'' -> t''[s],
r' -> r'[s], \[Theta]' -> \[Theta]'[s], \[Phi]' -> \[Phi]'[s],
t' -> t'[s],
r -> r[s], \[Theta] -> \[Theta][s], \[Phi] -> \[Phi][s],
t -> t[s]};
EQ = Table[eqs[[i]] == 0 /. {m -> 1, a -> .3}, {i, 1, 4}];
kerr = NDSolve[{EQ, r == 5, \[Theta] == Pi/2,
t == 0, \[Phi] == 0, r' == -0., \[Theta]' == 0.00006,
t' == 1, \[Phi]' == 0.003}, {r, \[Theta], \[Phi], t}, {s, 0,
2000}];
ParametricPlot3D[
Evaluate[{Sqrt[a^2 + r[s]^2]*Sin[\[Theta][s]]*Cos[\[Phi][s]],
Sqrt[a^2 + r[s]^2]*Sin[\[Theta][s]]*Sin[\[Phi][s]],
r[s]*Cos[\[Theta][s]]} /. kerr /. a -> .3], {s, 0, 2000},
ColorFunction -> Hue, AxesLabel -> {"x", "y", "z"}] 