# Exporting data file in For loop

I prepared a code in which some numerical data can be produced. I want to export the data in terms of counter of loop but I don't know how I can do it. It my please if someone who knows, helps me. Here is my code.

 Clear["Global*"]

m0 = 1;
ω = 1;
m = 1;

For[γ = 0.1, γ <=  1, γ = γ + 0.1,

ψ[x_] =  Sqrt[m0/(1 + γ x^2)^2] E^((-ω m0 )/(2 γ) ArcTan[x Sqrt[γ]]^2) HermiteH[m, Sqrt[(ω m0)/γ] ArcTan[x Sqrt[γ]]];

A = NIntegrate[ψ[x]^2, {x, -∞, ∞}];

n = 1/Sqrt[A];

Ψ[x_] = n ψ[x];

Subscript[S, x] =  NIntegrate[Ψ[x]^2 Log[Ψ[x]^2], {x, -∞, ∞}];

Print[γ, "    ", Subscript[S, x]]]

0.1    -1.43033

0.2    -1.49253

0.3    -1.53077

0.4    -1.54978

0.5    -1.55519

0.6    -1.55161

0.7    -1.54224

0.8    -1.52921

0.9    -1.51391

1.    -1.49725


My thoughts:

1. Don't use For. Either act with functions on lists using Map or Apply etc., use Table, or if you must loop, use Do.

2. It's rare to see the syntax f[x_] = ... used with great success, because this syntax evaluates the RHS immediately. Probably you mean to define a function that computes the RHS for every input x. The syntax for this is f[x_] := ...

3. Don't redefine functions inside loops if you can avoid it. In the loop only γ changes, so make ψ an expression in x, depending on γ, instead.

4. Avoid Subscript for anything except pretty-printing in plot labels etc. They often don't behave the way we naively think.

With all this in mind, here is the rewritten code:

 m0 = 1;
ω = 1;
m = 1;

ψ[γ_] :=  Sqrt[m0/(1 + γ x^2)^2] E^((-ω m0 )/(2 γ) ArcTan[x Sqrt[γ]]^2) HermiteH[m, Sqrt[(ω m0)/γ] ArcTan[x Sqrt[γ]]];

sx[γ_] :=  Module[{a, n, normpsi},
a = NIntegrate[ψ[γ]^2, {x, -∞, ∞}];
n = 1/Sqrt[a];
normpsi = n Ψ[γ];
NIntegrate[normpsi^2 Log[normpsi^2], {x, -∞, ∞}]
]


If you want one result per value of γ you can for instance do

Do[sx[g] >> ToString[g]<>".txt", {g, 0.1, 1, 0.1}]


The part ToString[g]<>".txt" generates a .txt file with a name depending on the value of g, which I guess is the main thing OP is asking for. One can also use Export in this way.

UPDATE

To export all the values in the format specified in the OP, one way is

Do[ToString[γ] <> "    " <> ToString[sx[γ]] >>> "data.txt", {γ, 0.1, 1, 0.1} ]


Here we use >>>, AKA PutAppend, to add new lines to the file for each value of γ.

• + , but note the non-delayed set is fine in cases where nothing on the r.h.s (excepting the function arguments) is going to change (which seems to be the case here). I'd argue delayed set := is somewhat over used. – george2079 Oct 24 '16 at 12:20
• @george2079, the classical counterexample goes like x = 3; f[x_] = 2 x + 1; f. However, I can agree that := is sometimes overused, and especially if the RHS needs subcalculations that can be done symbolically, I would sometimes prefer expressions, and then injecting values using With or Replace(All)` :) – Marius Ladegård Meyer Oct 24 '16 at 12:47
• Many thank for your kind points. But the mail problem and actually my goal in this code is production a data file which includes data as it was shown at the end of code. – AYBRXQD Oct 24 '16 at 15:52
• Aha. See my updated answer. – Marius Ladegård Meyer Oct 24 '16 at 15:58
{"ft_lang_label":"__label__en","ft_lang_prob":0.7182058,"math_prob":0.98281765,"size":694,"snap":"2020-45-2020-50","text_gpt3_token_len":335,"char_repetition_ratio":0.089855075,"word_repetition_ratio":0.0,"special_character_ratio":0.5576369,"punctuation_ratio":0.23004694,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99769044,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-20T23:16:01Z\",\"WARC-Record-ID\":\"<urn:uuid:3848a7a5-4b3a-4469-9534-95334b2036de>\",\"Content-Length\":\"153100\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:42971bf6-2712-4894-acc1-63bf92822a04>\",\"WARC-Concurrent-To\":\"<urn:uuid:a7043ec1-c175-4648-9eb6-112dd84dc87d>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://mathematica.stackexchange.com/questions/129492/exporting-data-file-in-for-loop\",\"WARC-Payload-Digest\":\"sha1:QHFE3KF6HJBIDRFLSP6KP7IPPSWNDFBV\",\"WARC-Block-Digest\":\"sha1:AK5BN3AUSQK3PIBYBKRLSGJDUJXBCWPG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107874340.10_warc_CC-MAIN-20201020221156-20201021011156-00582.warc.gz\"}"}