Message Boards Message Boards

NDSolve with DiracDelta gives unexpected answer

ODE with a DiracDelta at the origin fails using NDSolve (solution steps down at x=0).

sol = NDSolveValue[{y'[x] == DiracDelta[x] , y[0.5] == 0.}, 
  y, {x, -1, 1}]   
Plot[sol[x], {x, -1, 1}]

enter image description here solutions steps down at x=0

DSolve provides the right answer (solution steps up at x=0).

sol = DSolveValue[{y'[x] == DiracDelta[x] , y[0.5] == 0.}, 
  y, {x, -1, 1}]   
Plot[sol[x], {x, -1, 1}]

solution steps up at x=0

How can I reliably use NDSolve with DiracDelta functions? (the problem I am analyzing is much more complex, but I boiled down the issue I am running into to the above behavior).

POSTED BY: Eric Michielssen
2 Replies

It reminds me of this behavior:

sol = NDSolveValue[{y'[x] == 0, y[0.5] == 0., 
   WhenEvent[x == 0, y[x] -> y[x] + 1]}, y, {x, -1, 1}]
Plot[sol[x], {x, -1, 1}]

enter image description here

The problem, to which in the case of WhenEvent[] I have grown jadedly accustomed, is that the time integration direction is not accounted for in the internal event structure. So whether the integration passes the event with time moving forward or moving backward, +1 is added to the solution. NDSolve starts the integration at the initial condition. It integrates backward to the beginning of the interval and forward to the end of the interval. This means one might have to rewrite the event actions if one changes the initial condition to start on the other side of an event.

The simplest fix would be to put a minus sign in front DiracDelta[x]. Another would be to multiply by the integration signature Sign[x - x0]:

NDSolveValue[{y'[x] == Sign[x - 0.5] DiracDelta[-x], 
  y[0.5] == 0.}, y, {x, -1, 1}]
POSTED BY: Michael Rogers

See here read the comments.

DiracDelta function is only good in solving Ode or PDE analytically, not numeric.

Workaround with approximation :

   sol = With[{e = 1/10000}, 
     NDSolveValue[{y'[x] == 1/Pi*e/(x^2 + e^2), y[0.5] == 0.}, 
      y, {x, -1, 1}]]
   Plot[sol[x], {x, -1, 1}]
POSTED BY: Mariusz Iwaniuk
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract