# How to write values of function to file?

For example, say I wanted to plot Sin(x) like this:

Plot[Sin[x],{x,0,2 Pi}]


But instead of plotting to a graph, I want to now tabulate the values of Sin[x] to a data file. How do I achieve this?

EDIT: Sorry I wasn't clear enough, I mean plot the x values in one column and the y values in the second column

-
Well, that depends: do you want equal spacing of points, or do you want to make use of mathematica's adaptive sampling? –  Ajasja Jun 19 '12 at 10:23
Just equal spacing for now –  Eddy Jun 19 '12 at 10:32
See the update to my question for { x, Sin[x] } values together as per your question update. –  image_doctor Jun 19 '12 at 10:58

First create your points, running from 0 to 2 Pi at intervals of 0.01.

points = Table[{x,Sin[x]}, {x, Range[0, 2 \[Pi], .01]}];


You can plot them with:

ListLinePlot[points]


And export them to a csv file with:

Export["points.csv", points]

-
I think he need something like this:Table[{x,Sin[x]},{x,xmin,xman,dx}]//Transpose –  yulinlinyu Jun 19 '12 at 16:56
@yulinlinyu Perhaps, I interpreted the OPs use of "column" to mean that the output file should contain 2 columns. If they export the transposed data it will give them two rows and many columns. –  image_doctor Jun 19 '12 at 17:12

You can select the points from the plot and write it to file with Export:

pl = Plot[Sin[x], {x, 0, 2 Pi}];


The Points are arguments to a Line directive in the Plot function:

Position[pl, Line]


output is: {{1, 1, 3, 2, 0}} the zero is the Head of the Line directive, the points are the argument:

points = pl[[1, 1, 3, 2, 1]];


Alternatively you can also produce the {x,y} pairs directly:

points = Table[Sin[x], {x, 0, 2 Pi, 0.1}];


Now I use export to write it all to a file

Export[< output.file >, points, "Table"]


Please note, that there is a typo in your code. You should use square brackets to give function argumens: Sin[x] instead of Sin(x)

Edit:

To get {x,y} pairs from a list of x-positions you can for instance use:

xypairs = {#, Sin[#]} & /@ points


/@ is a shorthand notation for Map. To calculate it directly in the Table command replace Sin[x] by {x,Sin[x]}.

Here is another method to get the points directly from the plot using EvaluationMonitor:

{pl, points} =
Reap[Plot[Sin[x], {x, 0, 2 Pi},
EvaluationMonitor :> Sow[{x, Sin[x]}]]];

-
How do I get the x values in one column and the y values in another column? All I get are the y values –  Eddy Jun 19 '12 at 10:52
@Eddy I updated the answer for the {x,y} pairs. –  Markus Roellig Jun 20 '12 at 8:09

This is to show some of Mathematica's other features.

create a list of x values:

x = Range[0, 2 Pi, 0.1];


create a list of y values:

y = Sin @ x;


(Sin is Listable; if it were not you would use Map: Sin /@ x. Also see Function.)

Put them together:

data = {x, y}\[Transpose];


The Transpose symbol is entered EsctrEsc.

Export data in whatever format you desire, e.g.:

ExportString["datafile.txt", data, "Table"]

-
Just a little comment that there's not need to use the transpose superscript symbol there--although it does look nice in a notebook. A straightforward Transpose[{x,y}] will produce the same result. –  murray Jun 20 '12 at 13:55