6

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)?

Janosh
  • 1,281
  • 8
  • 30
  • 1
    One way is given here: https://mathematica.stackexchange.com/questions/134222/easy-way-to-plot-ode-solutions-from-ndsolve – Michael E2 Jun 09 '17 at 18:33
  • 1
    2nd way: See the last three lines of this answer: https://mathematica.stackexchange.com/a/65090/4999 – Michael E2 Jun 09 '17 at 18:37
  • @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. – Janosh Jun 12 '17 at 14:32
  • @MichaelE2 The same issue of Mathematica excluding singular points happens with the second option. Do you know how I can prevent this? – Janosh Jun 12 '17 at 14:46
  • 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]? – Michael E2 Jun 12 '17 at 14:46
  • @MichaelE2 I did try PlotRange -> All; no change. What I see when copying the last point in the domain: Imagine a plot dropping off continuously and suddenly exploding to infinity where NDSolve broke down. When I use ListLinePlot without explicit domain I get the same plot except that it stops just before the singularity. However, when examining the output I'd like to be able to see such features. – Janosh Jun 12 '17 at 14:51
  • @MichaelE2 I also tried Exclusions -> None when using Plot and if["domain"] but that didn't help either. – Janosh Jun 12 '17 at 14:53
  • Do you have an example I can see? This does not seem to have that problem: foo = NDSolve[{y'[x] == y[x]^2, y[0] == 1}, y, {x, 0, Infinity}]; ListLinePlot[y /. foo]. – Michael E2 Jun 12 '17 at 14:59
  • It's also possible that the value of the solution became complex. (It's still just a shot in the dark.) – Michael E2 Jun 12 '17 at 15:24

1 Answers1

7

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

hh["Domain"]

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

directly use as a plot range:

Plot[hh[x], Evaluate@{x, Sequence @@ First@hh["Domain"]}]
george2079
  • 38,913
  • 1
  • 43
  • 110
MikeY
  • 7,153
  • 18
  • 27