8
$\begingroup$

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.

$\endgroup$

2 Answers 2

9
$\begingroup$
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

$\endgroup$
2
  • $\begingroup$ Thank you so much for the clear answer! $\endgroup$
    – DumbleKo
    Commented Feb 12, 2013 at 22:37
  • $\begingroup$ How can I control my values of the x-axis? Let's say I need am plotting a graph from 1 to 2 and need 1000 equally spaced points, how can I ask mathematical to give me the function value at 1000 equally spaced points? $\endgroup$
    – Prabhat
    Commented Nov 3, 2022 at 21:50
15
$\begingroup$

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]
$\endgroup$
2
  • $\begingroup$ Could you please explain what exactly you have done in the second last line of the code? I also want finer grid points, hence would be beneficial if I can understand what you did to use it in my case. $\endgroup$
    – Prabhat
    Commented Nov 5, 2022 at 13:39
  • 1
    $\begingroup$ hfsol and hssol are InterpolatingFunction objects. These do have some properties (or methds) defined, one of them is "Coordinates" which gives a list of all time values in this case. I do create a matrix containing the time, the value of hfsol and the value of hssol in each row. That is the data that will be exported. By refering to the "Coordinates" property of the InterpolatingFunction object, we use the same time steps that were used to determine the solution. $\endgroup$ Commented Nov 6, 2022 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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