4
$\begingroup$

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]
$\endgroup$
6
  • $\begingroup$ Are you trying to export the actual points that NDSolve is using? $\endgroup$ Commented Sep 11, 2012 at 0:40
  • 4
    $\begingroup$ I HIGHLY caution against using variables such as E and I in Mathematica. These can have very bad side effects. $\endgroup$
    – kale
    Commented Sep 11, 2012 at 0:46
  • 1
    $\begingroup$ @kale ... because E and I are definitely not variables in Mathematica... ;) $\endgroup$ Commented Sep 11, 2012 at 0:58
  • $\begingroup$ @J.M. Ha. Hence the "HIGHLY" part. $\endgroup$
    – kale
    Commented Sep 11, 2012 at 0:58
  • 3
    $\begingroup$ Also, your function is stiff $\endgroup$ Commented Sep 11, 2012 at 7:04

2 Answers 2

6
$\begingroup$

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]

enter image description here

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

enter image description here

$\endgroup$
1
  • $\begingroup$ 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! $\endgroup$
    – vyperultra
    Commented Sep 11, 2012 at 12:18
5
$\begingroup$

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

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

enter image description here

$\endgroup$
3
  • 2
    $\begingroup$ 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. $\endgroup$ Commented Sep 11, 2012 at 0:59
  • 1
    $\begingroup$ 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 $\endgroup$ Commented Sep 11, 2012 at 2:47
  • 1
    $\begingroup$ If you'll look at the contents of InterpolatingFunctionAnatomy.m, you'll see how the exported functions map to hidden internal functionality of InterpolatingFunction[]. $\endgroup$ Commented Sep 11, 2012 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.