Extract one dimension from an n-dimensional InterpolatingFunction
Using some underdocumented functionality:
x = NDSolveValue[{x'[t] == -x[t], x[0] == {10, -10, 4}}, x, {t, 0, 5}];
pts = Transpose[Append[x["Coordinates"],
Total[Drop[x["ValuesOnGrid"], None, {2}], {2}]]];
xsum = Interpolation[pts, InterpolationOrder -> x["InterpolationOrder"][[1]],
Method -> x["InterpolationMethod"]];
Plot[xsum[t], {t, 0, 5}]
Taking a vector-valued InterpolatingFunction[]
apart: A blow-by-blow account
The package DifferentialEquations`InterpolatingFunctionAnatomy`
features a number of functions for taking an InterpolatingFunction[]
apart. The secret behind this package is that there is a built-in, yet undocumented way to extract required parts of an InterpolatingFunction[]
object, and the functions in this package are but an interface for this.
In the code given above, I used four of these "parts": "Coordinates"
, which gets the values of the independent variable ("Grid"
is an alternative); "ValuesOnGrid"
, which gives the corresponding values of the dependent variable; "InterpolationOrder"
, the order of the polynomial pieces used in the interpolation; and "InterpolationMethod"
, which gives the method used for the interpolation.
To use the OP's simpler example of just extracting the first component, here's how it's done. x["Coordinates"]
gives a list in the form {{x1, x2, …}}
, while x["ValuesOnGrid"]
yields a list of the vector values. To get just the first component of each vector, you can do x["ValuesOnGrid"][[All, 1]]
for this extraction, before massaging it into a nice list of pairs. One such way is
x1 = Transpose[Append[x["Coordinates"], x["ValuesOnGrid"][[All, 1]]]]
tho as with list-manipulation tasks in general, there are a lot of other ways to proceed. You can then feed this to Interpolation[]
, like so:
Interpolation[x1]
but to be safe, we have the InterpolationOrder
and Method
options be inherited from the original interpolant; thus,
Interpolation[x1, InterpolationOrder -> x["InterpolationOrder"][[1]],
Method -> x["InterpolationMethod"]]
x = NDSolveValue[x'[t] == -x[t] && x[0] == {10, -10, 4}, x, {t, 0, 5}]
y[t_?NumericQ] := x[t][[1]] + x[t][[3]]
sum = FunctionInterpolation[y[t], {t, 0, 5}];
Plot[{x[t], sum@t}, {t, 0, 5}]
But I believe just using y[t]
should do