2

I can see the correct result of my NDSolve when I plot them with large PlotPoints.

Plot[sol[[1, 14]], {t, 19, 20}, PlotPoints -> 2500, PlotRange -> All]
Plot[sol[[1, 14]], {t, 19, 20}, PlotRange -> All]

enter image description here

I want to export the result as .mat or .xlsx but by using Table even with a very very small step I cannot get the correct results.

dt = 1/15000;
Export["Asun.mat", Table[sol[[1, 14]], {t, 19, 20, dt}]]

enter image description here

Karsten7
  • 27,448
  • 5
  • 73
  • 134
Ali
  • 109
  • 6

3 Answers3

8

If s is produced by something like

s = NDSolveValue[sys, x, {t, 0, tf}]

producing

(*InterpolatingFunction[{{0., 20.}}, <>] *)

the raw data used by InterpolatingFunction can be extracted by

{s["Grid"], s["ValuesOnGrid"]}

and exported. (See 28337 for more data that can be extracted.)

bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
3

Exporting the points used by Plot

Export["Asun.mat", 
 InputForm[
   Plot[sol[[1, 14]], {t, 19, 20}, PlotPoints -> 2500, 
    PlotRange -> All]][[1, 1, 1, 3, 2, 1]]]

should export all the points shown in your first plot.


Finding a suitable fixed interval

Plot is using an adaptive algorithm to find determine the sample points.
You should check the Details and Options section of the documentation about this. Here I just want to point out that one should also try to increase the settings for MaxRecursion and that the resulting sampling is not equidistant, as the interval length is reduced around rapid oscillations.

If you want to get an Export with fixed interval while making sure no details get lost, you can set you dt to the smallest interval used by Plot

dt = Min @ Differences[
      FullForm[Plot[sol[[1, 14]], {t, 19, 20}, PlotPoints -> 2500, 
          PlotRange -> All]][[1, 1, 1, 3, 2, 1]][[All, 1]]]

and then use your

Export["Asun.mat", Table[sol[[1, 14]], {t, 19, 20, dt}]]
Karsten7
  • 27,448
  • 5
  • 73
  • 134
  • Thank you. It is working. is there any way to do it with a fixed interval? – Ali Jun 30 '15 at 12:31
  • @Ali I extended my answer in order to address your fixed interval question. You should note, that for a very small interval using a rational number is advised. In such a situation you can get a rational interval with dt = Rationalize[dt, 0]. – Karsten7 Jun 30 '15 at 15:13
1
Needs["DifferentialEquations`InterpolatingFunctionAnatomy`"];
iii = sol[[1, 14, 0]];
coords = InterpolatingFunctionCoordinates[iii];
valus = InterpolatingFunctionValuesOnGrid[iii];
Ali
  • 109
  • 6