1

In a simple example I switch the ode depending on two events.

V = NDSolveValue[{  v'[t] == (1 - flag[t]) 3.29 - flag[t] 10.8,v[0] == 10, flag[0] == 0
, WhenEvent[v[t] ==    25, flag[t] -> 1]
, WhenEvent[v[t] ==  0 , "StopIntegration"]
} , v, {t, 0, 10 }, DiscreteVariables -> flag[t] ]    ;

NDSolve seems to work quite good

Plot[V[t], {t, 0, V["Domain"][[1, 2]]}, GridLines -> {None, {10, 25}},PlotRange -> {0,30}]

enter image description here

and returns the plotted (Hermite-) interpolation.

Looking inside V[t] I get the interpolationdata

tv = Transpose[{V["Coordinates"][[1]], V["ValuesOnGrid"]}]

To my surprise there is a duplicate point inside the interpolation data tv

Cases[tv, {___, x_, x_, ___} :> x, All]
(*{{4.55927, 25.}}*)

which comes from the first event detection. That means NDSolve returns an interpolation object, working quite well, with duplicate interpolation points???

My questions:

  • V["InterpolationMethod"](*Hemite*) but Interpolation[tv, Method ->"Hermite"] fails. What kind of interpolation is used by NDSolve ?

  • How could I avoid duplicate points using 'NDSolve& WhenEvent`?

Thanks!

Ulrich Neumann
  • 53,729
  • 2
  • 23
  • 55
  • While waiting for an InterpolatingFunction guru like @MichaelE2 to show up, I'll just suggest that the duplicate point has the same value, but different derivatives since it is not continuously differentiable. E.g. V[[4,3]] includes the values 25., 3.29, 25., -10.8,, where I suspect the 25's are the values and the {3.29, -10.8} are the derivative. My own question is rather "can we create such an InterpolatingFunction with duplicate points using Interpolation?" – Chris K Nov 25 '20 at 18:09
  • @ChrisK Very interesting idea, thanks! I checked Map[{#, V[#], V'[#]} &, V["Coordinates"][[1]] ] which gives identical derivative for the duplicate point. Still waiting for a guru ;-) – Ulrich Neumann Nov 25 '20 at 18:15
  • Just curious, but why do you want to avoid it anyhow? It seems harmless under the cover there. – Chris K Nov 25 '20 at 23:47
  • (V')["ValuesOnGrid"] is a better way to extract those derivatives. – Chris K Nov 26 '20 at 00:37
  • 2
    The answer may be found here: https://mathematica.stackexchange.com/a/98349/4999 -- An event introduces a singularity or even a discontinuity (typically), and the only way deal with it is to splice together two interpolations. WRI handled by duplicating the point joining them. – Michael E2 Nov 26 '20 at 01:06
  • 1
    @ChrisK I don't think it's possible to construct an InterpolatingFunction with a duplicate point using Interpolation. It would be helpful if WRI exposed all the functionality of InterpolatingFunction through `Interpolation. – Michael E2 Nov 26 '20 at 01:42
  • @MichaelE2 Yes, we need InterpolatingFunctionSurgery to go with InterpolatingFunctionAnatomy. – Chris K Nov 26 '20 at 02:02
  • @ChrisK Thank you for your helpful comments. In my application I need the interpolation grid to combine several interpolation functions. – Ulrich Neumann Nov 26 '20 at 07:13
  • @MichealE2 Thank you your extremely useful link. NDSolve seems to use a monotonic Polynom interpolation(pchip). In my application, which worked in several simulations without problems, I try to combine simple interpolation function and NDSolve result. Perhaps there exists I direct way to do that... – Ulrich Neumann Nov 26 '20 at 07:59

0 Answers0