# 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

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

• 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? – CA Trevillian Jan 25 at 0:43
• Better for visual layout. The data to be stored is not affected by TableForm, since it is outside the parentheses. – Akku14 Jan 25 at 5:09
• 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. – CA Trevillian Jan 25 at 7:56
• Thanks for the hint @CATrevillian – Akku14 Jan 25 at 8:01

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