# What is the scope of the bug where Piecewise breaks DSolve?

Bug introduced after 9.0.1, persisting through 12.3.1. Fixed in 13.0

I have identified a bug in DSolve when differential equation includes a Piecewise statement with a discontinuity at the boundary.

I want help figuring out how to set the correct border condition for this differential equation that includes a Piecewise statement with a discontinuity at the boundary.

My tests are in WolframCloud

\$Version


12.3.0 for Linux x86 (64-bit) (May 10, 2021)

The differential system is simple

eqns={
r[0]==0,
D[r[t],t]== Piecewise[{{a, 0<=t<c}},0] - b r[t]
}


Mathematica does find a solution using DSolve, but it contains some intriguing results it's the wrong solution.

sol=Assuming[
And[a>0,b>0,c>0],
FullSimplify[
r[t]/.First@DSolve[eqns,r,t]
]
]


nsol=ReplaceAll[sol, {a->1,b->2,c->3}];
Plot[nsol, {t,-1,10}]


First, I would have expected the solution to be a zero constant for $$t<0$$, here is that I assume I'm not defining the border condition correctly.

Second, there is this term UnitStep[1-c], I don't see how c==1 is a special condition. I expected this to be UnitStep[t].

How am I supposed to set the border condition for the solution to be zero for $$t<0$$?

I'm expecting a solution like this

Piecewise[
{
{(1-Exp[-t b]) a/b, 0<t<c},
{(1-Exp[-c b]) Exp[-(t-c) b] a/b, c<t}
}
,0]


Mathematica is giving an incorrect answer and this has been reported to Wolfram Support, acknowledged as a bug, and the developers have been informed.

Questions: What is the scope of this problem? Do other discontinuous functions break DSolve too? Can this problem be reproduced in other versions and platforms?

• Looks like another bug of DSolve. NDSolve gives the same result as 2nd graphic. Commented Sep 15, 2021 at 16:40
• I have edited the question, as after communication with Wolfram Support it's clear this is a bug and not a problem on how do I define the border conditions. Commented Sep 23, 2021 at 12:10
• Result in v9.0.1 looks correct: Piecewise[{{(a*(1 - Cosh[b] + Sinh[b]))/b, t == 1 && c > t}, {(a*(-1 + E^(b*c)))/(E^(b*t)*b), c <= t && ((t < 1 && c <= 1) || t > 1)}, {(a - a/E^(b*t))/b, (Inequality[0, LessEqual, t, Less, 1] && (c > 1 || c > t)) || (c > t && t > 1)}, {0, t < 0}}, (a*(-1 + E^(b*c)))/(E^b*b)] i.sstatic.net/e8WaV.png Commented Sep 23, 2021 at 12:28

## 2 Answers

This is just a workaround.

By doing the substituting before calling DSolve then it gives the correct solution.

ClearAll[r, a, b, c, t];
ic = r[0] == 0;
vars = {a -> 1, b -> 2, c -> 3};
ode = D[r[t], t] == Piecewise[{{a, 0 <= t < c}}, 0] - b r[t];
sol = r[t] /. First@DSolve[{ode /. vars, ic}, r, t]
Plot[sol, {t, -1, 10}]


If the substitution is made in the solution afterwards, then yes, it does not give the correct solution.

ClearAll[r, a, b, c, t];
ic = r[0] == 0;
vars = {a -> 1, b -> 2, c -> 3};
ode = D[r[t], t] == Piecewise[{{a, 0 <= t < c}}, 0] - b r[t];
sol = r[t] /. First@DSolve[{ode, ic}, r, t];
sol = sol /. vars;
Plot[sol, {t, -1, 10}]


Version 12.3.1. windows 10

Fyi, Fixed in V 13.0

Clear["Global*"]
eqns={
r[0]==0,
D[r[t],t]== Piecewise[{{a, 0<=t<c}},0] - b r[t]
}

sol=Assuming[
And[a>0,b>0,c>0],
FullSimplify[
r[t]/.First@DSolve[eqns,r,t]
]
]


nsol=ReplaceAll[sol, {a->1,b->2,c->3}];
Plot[nsol, {t,-1,10}]
`