# How to save the output data of second order differential equation in Mathematica?

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

• It works without problems for me. Could you clarify what is the problem? – Kuba Dec 5 '18 at 12:55
• 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 – ghimire Dec 5 '18 at 13:09
• You have rows which looks like {0., {{{0.}}, {{0.0000124104}}, {{0.0000496416}}, {{0.000111694}},........ I am not surprised export is confused. – Kuba Dec 5 '18 at 13:25
• Sorry! I am expecting data in a text file like mathematica.stackexchange.com/questions/159564/… but I don't understand what zhk did? – ghimire Dec 5 '18 at 13:29

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

• 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}] – Mark R Sep 1 '19 at 19:18