# InterpolatingFunction gives different result when plot [closed]

I solve a series of ODE equations by NDSolve({t,0,200} is the region), it gives results in the form of InterpolatingFunction[sth][t]. When I try to plot the function in follow way, it gives different picture.

Plot[InterpolatingFunction[sth][t], {t, 0, 200}, PlotRange -> {{0, 0.0001}, {10^-7, -10^-7}}, PlotLabel -> "{t, 0, 200}"]
Plot[InterpolatingFunction[sth][t], {t, 0, 1}, PlotRange -> {{0, 0.0001}, {10^-7, -10^-7}}, PlotLabel -> "{t, 0, 1}"]


there is not any error when I plot.

I wonder why this happened. I don't know how to add my InterpolatingFunction[sth][t] to the question, and my NDSolve is very complex, so I don't show.

## closed as off-topic by Michael E2, m_goldberg, bbgodfrey, LCarvalho, José Antonio Díaz NavasJan 8 at 21:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Michael E2, m_goldberg, bbgodfrey, LCarvalho, José Antonio Díaz Navas
If this question can be reworded to fit the rules in the help center, please edit the question.

• It's hard to tell what your problem is. Because of your PlotRange function, you are only plotting between 0 and 0.0001, so the rest of the solution to the ODE (between 0 and 200) is not displayed. – bill s Dec 17 '18 at 4:04
• @bills In my opinion, the InterpolatingFunction has all information of the equation, why the plot picture changes when the "real plotrange" changes, I mean the function PlotRange changes only the range of axis and have no influence of Plot. – 袁子奕 Dec 17 '18 at 5:24
• I think maybe it caused by the different of PlotPints and MaxRecursion, I try to change these two option but have no idea how to get the same picture. – 袁子奕 Dec 17 '18 at 5:35
• Try sth["Domain"] to get the time range of the solution. Outside this range Plot extrapolates the function. And the plot-function creates starting values in the given time range! – Ulrich Neumann Dec 17 '18 at 8:26
• @ 袁子奕 NDSolve domain, for example {t,0,200}, is set before evaluation . Excecuting NDSolve it might happen that the simulation is terminated because of numerical reasons. sth["Domain"] helps you to get the used simulation time range which might be considerably smaller than {t,0,200}! – Ulrich Neumann Dec 17 '18 at 9:31

I believe that the reason behind the discrepancy is that when Plot decides at which points the target expression is to be evaluated, it assumes that the "typical scale" of the independent variable is of the same order as the range specified as the second argument of Plot. PlotRange doesn't matter here, it simply determines what area to include in the picture. So, when you ask to plot an expression in the range {t, 0, 200}, Plot doesn't try to be precise at a scale of 0.0001. Neither does it try to be precise at such scale, when you ask it to plot the expression at the {t, 0, 1} range. So, basically, I would suggest that both pictures are nothing but numerical rubbish.

Consider the following to examples:

Last[Reap[
Plot[Sin[t], {t, 0., 200.}, EvaluationMonitor :> Sow[{t, Sin[t]}],
PlotRange -> {{0., 0.0001}, {-1., 1.}}
]]][[1, 1 ;; 10]]
({{4.08163*10^-6,4.08163*10^-6},{3.92573,-0.706216},{8.18167,0.946788},{12.1556,-0.399337},{16.0515,-0.336847},{20.2777,0.989842},{24.2218,-0.790066},{28.4962,-0.220016},{32.6926,0.957044},{36.6069,-0.887643}}*)

Last[Reap[
Plot[Sin[t], {t, 0., 1.}, EvaluationMonitor :> Sow[{t, Sin[t]}],
PlotRange -> {{0., 0.0001}, {-1., 1.}}
]]][[1, 1 ;; 10]]
(*{{2.04082*10^-8,2.04082*10^-8},{0.0196287,0.0196274},{0.0409084,0.0408969},{0.0607779,0.0607405},{0.0802576,0.0801715},{0.101388,0.101215},{0.121109,0.120813},{0.142481,0.141999},{0.163463,0.162736},{0.183035,0.182014}}*)


They show that when Plot is asked to plot an expression at a smaller scale, it uses a different grid to plot the expression than when it was asked to plot this expression at a larger scale, but still this grid is too dense for your PlotRange.

P.S.

If you want to examine your solution at such a small time scale relative to overall time of evolution, you should ensure that the time step that was used by NDSolve is appropriately small.

EDIT

Regarding your comment about making the pictures normal. That is how Plot builds the pictures:

1. First, it chooses the initial grid for the plot. The number of nodes in this grid is determined by internal Plot algorithms but will not be less that PlotPoints option value. Plot will add extra nodes if its internal algorithms return less nodes than specified in PlotPoints option value. What is important here is that Plot algorithms determine the number of grid nodes using your target expression and the second argument of Plot. PlotRange doesn't matter, in fact, this options is inherited form Graphics.

2. Then Plot examines how fast the target expression changes from node to node. If this change is sufficiently large, Plot will add extra nodes where necessary, but only if MaxRecursion is greater than zero and it will do extra sampling not more times than MaxRecursion. What is important here is that Plot decides whether it needs extra grid points using your target expression and the previous grid.

So, conclusions:

1. If you set PlotRange much smaller then the range specified as the second argument, then MaxRecursion is unlikely to help because internal Plot algorithms might just decide that you don't need to see details at such scale. MaxRecusrion -> n says to Plot: "You can do extra sampling up to n times if you want". But maybe in combination with PlotPoints, when the initial grid is already very detailed, it will work.

An example:

 Last[Reap[
Plot[Sin[t], {t, 0., 1.}, EvaluationMonitor :> Sow[{t, Sin[t]}],
PlotRange -> {{0., 0.0001}, {-1., 1.}}, MaxRecursion -> 1]]][[1, 1 ;; 10]] ===
Last[Reap[
Plot[Sin[t], {t, 0., 1.}, EvaluationMonitor :> Sow[{t, Sin[t]}],
PlotRange -> {{0., 0.0001}, {-1., 1.}}, MaxRecursion -> 15]]][[1,
1 ;; 10]]
(*True*)

1. Setting PlotPoints to some appropriately large number will force Plot to use a very detailed grid, but note that Plot will use this detailed grid everywhere, not only inside the PlotRange.

An example:

Last[Reap[
Plot[Sin[t], {t, 0., 1.}, EvaluationMonitor :> Sow[{t, Sin[t]}],
PlotRange -> {{0., 0.0001}, {-1., 1.}}, PlotPoints -> 10^5]]][[1,
1 ;; 10]]
(*{{1.00001*10^-11,1.00001*10^-11},{9.61814*10^-6,9.61814*10^-6},{0.0000200453,0.0000200453},{0.0000297815,0.0000297815},{0.0000393266,0.0000393266},{0.0000496808,0.0000496808},{0.0000593441,0.0000593441},{0.0000698163,0.0000698163},{0.0000800976,0.0000800976},{0.0000896878,0.0000896878}}}*)


Honestly, why not to just specify the second argument to Plot appropriately? :)

• Yes, it shows why they have different picture, for "P.S.", I'm sure my NDSolve is appropriately small:). I look at the help manual and find that PlotPoints and MaxRecursion will have influence on grid, so do you have any idea of how to change theses two options to make the picture same? – 袁子奕 Dec 18 '18 at 3:15
• I have added an edit to address your comment. – Anton.Sakovich Dec 18 '18 at 8:40
• BTW, because InterpolatingFunction seems to be only indirectly connected to the issue, maybe it is worth changing the title of the question to something that points more to Plot than to InterpolatingFunction. – Anton.Sakovich Dec 18 '18 at 8:46