13
$\begingroup$

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

Output

$\endgroup$
  • 3
    $\begingroup$ 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]]]; $\endgroup$ – Volker Sep 20 '13 at 8:45
  • $\begingroup$ A related math.SE question. $\endgroup$ – J. M. is away May 15 '15 at 9:17
  • $\begingroup$ Related: mathematica.stackexchange.com/questions/57554/… $\endgroup$ – evanb Mar 4 '17 at 9:15
23
$\begingroup$

What about this?

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

enter image description here

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

enter image description here


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]

enter image description here

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]

enter image description here

(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" -> {_, {_, _, _}}};

enter image description here

Mr.Wizard

$\endgroup$
  • $\begingroup$ ,I want to know why I cannot log in stackexchange of mathematica in China? $\endgroup$ – xyz Sep 20 '13 at 14:24
  • 2
    $\begingroup$ @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? $\endgroup$ – Mr.Wizard Sep 24 '13 at 1:23
  • 1
    $\begingroup$ Done. I hope you like it. $\endgroup$ – Mr.Wizard Sep 25 '13 at 14:56
  • 3
    $\begingroup$ Note that Mod[val - min, max - min] + min is equivalent to the simpler Mod[val, max - min, min]. $\endgroup$ – Michael E2 Jan 7 '15 at 11:36
  • 1
    $\begingroup$ @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}] ? $\endgroup$ – Euler_Salter Mar 3 '17 at 18:31
10
$\begingroup$

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];
$\endgroup$
7
$\begingroup$

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

enter image description here

Or, equivalently (and somewhat more concisely)

 g[t_] := If[Abs[t] < 5, t^2, g[t - Sign[t] 10]]
$\endgroup$
7
$\begingroup$

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

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

enter image description here

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

enter image description here

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

enter image description here

$\endgroup$
1
$\begingroup$

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

Periodic Waveform

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

Shifted Periodic Waveform

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

Testing the Periodic Waveform Structure

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 :)!

$\endgroup$
  • $\begingroup$ This answer seems closely related to the answer by mm.Jang. $\endgroup$ – bbgodfrey May 21 '15 at 13:27
  • $\begingroup$ I agree with @bbgodfrey, this is an almost duplicate answer - I would encourage you to delete it. $\endgroup$ – gpap May 21 '15 at 13:55
  • $\begingroup$ It's also in the link I gave (or at least in a slightly different form). $\endgroup$ – J. M. is away May 21 '15 at 16:35
  • $\begingroup$ 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 :'( $\endgroup$ – Kerren May 21 '15 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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