# export data points from differential equation system?

I am solving a dynamical three equation system. Besides plotting the individual effects for each of the state variables in an array and in a tridimensional graph, I would like to export the data points to a .csv file. However, when I try to export the table to a .txt file via the "Export" command, the output is not suitable (as it exports the output in a very disorganized way). I was wondering if there could be a way to do this. The code I am using is below:

f = {L, \[Psi], d} /. NDSolve[{
d'[t] == (1/z) (\[Alpha] -
X \[Beta] (d[t]/Y[t]) - \[Gamma] L[t] - \[Delta] \[Psi][
t] - \[Tau] Y[t]) + \[Beta] (d[t]/Y[t]) d[t],
L'[t] == j (Y[t]/K) - e,
\[Psi]'[t] == p L[t] + 0.035 \[Psi][t],
Y[t] == (I + E + \[Alpha] -
X \[Beta] ( d[t]/Y[t]) - \[Gamma] L[t] - \[Delta] \[Psi][
t])/(1 - (1 - \[Tau]) (\[Psi][
t] + (1 - s) (1 - \[Psi][t]) - m z)),
L[0] == 0, \[Psi][0] == 0,
d[0] == 0} /. { \[Alpha] -> 1, \[Gamma] -> 10, \[Delta] -> 10,
X -> 1, \[Beta] -> 0.5, j -> 0.8, \[Tau] -> 0.01, I -> 1,
s -> 0.85, p -> 0.8, E -> 1, m -> 1.3, e -> 0.035, w -> 0.5,
z -> 1.2, K -> 1}, {L, \[Psi], d, Y}, {t, 0, 200}, MaxSteps -> 1000000 ][[1]]
l = {L[t], \[Psi][t], d[t]};
Show[GraphicsArray[Table[Plot[f[[i]][t], {t, 0, 200}, PlotRange -> All,
PlotStyle -> Blue,
AxesLabel -> TraditionalForm /@ {t, l[[i]]},
DisplayFunction -> Identity], {i, 3}]]]
Export["table.txt", f[t] /. sol, "Table"]
ParametricPlot3D[Evaluate[Append[#[t] & /@ f, Red]], {t, 0, 200}, BoxRatios -> {1, 1,0.5}, PlotRange -> All, PlotPoints -> 1500, AxesLabel -> TraditionalForm /@ l]

• Are you trying to export the actual points that NDSolve is using? Commented Sep 11, 2012 at 0:40
• I HIGHLY caution against using variables such as E and I in Mathematica. These can have very bad side effects.
– kale
Commented Sep 11, 2012 at 0:46
• @kale ... because E and I are definitely not variables in Mathematica... ;) Commented Sep 11, 2012 at 0:58
• @J.M. Ha. Hence the "HIGHLY" part.
– kale
Commented Sep 11, 2012 at 0:58
• Commented Sep 11, 2012 at 7:04

Result of NDSolve is an InterpolatingFunction, which is a Mathematica object, and is NOT a list of points. To export numerical list of points for any chosen step of t:

data1 = Table[Evaluate[f[[#]][t] & /@ {1, 2, 3}], {t, 0, 200, .1}];
Export["data1.csv", data1];


This creates data file "data1.csv" in default directory found by evaluating function Directory[]. The data file is now fine, it's easy to check:

data2 = Import["data1.csv"];

data1 === data2


True

Choosing step t small enough will make your data smooth:

ListLinePlot[Transpose@data2, Filling -> 0]


Graphics3D[Point[data2], BoxRatios -> 1]


• Thank you everyone! I am getting my feet wet in Mathematica, and this forum is great for people trying to get familiar with the program. Have a great day! Commented Sep 11, 2012 at 12:18

If you want the actual points NDSolve is using, you can extract that from the Interpolating functions.

    Needs["DifferentialEquationsInterpolatingFunctionAnatomy"];
coords = Map[ First[InterpolatingFunctionCoordinates[#]] &, f];
output = MapThread[Transpose[{#1, #2@#1}] &, {coords, f}];
ListPlot[output]


• No need to load that package, even, since it's just a front end for hidden built-in functionality. f["Coordinates"] (where f[] is the InterpolatingFunction[] object) does the job. Commented Sep 11, 2012 at 0:59
• Interesting. Is that buried somewhere in the Mathematica documentation? When I wanted to do something similar a while back I found reference.wolfram.com/mathematica/tutorial/NDSolvePackages.html which lead me to the solution above Commented Sep 11, 2012 at 2:47
• If you'll look at the contents of InterpolatingFunctionAnatomy.m, you'll see how the exported functions map to hidden internal functionality of InterpolatingFunction[]. Commented Sep 11, 2012 at 4:15