0
$\begingroup$

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
$\endgroup$
2
$\begingroup$

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 γ.

$\endgroup$
  • $\begingroup$ + , 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. $\endgroup$ – george2079 Oct 24 '16 at 12:20
  • $\begingroup$ @george2079, the classical counterexample goes like x = 3; f[x_] = 2 x + 1; f[18]. 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) :) $\endgroup$ – Marius Ladegård Meyer Oct 24 '16 at 12:47
  • $\begingroup$ 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. $\endgroup$ – AYBRXQD Oct 24 '16 at 15:52
  • $\begingroup$ Aha. See my updated answer. $\endgroup$ – Marius Ladegård Meyer Oct 24 '16 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.