4
$\begingroup$

[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
$\endgroup$
13
  • 2
    $\begingroup$ do SquareWave[t] // PiecewiseExpand $\endgroup$
    – george2079
    Commented Jun 6, 2016 at 19:56
  • 1
    $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Commented Jun 6, 2016 at 20:20
  • $\begingroup$ 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. $\endgroup$
    – mikado
    Commented Jun 6, 2016 at 20:56
  • 1
    $\begingroup$ 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] $\endgroup$ Commented Jun 7, 2016 at 1:47
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Jun 8, 2016 at 0:06

4 Answers 4

2
$\begingroup$

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

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

enter image description here

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

enter image description here

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

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

Mathematica graphics

$\endgroup$
2
$\begingroup$

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

$\endgroup$
2
$\begingroup$

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 *)

$\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.