7
$\begingroup$

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

EDIT 2:

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.

$\endgroup$
9
  • $\begingroup$ Do you wish to get the area from this integration? $\endgroup$
    – Öskå
    Aug 6, 2014 at 13:05
  • 1
    $\begingroup$ FYI This is basically the same. $\endgroup$
    – Öskå
    Aug 6, 2014 at 13:13
  • $\begingroup$ 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. $\endgroup$
    – Patrik
    Aug 6, 2014 at 13:15
  • $\begingroup$ And at least the integration works with SawtoothWave. See here. $\endgroup$
    – Öskå
    Aug 6, 2014 at 13:16
  • $\begingroup$ 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. $\endgroup$
    – Patrik
    Aug 6, 2014 at 13:35

2 Answers 2

3
$\begingroup$

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, 
  Piecewise[{
   {Print[x]; x - 1, x >= 1},
   {2 x + x, 0 <= x < 1},
   {Print[x]; x + 1, x < 0}}]]

Mathematica graphics

fff[x]
(*
  x
  x
*)

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Interesting! Thanks very much. $\endgroup$
    – Patrik
    Aug 6, 2014 at 14:41
2
$\begingroup$

You could define your function as

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

then

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

$\endgroup$
3
  • $\begingroup$ Thank you. See EDIT in my question. $\endgroup$
    – Patrik
    Aug 6, 2014 at 13:39
  • $\begingroup$ I think you'll find that the integral gives 5 :) $\endgroup$ Aug 6, 2014 at 13:40
  • $\begingroup$ @TeakeNutma thanks! typo...evaluates correctly...thanks for reading post $\endgroup$
    – ubpdqn
    Aug 6, 2014 at 13:44

Your Answer

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

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