0
$\begingroup$

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

$\endgroup$
1
$\begingroup$

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];

Mathematica graphics

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

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

enter image description here

One way to read it back is

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

Mathematica graphics

  (databack - data) // Chop

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ I have V12.0, and data = W[#] & /@ Range[-2, 2] gives me further output of NInt will be supressed during this calc $\endgroup$
    – newstudent
    May 22 '20 at 9:09
  • $\begingroup$ @newstudent it works for me on V 12.0 also, all on windows 10. Try with clean kernel. Here is screen shot !Mathematica graphics $\endgroup$
    – Nasser
    May 22 '20 at 9:12
  • $\begingroup$ @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. $\endgroup$
    – Nasser
    May 22 '20 at 9:24

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.