# How to solve the following delayed differential equations with periodic delay effect?

I want to solve the following differential equations

$$\partial_{t} f(t) = - a f(t)-a \sum_{n=1}^{N} f(t-n \tau) \cdot \Theta(t-n \tau)$$

I learned the way for solving the equations from the following example System of delay differential equations The following is my code

Module[{a = 1},
sol1 = f[t] /. NDSolve[{f'[t] == -a*f[t], f[t /; t <= 0] == 1}, f, {t, 0, 3}];
sol2 =  NDSolve[{f'[t] == -a*f[t] - a*f[t - 3]*HeavisideTheta[t-3], f[t /; t <= 3] == sol1}, f, {t, 0, 6}];
Plot[Evaluate[f[t] /. sol2], {t, 0, 6}, PlotRange -> All]]


But errors happens. So what's the correct way for solving this kind of delayed differential equations? I think this is a very simple delayed differential equation and I hope Mathematica can do this in a simple and elegant way!

Update: Thanks for user72028's answer, I know how to solve the equations for N=1, However, When I want to solve N=2,erros happens, the following is the code

a = 1;
sol1 = NDSolveValue[{f'[t] == -a f[t], f[t /; t <= 0] == 1},
f[t], {t, 0, 3}];
sol2 = NDSolveValue[{f'[t] == -a f[t] - a f[t - 3] UnitStep[t - 3],
f[t /; t <= 3] == sol1}, f[t], {t, 0, 6}];
sol3 = NDSolveValue[{f'[t] == -a f[t] - a f[t - 3] UnitStep[t - 3] -
a f[t - 6] UnitStep[t - 6], f[t /; t <= 6] == sol2},
f[t], {t, 0, 9}];
Plot[sol3, {t, 0, 9}]


The errors are

Solve::ratnz: Solve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result.
NDSolveValue::idelay: Initial history needs to be specified for all variables for delay-differential equations.
NDSolveValue::dsvar: 0.00018385714285714286 cannot be used as a variable.
NDSolveValue::dsvar: 0.18385732653061226 cannot be used as a variable.
General::stop: Further output of NDSolveValue::dsvar will be suppressed during this calculation.
InterpolatingFunction::dmval: Input value {6.06141} lies outside the range of data in the interpolating function. Extrapolation will be used.
InterpolatingFunction::dmval: Input value {6.24508} lies outside the range of data in the interpolating function. Extrapolation will be used.
InterpolatingFunction::dmval: Input value {6.42876} lies outside the range of data in the interpolating function. Extrapolation will be used.
General::stop: Further output of InterpolatingFunction::dmval will be suppressed during this calculation.


So how can I resolve this problem?

• "Solve::ratnz" is informational, not an error. Oct 5 '20 at 19:02
• Oh, it's my fault. I have updated all of the error informations on the questions. Oct 6 '20 at 2:41

The solution for any number of steps (for instance, 5) is

With[{a = 1, τ = 3, m = 4}, tm = (m + 1) τ;
s = NDSolveValue[{f'[t] == -a*Sum[f[t - n τ] UnitStep[t - n τ], {n, 0, m}],
f[t /; t <= 0] == 1}, f, {t, 0, tm}];
Plot[s[t], {t, 0, tm}, ImageSize -> Large, AxesLabel -> {t, f},
LabelStyle -> {15, Bold, Black}]]


This particular problem can be solved symbolically be replacing NDSolveValue by DSolveValue, but I assume that the OP has in mind a more complicated ODE in practice.

Incidentally, f[0] == 1 should be an adequate initial condition, because solving the ODE as written in this answer does not require knowledge of f[t] for t < 0. However, NDSolve error checking does not realize this and complains before solving the ODE without difficulty.

1. Your sol1 is a list containing the desired function of t. Either extract the first part of that list, or use NDSolveValue with f[t] in the second argument to return said function of t directly.

2. The "non-numerical value for a derivative at t == 3" message arises from HeavisideTheta[0] not having a numerical value. Use UnitStep instead.

Altogether:

a = 1;
sol1 = NDSolveValue[{f'[t] == -a f[t], f[t /; t <= 0] == 1}, f[t], {t, 0, 3}];
sol2 = NDSolveValue[{f'[t] == -a f[t] - a f[t - 3] UnitStep[t - 3], f[t /; t <= 3] == sol1}, f[t], {t, 0, 6}];
Plot[sol2, {t, 0, 6}]


• Thank you so much for answeing the question! This really helps me a lot. Your answer works for me. However, When I continue adding the delayed terms. There are erros, I have updated the errors in the questions. Do you have any idea on this error message? Oct 5 '20 at 12:42