# Export data from NIntegrate and Piecewise

I use the following to calculate my Integral.

Clear["Global*"]

G[ω_, τC_] := Exp[τC Sqrt[1 + ω^2] (1 - ω^2)]

position[x_] := Module[{xx = SetPrecision[x, 20]},
-2 (mSlope/(α π)) Exp[α xx] NIntegrate[
G[ω, τC] Cos[
2 τC ω Sqrt[1 + ω^2] -
2 ArcTan[ω] + α xx ω]/(1 + ω^2), {\
ω, 0, ∞}, WorkingPrecision -> 20] + 2 mSlope xx]

positionLeft[x_] := Module[{xx = SetPrecision[x, 20]},
-2 (mSlope/(α π)) Exp[α xx] NIntegrate[
G[ω, τC] Cos[
2 τC ω Sqrt[1 + ω^2] -
2 ArcTan[ω] + α xx ω]/(1 + ω^2), \
{ω, 0, ∞}, WorkingPrecision -> 20]]

W[x_] := Piecewise[{{position[x], x >= 0}, {positionLeft[x], x < 0}}]

τC = 1*^-6; α = 10^-3; mSlope = 1/5;


I Would like to save W[x_] for every x from -250 to 250. I have tried Export["result.txt",W[k_],{k,-200,200}] but it does not save. How do I save the data such that first column is x and second is W[x_]. Thanks

I Would like to save W[x_] for every x from -250 to 250

See if this works for you. V 12.1

 data = W[#] & /@ Range[-2, 2];


 SetDirectory[NotebookDirectory[]];
Export["result.txt", data, "List"]


The result.txt file in same folder as the notebook looks like

One way to read it back is

  databack = Import["result.txt", "List"]


  (databack - data) // Chop


• I have V12.0, and data = W[#] & /@ Range[-2, 2] gives me further output of NInt will be supressed during this calc Commented May 22, 2020 at 9:09
• @newstudent it works for me on V 12.0 also, all on windows 10. Try with clean kernel. Here is screen shot !Mathematica graphics Commented May 22, 2020 at 9:12
• @newstudent sorry, I do not understand. This is your code I run. You said range -250..250. So just change Range[-2, 2] in the example I posted to Range[-250, 250]`. I do not know what values they will produce. Commented May 22, 2020 at 9:24