2
$\begingroup$

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.

$\endgroup$
2
  • 2
    $\begingroup$ Have you already seen formula 5 here, by any chance? $\endgroup$ Jun 3, 2020 at 16:36
  • 2
    $\begingroup$ 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). $\endgroup$
    – user64494
    Jun 3, 2020 at 17:02

2 Answers 2

3
$\begingroup$

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.

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

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}
 ]
]

piecewise integral with no simplification

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}
   ]
]

result for t > a

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.