Mathematic Help

  • Thread starter knwest
  • Start date
  • #1
1
0

Main Question or Discussion Point

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.
 

Answers and Replies

  • #2
28,561
4,867
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
alphysicist
Homework Helper
2,238
1
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
CompuChip
Science Advisor
Homework Helper
4,284
47
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
alphysicist
Homework Helper
2,238
1
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 Threads for: Mathematic Help

Replies
8
Views
5K
Top