## How to plot periodic function's graphic?

14

9

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


3Your 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 – 2013-09-20T08:45:19.340

A related math.SE question. – J. M.'s ennui – 2015-05-15T09:17:41.927

– evanb – 2017-03-04T09:15:32.010

23

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 – 2013-09-20T14:24:45.710

I have no trouble logging in this site in China. Are you Chinese? And if you want to chat, please go to the chat room http://chat.stackexchange.com/rooms/2234/mathematica

– mmjang – 2013-09-20T14:40:18.027

2@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 – 2013-09-24T01:23:43.980

@Mr.Wizard Edit my answer would be nice :) Thanks. – mmjang – 2013-09-24T04:34:25.293

1Done. I hope you like it. – Mr.Wizard – 2013-09-25T14:56:35.083

This makes my function more robust and useful. And it teaches me a lot, thank you. – mmjang – 2013-09-26T04:24:15.357

3Note that Mod[val - min, max - min] + min is equivalent to the simpler Mod[val, max - min, min]. – Michael E2 – 2015-01-07T11:36:34.183

@ChenStatsYu,Yes, you're right:) – xyz – 2015-05-16T00:46:34.250

1@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 – 2017-03-03T18:31:37.457

@Euler_Salter Good catch. Every appearance of NumberQ should be NumericQ; I copied the first form in writing my extension without considering the implication of that. – Mr.Wizard – 2017-03-03T20:07:16.060

@MichaelE2 Two years late but I applied your simplification. Thanks. – Mr.Wizard – 2017-03-03T20:21:07.260

@Mr.Wizard Thank you a lot! – Euler_Salter – 2017-03-04T02:53:17.320

10

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


8

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


7

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


1

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 – 2015-05-21T13:27:53.523

I agree with @bbgodfrey, this is an almost duplicate answer - I would encourage you to delete it. – gpap – 2015-05-21T13:55:01.897

It's also in the link I gave (or at least in a slightly different form). – J. M.'s ennui – 2015-05-21T16:35:23.920

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 – 2015-05-21T17:19:09.690