0
$\begingroup$

I am beginner in Mathematica. I am trying to save the output data as a text file but I could not save !

m = 9.1*(10)^(-31)
q = 1.6*(10)^(-19)
Er = 14116.83261708455 
Ep = 0
Ez = 4609.405077963418 
Br = 0.2226*(10)^(-4)
Bp = 0
Bz = 0.1825*(10)^(-4)


s = NDSolve[{r''[t] - (q/m)* Er == 0, p''[t] - (q/m)* Ep == 0, z''[t] - (q/m)* Ez == 0, r[0] == 0, r'[0] == 0, p[0] == 0, p'[0] == 0, z[0] == 0.006, z'[0] == 6.9*(10)^(5)}, {r, p, z}, {t, 0, 10}]

data1 = Table[Evaluate[{r[t]} /. s], {t, 0*(10)^(-9), 10*(10)^(-9), 0.1*(10)^(-9)} ]

data2 = Table[Evaluate[{p[t]} /. s], {t, 0*(10)^(-9), 10*(10)^(-9), 0.1*(10)^(-9)} ]

data3 = Table[Evaluate[{z[t]} /. s], {t, 0*(10)^(-9), 10*(10)^(-9), 0.1*(10)^(-9)} ]

data = Table[Prepend[{data1, data2, data3}, t], {t, 0*(10)^(-9), 10*(10)^(-9), 0.1*(10)^(-9)} ] 

TableForm[data,TableHeadings -> {None, Prepend[{"r[t]", "p[t]", "z[t]"}, t]}]

Export["myfile.txt", data, "Table"]
$\endgroup$
4
  • $\begingroup$ It works without problems for me. Could you clarify what is the problem? $\endgroup$ – Kuba Dec 5 '18 at 12:55
  • $\begingroup$ I want all the data in a text file as a column. all the data are mixing in the text file. I am expecting the data for "t" in first column "r[t]" in second and so on $\endgroup$ – ghimire Dec 5 '18 at 13:09
  • $\begingroup$ You have rows which looks like {0., {{{0.}}, {{0.0000124104}}, {{0.0000496416}}, {{0.000111694}},........ I am not surprised export is confused. $\endgroup$ – Kuba Dec 5 '18 at 13:25
  • $\begingroup$ Sorry! I am expecting data in a text file like mathematica.stackexchange.com/questions/159564/… but I don't understand what zhk did? $\endgroup$ – ghimire Dec 5 '18 at 13:29
1
$\begingroup$

If you want to be most exact, store the original function values at the gridpoints (t-values) NDSolve generates.

Generating data0, data1,... with Table, you get intepolated values less exact. With "Coordinates" and "ValuesOnGrid" you get the original grid points. Do (TableForm is only for better layout)

TableForm@(data = Transpose@
   Join[r["Coordinates"] /. s[[1]], 
        Through[List[r, p, z]["ValuesOnGrid"]] /. s[[1]]
])
$\endgroup$
4
  • $\begingroup$ Can you clarify what you mean by “TableForm is only for better layout”? Is it better visual layout of the data to examine it? Or better to be exported into some format? $\endgroup$ – CA Trevillian Jan 25 at 0:43
  • 1
    $\begingroup$ Better for visual layout. The data to be stored is not affected by TableForm, since it is outside the parentheses. $\endgroup$ – Akku14 Jan 25 at 5:09
  • $\begingroup$ it is worth clarifying, imo, that it is only for better visual layout, then. The OP is seemingly new here, and future users may benefit from the clarification as well, even if it is obvious to those familiar with the various *Forms. $\endgroup$ – CA Trevillian Jan 25 at 7:56
  • 1
    $\begingroup$ Thanks for the hint @CATrevillian $\endgroup$ – Akku14 Jan 25 at 8:01
0
$\begingroup$

Yes, I got it! Now I can save all the outputs in a single text file in the multiple columns!

m = 9.1*(10)^(-31)
q = 1.6*(10)^(-19)
Er = 14116.83261708455 
Ep = 0
Ez = 4609.405077963418 
Br = 0.2226*(10)^(-4)
Bp = 0
Bz = 0.1825*(10)^(-4)
s = NDSolve[{r''[t] - (q/m)* Er == 0, p''[t] - (q/m)* Ep == 0, 
   z''[t] - (q/m)* Ez == 0, r[0] == 0, r'[0] == 0, p[0] == 0, 
   p'[0] == 0, z[0] == 0.006, z'[0] == 6.9*(10)^(5)}, {r, p, z}, {t, 
   0, 10}]

data0 = Table[
  Evaluate[t /. s], {t, 0*(10)^(-9), 10*(10)^(-9), 0.1*(10)^(-9)} ]

data1 = Table[
  Evaluate[{r[t]} /. s], {t, 0*(10)^(-9), 10*(10)^(-9), 
   0.1*(10)^(-9)} ]

data2 = Table[
  Evaluate[{p[t]} /. s], {t, 0*(10)^(-9), 10*(10)^(-9), 
   0.1*(10)^(-9)} ]

data3 = Table[
  Evaluate[{z[t]} /. s], {t, 0*(10)^(-9), 10*(10)^(-9), 
   0.1*(10)^(-9)} ]

data = MapThread[{#1, Sequence @@ #2, Sequence @@ #3, 
    Sequence @@ #4} &, {data0, data1, data2, data3}]
Export["withoutB.txt", Flatten /@ data, "Table"]
$\endgroup$
1
  • 1
    $\begingroup$ Congratulations on getting it to work as you hoped. To give you another way of doing it, instead of MapThread with Sequence, you could do this: Transpose[Flatten[#] & /@ {data0, data1, data2, data3}] $\endgroup$ – Mark R Sep 1 '19 at 19:18

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.