# Getting error when trying to plot triangle wave

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}]}

• Changing x_ to t_ on the left-hand sides fixes it here I think... Oct 27, 2016 at 14:27
• Mathematica has a built in function for triangle waves Oct 27, 2016 at 18:05

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}]] You might also look at Piecewise.