Exporting NDSolve Data to Excel

In summary, you can export the data from Mathematica by exporting it as a text file and importing it into Excel. If you have more than one y variable to output, you can create lists for yvalues2, yvalues3, etc.
  • #1
knwest
1
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
  • #2
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.
 
  • #3
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.
 
  • #4
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 *)
 
  • #5
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.)
 

Related to Exporting NDSolve Data to Excel

1. How can I export NDSolve data to Excel?

To export NDSolve data to Excel, you can use the Export function in Mathematica. This function allows you to specify the file format as Excel, and then save your NDSolve data in a .xlsx file for easy access in Excel.

2. Can I export only certain time steps or data points from NDSolve to Excel?

Yes, you can use the Part function in Mathematica to select specific time steps or data points from your NDSolve output, and then export only those selected points to Excel using the Export function.

3. Is there a way to automatically update the Excel file with new NDSolve data?

Yes, you can use the Dynamic function in Mathematica to create a dynamic link between your NDSolve data and the Excel file. This way, any changes or updates in your NDSolve data will automatically be reflected in the Excel file.

4. Can I export data from multiple NDSolve simulations to different sheets in an Excel file?

Yes, you can use the Export function with the option "Sheets" to specify different sheets in the Excel file for each NDSolve simulation. This way, you can easily organize and compare data from multiple simulations in one Excel file.

5. Are there any limitations to exporting NDSolve data to Excel?

There are no major limitations to exporting NDSolve data to Excel. However, depending on the complexity of your NDSolve simulation and the amount of data, the Excel file may become large and take longer to open or manipulate. It is recommended to use the Export function with the option "Data" to only export the data points and not the entire NDSolve output, which can help reduce the file size.

Similar threads

  • Computing and Technology
Replies
3
Views
1K
  • Computing and Technology
Replies
9
Views
1K
  • Computing and Technology
Replies
1
Views
985
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
200
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
11
Views
881
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
813
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Computing and Technology
Replies
9
Views
2K
Back
Top