Exporting NDSolve Data to Excel

  • Thread starter Thread starter knwest
  • Start date Start date
  • Tags Tags
    Data Excel
Click For Summary

Discussion Overview

The discussion revolves around exporting data from Mathematica's NDSolve function to Excel for graphing purposes. Participants share methods and experiences related to exporting numerical data, particularly from solutions of partial differential equations (PDEs), and discuss the merits of different approaches.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant seeks guidance on exporting data from NDSolve to Excel, specifically wanting to create columns for x and y variables.
  • Another participant suggests a method involving pasting data into a text file, removing braces, and importing it as comma-separated values, while also recommending learning Mathematica's plotting features.
  • A different participant provides a detailed example of exporting data for one y value, including code snippets for generating x and y values and writing them to a text file, while noting potential formatting issues with large numbers.
  • Another contributor recommends using Mathematica's Export function for saving plots in various formats, emphasizing its simplicity.
  • One participant expresses a preference for using gnuplot and pstricks for high-quality output in publications, suggesting that they find these tools better for controlling plot aesthetics compared to Mathematica.

Areas of Agreement / Disagreement

Participants present multiple methods for exporting data, with no consensus on the best approach. Some advocate for using Mathematica's built-in features, while others prefer alternative tools for plotting and exporting.

Contextual Notes

Participants mention potential issues with formatting when exporting large numbers and the varying quality of output between Mathematica and other plotting tools. There is also a suggestion to explore different coding techniques as one gains experience with Mathematica.

Who May Find This Useful

This discussion may be useful for Mathematica users, particularly novices, who are looking to export data for use in Excel or other plotting software, as well as those interested in comparing different plotting and exporting methods.

knwest
Messages
1
Reaction score
0
Hello,

I'm using mathematic (NDSolve) to solve a series of PDEs, which I have successfully done. Now I would like to export the data so that I can make a graph in Excel (I know I can plot the results in Mathematica (and I have) but I would like to be able to export it to Excel, as I am a novice in Mathematica. How can I export the data so that I have columns of data corresponding to my x variable and the series of y variables? Thanks in advance.
 
Computer science news on Phys.org
It is not real easy to do. The best thing that I can recommend is to paste it into a text file, remove the braces, and import it into excel as comma separated values.

But I would strongly recommend learning the Mathematica plotting features, it will be well worth your time.
 
Hi knwest,

Here is a quick way to do it for x and one y value that I think is understandable. Let's say that you have solved the DE with the statement (just to think of a random problem):

Code:
result=NDSolve[{y''[x]+5 x ==0,y[0]==3,y[2]==5,y,{x,0,2}]

so that x varies from 0 to 2. If you want to plot it in mathematica you can just use:

Code:
Plot[y[x]/.result,{x,0,2}]

If you want to write out a text file of 101 values that has two columns separated by several spaces, here is a straightforward way to do it:

Code:
xvalues=Table[x,{x,0,2,(2-0)/100.}]
yvalues=Table[ (y[x]/.result)[[1]],{x,0,2,(2-0)/100.}]

filename="outputfile.txt'
OpenWrite[filename]
Do[ 
     WriteString[filename,xvalues[[i]],"     ",yvalues[[i]],"\n"]
                 ,{i,1,Length[[xvalues]]}]
Close[filename]

As you get more experience with mathematica I would suggest getting rid of the do loops with other methods (I prefer MapThread, for example). (I find Mathematica is very enjoyable for learning how to do things in new ways.)

As written above, the file is written out to the current directory. Also, you may run into issues if the values of your numbers are large. For example, if your number is [itex]6.0\times 10^{18}[/itex] you might find the 18 written on one line and the numbers 6. 10 written on the other. If that happens, you might try writing the inner line of the DO loop as

Code:
WriteString[filename, FortranForm[xvalues[[i]] ],"     ",FortranForm[yvalues[[i]]],"\n"]

and I think you can also use CForm instead of FortranForm. This would write the output as 6.e18 which Excel can understand.

If you have more than one y variable to output, just create lists for yvalues2, yvalues3, etc. and then modify the line inside the DO loop.

Keep trying new ways to do things. The above method is definitely not the best way (I don't even think it's a very good way!) but I think it might be the easiest to understand if you're new to mathematica.
 
I strongly recommend exporting the file from Mathematica. All you have to do it
Code:
MyPlot = Plot[...];
Export["file.ext", MyPlot, "format"];
For example
Code:
MyPlot = Plot[...];
Export["pic.jpg", MyPlot, "JPEG"];  (* JPG *)
Export["pic.pdf", MyPlot, "PDF"];  (* PDF, Mathematica >= 6.0 *)
$ExportFormats  (* show all supported formats *)
 
Hi CompuChip,

I have always found Mathematica's plotting abilities to be an incredible way to explore mathematical results. However, when it comes time to print out or publish those plots and diagrams, I've always found that gnuplot and pstricks give much better quality results-in regards to controlling and displaying tics, labels, legends, axes, etc. (Just my opinion of course.) Because of that, I still find use for writing out files that those programs can read.

(I must admit that I don't have access to the latest version of Mathematica.)
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K