I figured out part of the problem, and it no longer seems random. My first answer-comment was premature, but I was confused and thought I had gotten as far as I could. The problem occurs as described below. I don't know why it messes up the numerical function's "Jacobian".
Inside the InterpolatingFunction solution, there is an Experimental`NumericalFunction that encodes the history of the DDE; let's call it nf[]. The problem occurs when nf["Jacobian"[...]] is evaluated with whatever numeric argument for ... while t has been given a value.
Once this happens, then the Jacobian's value for x'[...] is fixed. You can rerun NDSolve to create a new solution with a new numerical function.
On the other hand, if the Jacobian is first evaluated when t has no value, then it will evaluate correctly from that point on, even if t is given a value afterwards.
Note that the numerical function is used to evaluate the derivative x'[t] only for values of t in the initial history, that is, for t < 0 in the OP's problem. For higher order DDEs, the Jacobian is not evaluated for derivatives of order less than the order of the DDE. This problem arises in higher order DDEs only when evaluating derivatives of the order of the DDE or higher.
Evidence for the above:
We start over with a new NDSolve every time to get a new numerical function in the solution.
Clear[x, t];
sol = NDSolve[{x'[t] + x[t - 1] == 0, x[t /; t <= 0] == t^2},
x, {t, -1, 5}];
t = -0.44;
x'[-0.6] /. First@sol (* t has a value *)
x'[{-0.5, -0.33}] /. First@sol (* all derivatives = x'[-0.44] *)
t =.
(*
-0.88
{-0.88, -0.88}
*)
Clear[x, t];
sol = NDSolve[{x'[t] + x[t - 1] == 0, x[t /; t <= 0] == t^2},
x, {t, -1, 5}];
x'[-0.6] /. First@sol; (* t has no value )
t = -0.44;
x'[-0.6] /. First@sol ( the value of t is irrelevant )
x'[{-0.5, -0.33}] /. First@sol ( & derivatives are correct )
t =.
(
-1.2
{-1., -0.66}
*)
Clear[x, t];
sol = NDSolve[{x'[t] + x[t - 1] == 0, x[t /; t <= 0] == t^2},
x, {t, -1, 5}];
Block[{t = 0.4}, (* positive t but... )
x'[-0.6] /. First@sol] ( evaluate x' at neg. number )
x'[{-0.5, -0.33}] /. First@sol
(
0.8
{0.8, 0.8}
*)
Another test: Using a different variable than t in Plot avoids the issue (unless the x' has been evaluated at a negative number when t had a value already, of course):
Clear[x, t];
sol = NDSolve[{x'[t] + x[t - 1] == 0, x[t /; t <= 0] == t^2},
x, {t, -1, 5}];
Plot[Evaluate[{x[tt], x'[tt]} /. First@sol], {tt, -1, 2},
PlotRange -> All]
I'm deleting most of the original answer, because the above is more accurate and clearer. However, there is a workaround I had proposed that I cannot fully explain in terms of what was said above, but I have a guess. The following produces a solution that does not have the bug:
sol2 = NDSolve[
{x'[t] + $MinMachineNumber*x'[t - 1] + x[t - 1] == 0,
x[t /; t <= 0] == t^2}, x, {t, -1, 5}]
If the first thing you do is plot it in terms of t, you get the same graph as above (not shown below):
Plot[Evaluate[{x[t], x'[t]} /. First@sol2], {t, -1, 2},
PlotRange -> All]
If we run one of the tests above, we see we get different values from the two numerical functions even though according to SameQ they are identical. Something must happen internally, which I cannot discover, that makes the difference. Perhaps the Jacobian of the numerical function is evaluated to determine x[t - 1] during the integration of the DDE. It seems plausible and an internal Trace shows that a numerical function like the one in the solution occurs (in NDSolve`DDEFunction at each integration step). However, if it's evaluated, Trace does not reveal it. (Trace cannot show all steps.)
Clear[x, t];
sol = NDSolve[{x'[t] + x[t - 1] == 0, x[t /; t <= 0] == t^2},
x, {t, -1, 5}];
Block[{t = -0.4}, x'[t] /. First@sol];
x'[{-0.5, -0.33}] /. First@sol
ifn = x /. First@sol;
nf = ifn[[4, 1, 2]];
nf["Jacobian"[#]] & /@ {-0.5, -0.33}
(*
{-0.8, -0.8}
{{-0.8}, {-0.8}}
*)
Clear[x, t];
sol2 = NDSolve[{x'[t] + $MinMachineNumberx'[t - 1] + x[t - 1] == 0,
x[t /; t <= 0] == t^2}, x, {t, -1, 5}];
Block[{t = -0.4}, x'[t] /. First@sol2];
x'[{-0.5, -0.33}] /. First@sol2
ifn2 = x /. First@sol2;
nf2 = ifn2[[4, 1, 2]];
nf2["Jacobian"[#]] & /@ {-0.5, -0.33}
(
{-1., -0.66}
{{-1.}, {-0.66}}
*)
nf === nf2
(*
True
*)
Experimental`NumericalFunction. The function is defined to bet^2, but there seems to be a bug with its derivative. See this:ifn = x /. First@sol; nf = ifn[[4, 1, 2]]; nf["Jacobian"[-3]]. Whatever number is used instead of-3, you get-2.for the derivative ("Jacobian"). Please report it to WRI. – Michael E2 Jul 12 '22 at 04:22t<=0asx[t /; t <= 0] == t^2then we can integrate delay equation att>0only. In your example you try to integrate at{t, -1, 5}. – Alex Trounev Jul 12 '22 at 05:05sol = NDSolve[{x'[t] + x[t - 1] == 0, x[t /; t <= 0] == Sin[t]}, x, {t, -1, 5}]. – user64494 Jul 12 '22 at 11:53x''[t]instead ofx'[t]):sol = NDSolve[{x''[t] + x[t - 1] == 0, x[t /; t <= 0] == t^2}, x, {t, -1, 5}](first example here). It, too, produces a numerical function with a buggynf["Jacobian"[-1]]. I don't think specifying the domain to include the history meansNDSolvewill integrate the DE. It simply incorporates the initial history and integrates only fromt == 0forward. – Michael E2 Jul 12 '22 at 15:10x[t /; t <= 0] == t^2and{t, -1, 5}solved withNDSolve, but that is incorrect. In DDE theory we can't solve equation att<=0since solution is defined att<=0as an initial condition. – Alex Trounev Jul 12 '22 at 16:32NDSolvedoes not solve the DDE fort <= 0even if you specify{t, -1, 5}. While it is incorrect to solve the DDE fort <= 0,NDSolveis not doing the incorrect thing. So there is nothing incorrect with specifying{t, -1, 5}. It simply uses the initial history fort <= 0, which is correct, yes? – Michael E2 Jul 12 '22 at 16:45