# Double integration with a piecewise function gives wrong result

I am attempting to solve a double integral $$I(t) = \int_0^t dt' \int_0^{t'} dt'' f(t'')$$ of a piecewise function of the type $$f(t) = \begin{cases}1 & \text{for } 0 \leq t < a \\ -1 & \text{for } t>a\end{cases}$$ with Mathematica. One can easily verify that for $$t>a$$ this should evaluate to $$I(t) = \frac12 t (4 a - t) - a^2$$ However, if I try to solve in Mathematica using

Simplify[Integrate[Integrate[Piecewise[{{1, 0 <= ttt < a}, {-1, a <= ttt}}], {ttt, 0, tt}], {tt,0, t}], {t > a > 0}]


The result I get is

-(1/2) t (-4 a + t)


I.e. it is obviously missing the $$-a^2$$.

I am puzzled why this is happening. Am I simply too stupid to use Mathematica's Piecewise/Integrate function correctly? If instead of the variable $$a$$ I plug in a number, say 1, everything seems to evaluate correctly.

• Have you already seen formula 5 here, by any chance? – J. M.'s ennui Jun 3 '20 at 16:36
• The command Integrate[ Piecewise[{{1, 0 <= ttt < a}, {-1, a <= ttt}}], {tt, 0, t}, {ttt, 0, tt}, Assumptions -> t > a > 0] performs 1/2 (-2 a^2 + 4 a t - t^2). – user64494 Jun 3 '20 at 17:02

In Mathematica 12, your original code returns the warning

Integrate::pwrl: Unable to prove that integration limits {0,tt} are real. Adding assumptions may help.

And, indeed, adding the appropriate assumptions does help:

Simplify[Integrate[Integrate[
Piecewise[{{1, 0 <= ttt < a}, {-1, a <= ttt}}], {ttt, 0, tt},
Assumptions -> {tt, 0} \[Element] Reals], {tt, 0, t}], {t > a > 0}]

(* -a^2 + 2 a t - t^2/2 *)


I couldn't tell you why such assumptions are necessary, but it does seem to give you the correct answer this way.

• Thanks a lot, so I guess I was right about just being too stupid. Strangely, I did not get the warning you are quoting. – André Jun 4 '20 at 12:11
• @André: I probably would have been confused too if I hadn't received any error messages. Which version of Mathematica are you using? – Michael Seifert Jun 4 '20 at 14:10

Combining your two sequential evaluations of Integrate into one, from your original code:

Integrate[
Piecewise[{{1, 0 <= tDoublePrime < a}, {-1, tDoublePrime > a}}],
{tPrime, 0, t},
{tDoublePrime, 0, tPrime}
]


returns a warning:

Integrate::pwrl: Unable to prove that integration limits {0, t, tPrime} are real. Adding assumptions may help.

So let's add assumptions as suggested:

Assuming[
{t ∈ Reals, tPrime ∈ Reals},
Integrate[
Piecewise[{{1, 0 <= tDoublePrime < a}, {-1, tDoublePrime > a}}],
{tPrime, 0, t},
{tDoublePrime, 0, tPrime}
]
]


You mentioned that you are particularly interested in the case of $$t>a$$, so we can include that assumption as well, and see if the output can be simplified:

Assuming[
{t ∈ Reals, tPrime ∈ Reals, t > a},
Simplify@
Integrate[
Piecewise[{{1, 0 <= tDoublePrime < a}, {-1, tDoublePrime > a}}],
{tPrime, 0, t},
{tDoublePrime, 0, tPrime}
]
]


The result above is equivalent to the one you mentioned for $$t>a$$.

If you changed the definition of your Piecewise function to specifically include that $$a>0$$ (e.g. Piecewise[{{1, 0 <= tDoublePrime < a}, {-1, tDoublePrime > a > 0}}]), then you could further simplify the output.