How Can I Extract Data from Mathematica's NDSolve Output for Use in Gnuplot?

In summary, the conversation discusses using Mathematica to solve a differential equation and plot the solution using NDSolve. The solution can be exported as a table of data using Export["*.txt", f[t] /. sol, "Table"]. The data can also be extracted using StepMonitor and Sow/Reap, or by looking at the structure of the InterpolatingFunction. This method can also be used for parametric plots by introducing a new variable and extracting the data from the resulting InterpolatingFunctions. The conversation also mentions using the Mathematica documentation and suggests experimenting to better understand the code.
  • #1
lakmus
23
1
Hi,
I solved a differential equation using Mathematica using NDsolve, I get some solution, but I
only know how to plot it in Mathematica. The problem is that I need the data - numbers -
and I want to work with then a finally make the graph using gnuplot. Is there any way how
can I cat data in text format?

Export["*.txt", f[t] /. sol, "Table"] but the output was in useless format - I need two columns from
which a could make my own chart . . .

Thaks a lot and sorry for my english.
 
Physics news on Phys.org
  • #2
OK, let's use NDSolve to generate Sin[t] for t from 0 to 4 Pi

Code:
nd = First@NDSolve[x''[t] + x[t] == 0 && x[0] == 0 && x'[0] == 1, x, {t, 0, 4 Pi}]
sin = x /. nd[[1]];
which creates an InterpolatingFunction, which can be plotted to give

Plot[sin[t], {t, 0, 4 Pi}]
attachment.php?attachmentid=41293&stc=1&d=1322357740.png


Now, one way to get a table of data for the solution is to just generated it using Table:
table = Table[sin[t], {t, 0, 4 Pi, .2}]
but this doesn't give the raw data generated by NDSolve.

If you look at the structure of InterpolatingFunction by running, e.g., InputForm[sin], then you see that the t values which NDSolve actually calculated are at the position {3,1}, so can be extracted with sin[[3, 1]]. You can plot their values using

ListPlot[{#, sin[#]} & /@ sin[[3, 1]]]
attachment.php?attachmentid=41294&stc=1&d=1322358031.png


Another way to get at the raw data is to use Sow/Reap to extract it as it is generated:

Code:
{nd, data1} = Reap[NDSolve[(x^\[Prime]'')[t] + x[t] == 0 && x[0] == 0 && x'[0] == 1, x, {t, 0, 4 Pi}, StepMonitor :> Sow[{t, x[t]}]]];
Note that the StepMonitor does not catch the initial {0,0} point, so the full raw data is
Code:
data = Prepend[data1[[1]], {0., 0.}];
We can check that this is the same as the data extracted from the InterpolationFunction since the following will return True:
Code:
data == ({#, sin[#]} & /@ sin[[3, 1]])

Finally, to export the data to a file in your home directory as a pair of columns, try something like

Code:
Export[FileNameJoin[{$HomeDirectory, "sin.txt"}], data, "TSV"]

Note that Mathematica tries to guess the format you want from the file extension. ".txt" will just export something like InputForm. In the above I forced the use of tab separated values, but you can also use CSV or Microsoft's XLS, etc...

You can see the file produced by either opening it in a text editor or by using Mathematica to print its raw form:
Code:
FilePrint[FileNameJoin[{$HomeDirectory, "sin.txt"}]]
(* Returns:
0.	0.
0.00010232500915059042	0.00010232500807920588
0.00020465001830118084	0.00020465001401564266
0.003789692138280695	0.0037896791870416077
etc... *)

Note that if you want more points, then it's probably best to use the NDSolve options such as MaxStepSize to generate them. However, you can also just use Table on the original interpolation function to generate the data at any points that you want. Etc...
 

Attachments

  • sin.png
    sin.png
    3.2 KB · Views: 1,596
  • sin1.png
    sin1.png
    1.8 KB · Views: 1,524
  • #3
Thank you very much! It is working and its great, awsome!

I was trying to google something like that all saturday -
could you just tell where you read it or where can I get these knowlege?
 
  • #4
Glad I could help.

Pretty much everything is in the Mathematica documentation. You just have to piece it all together. As for where I learned it? Well, I've been using Mma for quite a while now, so I guess these are just things you pick up...
 
  • #5
Hi, I have just one last problem to this topic - is it possible with the same way
get data for parametric plot? I have system of diffrencial equations and with your help
I cat get f1(x), and f2(x), but I need also table of f1, f2 and manipulating with so many
values is difficult . .. .(I don't understand the principle of mathematica language, so any of
my tries for it doesn't work . .)
thank you.
 
  • #6
OK then,

So let's use the same DE as last time, but turn it into a first order one by introducing y[t] == x'[t].
We can numerically integrate it using the optional Reap/Sow/StepMonitor construction
Code:
{nd, data1} = Reap[NDSolve[
          y[t] == x'[t] && y'[t] + x[t] == 0 && x[0] == 0 && y[0] == 1, 
          {x, y}, {t, 0, 4 Pi}, StepMonitor :> Sow[{t, x[t], y[t]}]]];

As before, the full raw data can be defined using
data = Prepend[data1[[1]], {0, 0, 1}];
and the sin = x and cos = y (interpolation) functions can be extracted using
{sin, cos} = {x, y} /. nd[[1]]
We can check that data is the same as what is hidden in the InterpolationFunctions of sin and cos by checking that the following returns True
data == ({#, sin[#], cos[#]} & /@ sin[[3, 1]])

We can then make the plots using the raw data and the interpolation functions
Code:
GraphicsGrid[{
  {Show[ListPlot[data[[All, {1, 2}]], PlotStyle -> Blue],
         ListPlot[data[[All, {1, 3}]], PlotStyle -> Red]],
   ListPlot[data[[All, {2, 3}]], AspectRatio -> 1]},
  {Plot[{sin[t], cos[t]}, {t, 0, 4 Pi}], 
   ParametricPlot[{sin[t], cos[t]}, {t, 0, 4 Pi}]}}]
attachment.php?attachmentid=41316&stc=1&d=1322434938.png


I hope that this is useful... if you need further details on any step, just ask.
But first, I suggest you try to break down the various constructions to see how they work.
Make small changes to test your understanding, etc...
 

Attachments

  • Grid.png
    Grid.png
    7.3 KB · Views: 1,526

What is the default output format in Mathematica?

The default output format in Mathematica is StandardForm, which displays expressions in a 2D form with operators and functions arranged in a traditional mathematical layout.

How can I change the output format in Mathematica?

To change the output format in Mathematica, you can use the command SetOptions[$FrontEnd, OutputFormatting -> format], where "format" is the desired output format. Some common formats include TextForm, TraditionalForm, and ScientificForm.

Can I customize the output format in Mathematica?

Yes, you can customize the output format in Mathematica by using the command Format[expression, format], where "expression" is the expression you want to format and "format" is the desired output format. This allows you to define your own formatting rules for specific expressions.

How can I save the output in a specific format in Mathematica?

In order to save the output in a specific format in Mathematica, you can use the command Export["filename.format", expression], where "filename" is the desired name for the file and "format" is the desired output format. This will save the output of the expression in the specified format.

What is the difference between InputForm and OutputForm in Mathematica?

InputForm displays expressions in a way that can be used as input in Mathematica, while OutputForm displays them in a readable and aesthetically pleasing way. InputForm is primarily used for inputting expressions, while OutputForm is used for displaying the results of calculations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Replies
5
Views
12K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Back
Top