First of all, the kind of interpolation produced by NDSolve
for an ODE can depend on the Method
and the setting of InterpolationOrder
. I will answer just for the OP's case, which is the usual case. NDSolve
stores more data than just the abscissae and ordinates. It also stores derivative values at each abcissa, up to the order of the ODE. I will show how to package this in a table of the form
$$\{ \,\{\,x_i,y_i,y_i',y_i'',\dots\,\},\dots\,\}$$
The easy way to do this is the following if sol
is the solution to the OP's NDSolve
command:
With[{f = Subscript[U, 2] /. First@sol},
data = Table[{x, f[x], f'[x], f''[x]}, {x, First@f["Coordinates"]}]]
(* {{0., 0., 1., -2.1684*10^-19}, {0.000107875, 0.000107875, 1., -0.00064725},... *)
There are sometimes very slight round-off errors compared with the data stored in the solution. The following extracts the data from the solution's InterpolatingFunction
for Subscript[U, 2]
. Some description of the internal structure may be found here, What's inside InterpolatingFunction[{{1., 4.}}, <>]?,
and some more can be found here,
Interpolating data with a step. The usual InterpolatingFunction
produced by NDSolve
has the data stored in an argument of the form
{Developer`PackedArrayForm, idcs_, vals_}
where vals
is a flattened array of the ordinates and derivative values. The indices idcs
indicate where to partition vals
to recover the array. Usually in a solution to an ODE these are equally spaced. The code below assumes all this. If it fails, then the solution at hand is not of the form I have been calling "usual."
solvals =
First@ Cases[
Subscript[U, 2] /. First@sol,
{Developer`PackedArrayForm, idcs_, vals_} :>
Partition[vals, Differences@idcs /. {n_Integer ..} :> n],
Infinity];
data = Transpose@ Join[Subscript[U, 2]["Coordinates"] /. First@sol, Transpose[solvals]]
(* {{0., 0., 1., 0.}, {0.000107875, 0.000107875, 1., -0.00064725},... *)
Table[]
? $\endgroup$