How to numerically solve a differential equation with a dirac delta function ?
Show older comments
The differential equation that I want to solve is
Upon using ode45 and the dirac function, the dirac function doesn't seem to have any effect (which makes sense because x never reaches 1 in a numerical solution)
Any ideas on how to solve this numerically?
6 Comments
Alan Stevens
on 30 Jun 2020
What are your initial conditions and the time over which you want to solve?
Mohit Kumar
on 30 Jun 2020
Alan Stevens
on 30 Jun 2020
You could use a coarse approximation to the dirac delta function to see it give a kick to dx/dt. Something like:
d = 0;
if abs(x - 1) < small value
d = (v - abs(v))/2;
end
dXdt = [v; -v - x + d];
However, the "small value" probably needs to be quite large (say 10^-1 or 10^-2 ) to see anything!
Any smaller and ode45 is likely to jump across x = 1 without invoking the condition.
Mohit Kumar
on 30 Jun 2020
Mohit Kumar
on 1 Jul 2020
Alan Stevens
on 1 Jul 2020
Edited: Alan Stevens
on 1 Jul 2020
Hmm. I assumed you just wanted the dxdt - |dxdt| to kick in when x = 1 (The area under the delta function being unity). I'm not sure what you are after if you truly want it to go to infnity (what do you expect the ode function to do with that?). Indeed, if infinity is what you want why bother multiplying it by anything else?
Accepted Answer
More Answers (1)
Carlos M. Velez S.
on 24 Jul 2025
If you want to apply the Dirac delta function in simulation to continuous-time systems, the following code is enough:
function y = delta_dirac(u)
[n,m] = size(u);
if max(n,m) ==1
dt = 1e-6; % Define a small time increment for the delta function
else
dt = u(2) - u(1);
end
y = zeros(n,m);
for i=1:max(m,n)
if u(i) == 0
y(i) = 1/dt;
else
y(i) = 0;
end
end
1 Comment
Walter Roberson
on 24 Jul 2025
ode45() is not a continuous time system, so this function is irrelevant to the situation.
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



