# How to plot periodic function's graphic?

How to plot periodic function's graphic?

For example, $f(t) = t$ when $-5<t<5$ and $f(t+10) = f(t)$.

f[t] := If[-5 <= t <= 5, t, f[t + 10] = f[t]];
Plot[f[t], {t, -20, 20}]


• Your syntax is off. Functions in Mathematica are defined by patterns, so it needs to be f[t_] on the left hand side. Also, I don't know why you expect Mathematica to understand f[t+10]=f[t] properly. It works if you define it like this: f[t_] := If[-5 <= t <= 5, t, If[t > 0, f[t - 10], f[t + 10]]]; – Volker Sep 20 '13 at 8:45
• A related math.SE question. – J. M. will be back soon May 15 '15 at 9:17
• – evanb Mar 4 '17 at 9:15

This defines a rather general function myperiodicthat translate a normal function to periodic form, the second parameter {val, min, max} specify a periodic interval of your desired periodic function:

myperiodic[func_, {val_Symbol, min_?NumericQ, max_?NumericQ}] :=
func /. (val :> Mod[val - min, max - min] + min)


Then you can use it to plot things:

Plot[myperiodic[t, {t, -5, 5}] // Evaluate, {t, -40, 40}]


Plot[myperiodic[t^2, {t, -3, 5}] // Evaluate, {t, -40, 40}]


# Extensions

We can extend this function in a couple of possibly useful ways. It may be noted that if t has a global value the plots above will fail, because Evaluate breaks the scoping of Plot. This could be remedied by using the Evaluated option but it would be nice not to need either. We can achieve that by holding arguments unevaluated using HoldAll and then holding at least the Symbol and expression unevaluated while making the substitution. These methods come to mind:

• Adding Unevaluated and HoldPattern to the existing replacement
• leveraging the attributes of Pattern and RuleDelayed in an inverted rule form (injector pattern)
• Using Function which holds parameter name and body
• An undocumented syntax for With (:=) that does not evaluate substitution expressions

In code:

SetAttributes[{periodic1, periodic2, periodic3, periodic4}, HoldAll]

periodic1[expr_, {s_Symbol, min_?NumericQ, max_?NumericQ}] :=
Unevaluated[expr] /. HoldPattern[s] :> Mod[s, max - min, min]

Quiet[
periodic2[expr_, {s_Symbol, min_?NumericQ, max_?NumericQ}] :=
Mod[s, max - min, min] /. s_ :> expr
]

periodic3[expr_, {s_Symbol, min_?NumericQ, max_?NumericQ}] :=
Function[s, expr][Mod[s, max - min, min]]

periodic4[expr_, {s_Symbol, min_?NumericQ, max_?NumericQ}] :=
With[{s := Mod[s, max - min, min]}, expr]


Testing indicates that the last method is the fastest:

First @ AbsoluteTiming @ Do[#[7 + t^2, {t, -5, 5}], {t, -40, 40, 0.001}] & /@
{periodic1, periodic2, periodic3, periodic4}

{0.35492, 0.382667, 0.237522, 0.235105}


Demonstration of use:

Plot[periodic4[7 + t^2, {t, -5, 5}], {t, -40, 40}, Frame -> True]


The next extension that can be valuable it to have periodic return a function rather than a bare expression. While the functions above evaluate correctly internally in the presence of a global assignment to the declared Symbol the result evaluates with that global value and therefore cannot be reused. Returning a function allows us to use it more generally such as mapping over a list of values, which can have advantage as I will show.

### Proposal

SetAttributes[periodic, HoldAll]

periodic[expr_, {s_Symbol, min_?NumericQ, max_?NumericQ}] :=
Join[s &, With[{s := Mod[s, max - min, min]}, expr &]]


(Note that Join works on any head. Function acts to hold the parts of the final Function before it is assembled.)

Now:

periodic[7 + t^2, {t, -5, 5}]

Function[t, 7 + Mod[t, 5 - -5, -5]^2]

Plot[periodic[7 + t^2, {t, -5, 5}][x], {x, -40, 40}, Frame -> True]


(Note that x was used above for clarity but using t would not conflict.)

If the somewhat awkward from of Function returned is a concern know that it will be optimized by Compile, either manually or automatically:

Compile @@ periodic[7 + t^2, {t, -5, 5}]

CompiledFunction[{t}, 7 + (Mod[t + 5, 10] - 5)^2, -CompiledCode-]


The function-generating form does not perform quite as well (as e.g. periodic4) when used naively:

Table[periodic[7 + t^2, {t, -5, 5}][t], {t, -40, 40, 0.001}] // AbsoluteTiming // First

0.3600206


However it allows for superior performance if applied optimally, using Map, which auto-compiles:

periodic[7 + t^2, {t, -5, 5}] /@ Range[-40, 40, 0.001] // AbsoluteTiming // First

0.0130007


As a final touch we can give our function proper syntax highlighting:

SyntaxInformation[periodic] =
{"LocalVariables" -> {"Table", {2}}, "ArgumentsPattern" -> {_, {_, _, _}}};


Mr.Wizard

• ，I want to know why I cannot log in stackexchange of mathematica in China? – xyz Sep 20 '13 at 14:24
• @mm.Jang I would like to attempt to refine your code. Would you prefer that I edit your answer, or post one of my own? – Mr.Wizard Sep 24 '13 at 1:23
• Done. I hope you like it. – Mr.Wizard Sep 25 '13 at 14:56
• Note that Mod[val - min, max - min] + min is equivalent to the simpler Mod[val, max - min, min]. – Michael E2 Jan 7 '15 at 11:36
• @Mr.Wizard why it doesn't work if you put Pi in the range of t? for example Plot[myperiodic[Exp[2*t], {t, -Pi, Pi}] // Evaluate, {t, -20, 20}] ? – Euler_Salter Mar 3 '17 at 18:31

another way to define a periodic function

Clear[f];
f[t_ /; -5 <= t < 5 ] := t
f[t_ /; t >= 5] := f[t - 10];
f[t_ /; t < -5] := f[t + 10];


Here's a pretty straightforward alternative for defining a periodic function:

 f[t_] := Which[-5 < t <= 5, t^2, t < -5, f[t + 10], t > 5, f[t - 10]]

Plot[f[t], {t, -30, 30}]


Or, equivalently (and somewhat more concisely)

 g[t_] := If[Abs[t] < 5, t^2, g[t - Sign[t] 10]]


Using built in functions: SawtoothWave , TriangleWave, and SquareWave:

Plot[10 SawtoothWave[(x - 5)/10] - 5, {x, -20, 20},
ExclusionsStyle -> Dashed]


Plot[SawtoothWave[x]^2, {x, -2, 2}, ExclusionsStyle -> Dashed]


Plot[1 - TriangleWave[x/(4 \[Pi])]^2, {x, -5 \[Pi], 5 \[Pi]},
Ticks -> {Range[-5 \[Pi], 5 \[Pi], \[Pi]], Automatic}]


Hey dude I know this is a year late but I have another option that people might be interested in :) You can also use the modulus to create a periodic waveform similar to previous comments but this is the "uncompressed" version :)!

f[t_] := t;
periodicf[t_] := f[Mod[t, 10] - 5];
Plot[periodicf[t], {t, -100, 100}]


You'll notice that I've shifted the t value because you'd like it to start from a non-zero t value but then that results in a shift in the graph. To fix that we can use:

f[t_] := t;
periodicf[t_] := f[Mod[t+5, 10] - 5];
Plot[periodicf[t], {t, -100, 100}]


So in general if you have a fuction, f[t], and you want to make it periodic, periodicf[t], between the domain [minTime, maxTime] then you can use this structure:

f[t_] := t; (* Or any function of time that you want *)
periodicf[t_] := f[Mod[t - minTime, maxTime-minTime] + minTime];
Plot[periodicf[t], {t, -100, 100}]


Let's test it for the same thing but from -1 < t < 3:

f[t_] := t;
minTime = -1;
maxTime = 3;
periodicf[t_] := f[Mod[t - minTime, maxTime - minTime] + minTime];
Plot[periodicf[t], {t, -100, 100}]


I'd also like to mention that I know that I read this somewhere else on the internet and remembered it because it's such a great trick but I can't remember where I read it :/! So please don't give me full credit for this :)!

• This answer seems closely related to the answer by mm.Jang. – bbgodfrey May 21 '15 at 13:27
• I agree with @bbgodfrey, this is an almost duplicate answer - I would encourage you to delete it. – gpap May 21 '15 at 13:55
• It's also in the link I gave (or at least in a slightly different form). – J. M. will be back soon May 21 '15 at 16:35
• If you want to delete it then you can but why I put it here is because it shows what mm.Jang is actually doing in less complicated syntax. Instead of just copying code because it works I thought it might be useful to understand why it works? That's why it's "uncompressed" :) But if you guys rate I should remove it I will :'( – Kerren May 21 '15 at 17:19