Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a periodic function ff:

ff := Function[x, Piecewise[{{ff[x - 1], x >= 1}, {2 x, 0 <= x < 1}, {ff[x + 1], x < 0}}]]

Plotting it works fine:

Plot[ff[x], {x, -4, 4}, PlotRange -> {{-4, 4}, {-.5, 3}}]

Mathematica graphics

But integrating it like this:

Integrate[ff[t], {t, 0, 5}]

What I get is a few of:

$RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>

and a few of:

$IterationLimit::itlim: Iteration limit of 4096 exceeded. >>

And it keeps running until I abort the evaluation. What's the problem? Thanks.

EDIT: Thanks for the answers. To avoid any more confusion, this is the signal I actually have to integrate. It's a bit more complex, and I wanted to know if that's the reason why it won't integrate, that's why I went for the sawtooth signal.

s := x \[Function] Piecewise[
  {s[x + 2 Pi], x < -Pi},
  {0, -Pi <= x < -Pi/2},
  {Cos[x], -Pi/2 <=  x <= Pi/2},
  {0, Pi >= x > Pi/2},
  {s[x - 2 Pi], x > Pi}

Plot command:

Plot[s[x], {x, -8, 6}, PlotRange -> {{-8, 6}, {-.5, 6}}]

Mathematica graphics


Using this much simpler, "automatically" periodic definition for the signal, there seems to be no problem with integration:

s = Function[x, Piecewise[{{0, Cos[x] < 0}, {Cos[x], Cos[x] >= 0}}]]

Still doesn't answer the question why Mathematica isn't able to integrate the signal when it's defined like above.

share|improve this question
Do you wish to get the area from this integration? –  Öskå Aug 6 at 13:05
FYI This is basically the same. –  Öskå Aug 6 at 13:13
Thanks for the fix. Yes, that would be good. I'm exercising Fourier series, and encountered this problem while calculating a_0 (the constant). The formula would be: (2/T)*Integrate[ff[t], {t, 0, T}]], where T is the period. –  Patrik Aug 6 at 13:15
And at least the integration works with SawtoothWave. See here. –  Öskå Aug 6 at 13:16
Ok, thanks. What about this signal? s := x \[Function] Piecewise[{ {s[x + 2 Pi], x < -Pi}, {0, -Pi <= x < -Pi/2}, {Cos[x], -Pi/2 <= x <= Pi/2}, {0, Pi >= x > Pi/2}, {s[x - 2 Pi], x > Pi} }] This the one I actually have to integrate. Again, plotting works, but integrating it doesn't work at all. –  Patrik Aug 6 at 13:35

2 Answers 2

A fix

For s, you could use

s = x \[Function] Piecewise[{{Cos[x], -Pi/2 <= Mod[x, 2 π, -π] <= Pi/2}}];

Integrate[s[t], {t, -8, 6}]
(* 5 + Sin[6] *)

The problem

The problem with the original ff and s is that the function calls itself. Now consider ff[t] or s[t]. None of the conditions will evaluate to False so "the Piecewise function is returned in symbolic form" (documentation). Then the next bullet in the documentation is a somewhat mysterious line: "Only those $val_i$ explicitly included in the returned form are evaluated." Normally, in ff[2.3] say, only one of the values is evaluated; however, one can test that all of the values are evaluated in the case of ff[t]. That causes ff[t - 1] to be evaluated, which leads to ff[t - 2], ad infinitum were it not for limits on recursion and iteration.

Here's test function that does no recursion. We can see that x is printed twice, and the value of fff[x] contains the evaluated piecewise argument.

fff = Function[x, 
   {Print[x]; x - 1, x >= 1},
   {2 x + x, 0 <= x < 1},
   {Print[x]; x + 1, x < 0}}]]

Mathematica graphics


Mathematica graphics

share|improve this answer
Interesting! Thanks very much. –  Patrik Aug 6 at 14:41

You could define your function as

f[x_] := 2 Mod[x, 1]


Integrate[f[x], {x, 0, 5}]

yields 5 (as expect 5 triangles of area 1)

To plot:

Plot[f[x], {x, -4, 4}, Exclusions -> None]

enter image description here

share|improve this answer
Thank you. See EDIT in my question. –  Patrik Aug 6 at 13:39
I think you'll find that the integral gives 5 :) –  Teake Nutma Aug 6 at 13:40
@TeakeNutma thanks! typo...evaluates correctly...thanks for reading post –  ubpdqn Aug 6 at 13:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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