# Integrating SquareWave - don't understand result

[Acknowledged as a bug persisting in V12.2]

Integrate[SquareWave[t], t] // InputForm


Gives as output

Piecewise[{{-t, Inequality[1/2, LessEqual, Mod[t, 1], Less, 1]}}, t]


I don't understand how this can be the right answer. For example, substituting in

% /. t->19/2


gives -19/2, whereas

NIntegrate[SquareWave[t], {t, 0, 19/2}]


gives 0.5

Edited:

As made clear in the comments below (very informative thank you), there are unavoidable ambiguities in indefinite integrals for functions with branch cuts in the complex plane.

However, as explicitly stated in the documentation, SquareWave is only defined for real numbers. In this case, the result returned by Mathematica seems perverse. Wouldn't a more appropriate result be

TriangleWave[t + 3/4]/4

• do SquareWave[t] // PiecewiseExpand Commented Jun 6, 2016 at 19:56
• Both answers are right. An indefinite integral has more than one possible answer. The fact that the integrand is not continuous complicates things a bit, as the two answer differ by a different constant on each $(k,k+1)$ interval. Commented Jun 6, 2016 at 20:20
• I understand that an indefinite integrals can differ by arbitrary constants, and that the value can depend on the contour chosen across the complex plane (which I don't think can be relevant here). However, I don't see that finite step change in the integrand should introduce a step change in the integral. Commented Jun 6, 2016 at 20:56
• Not just arbitrary constants, arbitrary piecewise constants in general. See e.g. this. If you want to try things yourself, plot the result of Integrate[SquareWave[t], t] - Integrate[PiecewiseExpand[SquareWave[t], 0 < t < 19/2], t] Commented Jun 7, 2016 at 1:47
• I'm not even talking about branch cuts; it is just that Mathematica in this case chose the particular integral with the arbitrary piecewise constant you don't want. Commented Jun 8, 2016 at 0:06

## 4 Answers

May be it will help if you look at the plots:

Plot[SquareWave[t], {t, 0, 10}]


Plot[Evaluate[Integrate[SquareWave[t], t]], {t, 0, 10}]


• But this doesn't agree with the values I get using NIntegrate Commented Jun 6, 2016 at 20:05
• @mikado, then your question is not complete, and it should be edited. Commented Jun 6, 2016 at 20:09
• @mikado Szabolcs answered your addendum for agreement with NIntegrate. The discrepancy is also discussed in this "MathWorld" page: "Indefinite Integral". Commented Jun 6, 2016 at 20:34

One problem with picking a constant at each discontinuity is that there are infinitely many discontinuities. In general, this couldn't be done, so returning a mathematically correct answer would be the next best thing. In the SquareWave[] case, some cleverness leads some to realize that there is a way to choose the constants or even an antiderivative expression (in terms ofTriangleWave[]). One might hope this could be handled (eventually) by a special rule for SquareWave[].

You can get a continuous result by restricting the domain, thereby restricting the number of discontinuities. The answer is valid only over the domain, however.

Assuming[{0 < t < 10},
Integrate[SquareWave[t], t]
]
Plot[%, {t, 0, 10}]


Out[]= large piecewise function

Wolfram support have confirmed that

TriangleWave[t+3/4]/4


would be a more appropriate result for

Integrate[SquareWave[t], t]


EDIT

This problem remains in V12.2

I'd just like to weigh in that this is one of the reasons I don't like indefinite integrals most of the time. I much prefer to consider definite integrals with the upper limit as a variable, since that fixes the whole off-by-a-constant (or constants-on-each-piecewise-segment, as we're seeing here) problem and replaces it with the much more intuitive question of "where do I put the lower limit?".

Unfortunately, it seems that Mathematica can't deal with this kind of integral right now, even though it's perfectly well-defined:

Assuming[t > 0,
Integrate[SquareWave[\[FormalT]], {\[FormalT], 0, t}]
]


(* returns unevaluated *)