       Re: a problem about mathematica output

• To: mathgroup at smc.vnet.net
• Subject: [mg82422] Re: a problem about mathematica output
• From: Szabolcs Horvát <szhorvat at gmail.com>
• Date: Sat, 20 Oct 2007 05:44:36 -0400 (EDT)
• References: <ff9rli\$4nj\$1@smc.vnet.net>

```qwer wrote:
> I have a puzzle when using mathematica, I want to out put my resuts to
> a .dat file ,but I do not know how to control the digital,  for
> example:
>
> the mathematica code is
>
> f[x_]:=NIntegrate[Sin[y]/y,{y,0,x}];

You don't need the ; here ... -------^

> OpenWrite["xx.dat",FormatType->TextForm];
> Do[Write["xx.dat",N[x,4],"   ",N[f[x],4]],{x,0.001,1,0.05}];

... or here -------------------------------------------------^

> Close["xx.dat"];
>
> then in xx.dat you can find
> 0   0.
> 0.05   0.0499931
> 0.1   0.0999445
> 0.15   0.149813
> 0.2   0.199556
> 0.25   0.249134
> ..
> ..
> but I want it can be written in the form
> 0.000   0.000
> 0.050   0.050
> 0.100   0.100
> 0.150   0.150
> 0.200   0.200
> 0.250   0.249
> ..
> ..
> It will be look much better!

Try to avoid procedural constructs such as Do[] when using Mathematica.
They have their uses, but Table and Exports are a better fit for this
problem:

dat = Table[{x, NIntegrate[Sin[y]/y, {y, 0, x}]}, {x, .001, 1, .05}]

Export["xx.dat", Round[dat, 0.001], "Table"]

Truncating the result to only 3 digits might not be a very good idea, so
if you are doing this only to *look* at the numbers, then try

NumberForm[dat // TableForm, 3]

--
Szabolcs

```

• Prev by Date: Re: Returning rules from functions inside a package context
• Next by Date: Re: a problem about mathematica output