Exporting NDSolve Data to Excel

  • Thread starter Thread starter knwest
  • Start date Start date
  • Tags Tags
    Data Excel
AI Thread Summary
To export data from Mathematica after solving PDEs with NDSolve, users can create a text file containing columns of x and y values. This involves generating x values over a specified range and calculating corresponding y values. The data can be written to a text file using OpenWrite and WriteString functions. For better compatibility with Excel, especially for large numbers, FortranForm can be used to format the output. Users are encouraged to explore Mathematica's built-in plotting capabilities and consider exporting plots directly in various formats like JPEG or PDF for better quality. While Mathematica is powerful for plotting, some users prefer tools like gnuplot for enhanced control over graphical elements when preparing for publication.
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 6.0\times 10^{18} 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.)
 
This week, I saw a documentary done by the French called Les sacrifiés de l'IA, which was presented by a Canadian show Enquête. If you understand French I recommend it. Very eye-opening. I found a similar documentary in English called The Human Cost of AI: Data workers in the Global South. There is also an interview with Milagros Miceli (appearing in both documentaries) on Youtube: I also found a powerpoint presentation by the economist Uma Rani (appearing in the French documentary), AI...
Back
Top