Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a question about exporting an InterpolatingFunction to excel. I checked the forum, but I still couldn't completely understand how to solve my problem. Could someone help me with this problem?

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

The I got solution for hf[t] & hs[t] :

{{hf -> InterpolatingFunction[{{0.,100.}},<>],
  hs -> InterpolatingFunction[{{0.,100.}},<>]}}

I'm wondering how I can export hf[t] & hs[t] values to excel as t ranges from 0 to 100.

share|improve this question

2 Answers

up vote 4 down vote accepted
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}]

Mathematica graphics

Export["c:\\test.xls", Table[Flatten[{t, hf[t], hs[t]} /. sol1], {t, 0, 100}]]

Mathematica graphics

share|improve this answer
Thank you so much for the clear answer! – DumbleKo Feb 12 at 22:37

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]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.