Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematic Help

  1. Apr 19, 2008 #1

    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.
  2. jcsd
  3. Apr 19, 2008 #2


    Staff: Mentor

    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.
  4. Apr 20, 2008 #3


    User Avatar
    Homework Helper

    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 (Text):

    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 (Text):

    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 (Text):

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

         WriteString[filename,xvalues[[i]],"     ",yvalues[[i]],"\n"]
    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 (Text):

    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.
  5. Apr 20, 2008 #4


    User Avatar
    Science Advisor
    Homework Helper

    I strongly recommend exporting the file from Mathematica. All you have to do it
    Code (Text):

    MyPlot = Plot[....];
    Export["file.ext", MyPlot, "format"];
    For example
    Code (Text):

    MyPlot = Plot[....];
    Export["pic.jpg", MyPlot, "JPEG"];  (* JPG *)
    Export["pic.pdf", MyPlot, "PDF"];  (* PDF, Mathematica >= 6.0 *)
    $ExportFormats  (* show all supported formats *)
  6. Apr 20, 2008 #5


    User Avatar
    Homework Helper

    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.)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook