Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]
share|improve this question
    
Are you trying to export the actual points that NDSolve is using? –  David Slater Sep 11 '12 at 0:40
4  
I HIGHLY caution against using variables such as E and I in Mathematica. These can have very bad side effects. –  kale Sep 11 '12 at 0:46
1  
@kale ... because E and I are definitely not variables in Mathematica... ;) –  J. M. Sep 11 '12 at 0:58
    
@J.M. Ha. Hence the "HIGHLY" part. –  kale Sep 11 '12 at 0:58
2  
Also, your function is stiff –  belisarius Sep 11 '12 at 7:04
show 1 more comment

2 Answers

up vote 4 down vote accepted

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

share|improve this answer
    
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! –  vyperultra Sep 11 '12 at 12:18
add comment

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

share|improve this answer
1  
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. –  J. M. Sep 11 '12 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 –  David Slater Sep 11 '12 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[]. –  J. M. Sep 11 '12 at 4:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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