0
$\begingroup$

I am programming a triangle wave.

My issue here, is that when I set the period from -2T, 2T I cannot plot it; an error appears:

Iteration limit of 4096 exceeded

f[x_] := 2 t /; 0 <= t < T/2;
f[x_] := 2 - 2 t /; t/2 <= t < T;
(*PERIOD*)T := 1;
Plot[f[x], {t, 0, T}];

(*this is the part were if I increase the fundamental period to be plotted I have an error "Iteration limit of 4096 exceeded."*)
f[x_] := f[t - T] /; t > T;
f[x_] := f[t + T] /; t < 0
T := 1;
Plot[f[x], {t, -T, T}]}
$\endgroup$
2
  • 1
    $\begingroup$ Changing x_ to t_ on the left-hand sides fixes it here I think... $\endgroup$ Oct 27, 2016 at 14:27
  • $\begingroup$ Mathematica has a built in function for triangle waves $\endgroup$
    – mikado
    Oct 27, 2016 at 18:05

1 Answer 1

1
$\begingroup$
  1. Periodic functions are best defined with Mod. This avoids recursion.

  2. Variables should match on the lefthand and righthand side of function definitions.

  3. It is best to localize variables and parameters.

With these points in mind, you can write:

Clear[f]
f[t_, T_] /; Mod[t, T] <= T/2 := 2 Mod[t, T]
f[t_, T_] := 2 - 2 Mod[t, T]

With[{T = 1}, Plot[f[t, T], {t, -2 T, 2 T}]]

plot

You might also look at Piecewise.

$\endgroup$

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.