Exporting interpolating functions to excel
The interpolating functions that NDSolve
returns contain an irregular grid that reflects which points were used to calculate the solution. Not always, but often this grid is a better choice than a regular grid as you would generate with Table
when exporting. Here is how you could export the data as NDSolve
generated it:
a = 10^-2;
eq1 = {hf'[t] == -a*(hf[t] - hs[t]), hs'[t] == a*(hf[t] - hs[t]),
hf[0] == 20, hs[0] == 0};
sol1 = NDSolve[eq1, {hf, hs}, {t, 0, 100}]
hfsol = hf /. First[sol1]
hssol = hs /. First[sol1]
data = {#, hfsol[#], hssol[#]} & /@ First[hfsol@"Coordinates"]
Export[FileNameJoin[{$UserDocumentsDirectory, "sol.xlsx"}], data]
a = 10^-2;
eq1 = {hf'[t] == -a*(hf[t] - hs[t]), hs'[t] == a*(hf[t] - hs[t]), hf[0] == 20, hs[0] == 0};
sol1 = NDSolve[eq1, {hf, hs}, {t, 0, 100}]
Now:
Plot[{hf[t], hs[t]} /. sol1, {t, 0, 100}]
Export["c:\\test.xls", Table[Flatten[{t, hf[t], hs[t]} /. sol1], {t, 0, 100}]]