I'm using NDSolve to solve a system of coupled ODEs for a range of different parameters. For most points in the parameter space, the integration breaks down prematurely. However, I would still like to plot the resulting InterpolatingFunctions up to the point where NDSolve broke down.

Of course, that point NDSolve changes based on the chosen parameters so I find myself constantly having to adjust the plot bounds manually.

It seems to me that there should be an easier way. Can I tell Mathematica to simply plot an InterpolatingFunction for all values inside the domain (i.e. without using extrapolation)?

  • 1
    $\begingroup$ One way is given here: mathematica.stackexchange.com/questions/134222/… $\endgroup$ – Michael E2 Jun 9 '17 at 18:33
  • 1
    $\begingroup$ 2nd way: See the last three lines of this answer: mathematica.stackexchange.com/a/65090/4999 $\endgroup$ – Michael E2 Jun 9 '17 at 18:37
  • $\begingroup$ @MichaelE2 Regarding option 1: Can I somehow modify the interpolating function ipf within (List)LinePlot? Say I consider ipf a function of $x$, can I somehow listlineplot ipf[x] e^x? Also, ListLinePlot seems to stop just before the point where NDSolve broke down; the plot it gives looks completely regular. But when I copy the last point from NDSolve's output into a Plot range, I see a singularity. $\endgroup$ – Casimir Jun 12 '17 at 14:32
  • $\begingroup$ @MichaelE2 The same issue of Mathematica excluding singular points happens with the second option. Do you know how I can prevent this? $\endgroup$ – Casimir Jun 12 '17 at 14:46
  • $\begingroup$ I don't think so. ListPlot[if] seems a special case, and it just connected the interpolated points with (straight) lines. For other kinds of plots use Plot[] with if["Domain"] as in the 2nd way (or MikeY's answer). -- "ListLinePlot seems to stop": I'm not sure what you're seeing, but it could be due to automatic plot range adjustments. This would be the case if NDSolve stopped because the solution was going to infinity. Did you try ListLinePlot[if, PlotRange -> All]? $\endgroup$ – Michael E2 Jun 12 '17 at 14:46

You want the domain of your interpolating function. You can get this easily. Creating an InterpolatingFunction[ ] with domain {0,100} ...

hh = h /. NDSolve[{h'[x] == x, h[0] == 0}, h, {x, 0, 100}][[1]];

Then extract it


(* {{0., 100.}} *)

directly use as a plot range:

Plot[hh[x], Evaluate@{x, Sequence @@ First@hh["Domain"]}]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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