# Exporting data from a simply generated interpolating function

The question referenced in the link here has a title that matches my question, but the discussion is very complex and does not seem to address my issue. I have solved a partial differential equation using NDSolve, getting an InterpolatingFunction, and I need to export a mesh of interpolated values so that I can process the data in a Python scipt.

Here is my code

nsol1 = NDSolve[{D[y[x, t], t] + 2 D[y[x, t], x] == 3,
y[x, 0] == x + 3, y[5, t] == t + 8}, y[x, t], {x, 0, 5}, {t, 0, 4}]
Plot3D[nsol1[[1, 1, 2]], {x, 0, 2}, {t, 0, 2}]


It plots OK but I don't know how to extract numbers from the function.

Clear["Global*"]

nsol1 = NDSolve[{D[y[x, t], t] + 2 D[y[x, t], x] == 3, y[x, 0] == x + 3,
y[5, t] == t + 8}, y[x, t], {x, 0, 5}, {t, 0, 4}][[1]];


For a mesh of interpolated values

data = Flatten[
Table[{x, t, y[x, t] /. nsol1}, {x, 0, 5, 1/2}, {t, 0, 4, 1/2}], 1];


Plotting,

Show[
Plot3D[y[x, t] /. nsol1, {x, 0, 5}, {t, 0, 4}],
Graphics3D[{AbsolutePointSize[6], Red, Point[data]}]]


We can use the properties of the InterpolatingFunction object produced by NDSolve to get the list of coordinates:

First we get the interpolating function object from nsol1:

intF = nsol1[[1, 1, 2, 0]]


Available list of properties for intF can be obtained using intF["Methods"]:

intF["Methods"]


We can access the xt-coordinates used in interpolation using

grid = intF["Grid"];
grid // Short[#, 20] &


ListPlot[intF["Grid"]]


Alternatively, we can use intF["Coordinates"] to the lists of x and t coordinates and construct tuples of them to get the list of xt coordinates:

Join @@ intF["Grid"] == Tuples@intF["Coordinates"]

 True


Similarly, we can use the property "ValuesOnGrid" to get the z values:

zlst = intF["ValuesOnGrid"];

Short[zlst, 20]


We can combine the grid and zlist to get the list of 3D coordinates used by intF:

xtz = Join[grid, Map[List, zlst, {-1}], 3];

Short[xtz, 10]


We get the x and t ranges using the property "Domain":

intF["Domain"]

{{0., 5.}, {0., 4.}}


Plot the surface and the coordinates used in interpolation:

Show[Plot3D[intF[x, t], {x, 0, 5}, {t, 0, 4}, PlotStyle -> Opacity[.5], Mesh -> None],
ListPointPlot3D[xtz]]


To get the list of 3D coordinates in a single step, use

coords3D = Join @@ Join[intF["Grid"], Map[List, intF["ValuesOnGrid"], {-1}], 3];
`
• (+1) excellent as always. Did not even know that we can extract the data like that. Awesome stuff!
– user49048
Mar 10, 2022 at 4:55
• Thank you for the kind words and upvote @kcr.
– kglr
Mar 10, 2022 at 4:59
• Mar 10, 2022 at 5:33
• @MichaelE2 thanks for bringing this to my attention. Very useful and educational!
– user49048
Mar 10, 2022 at 5:37