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}]
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityWhat about this?
This defines a rather general function myperiodic
that 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}]
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:
Unevaluated
and HoldPattern
to the existing replacementPattern
and RuleDelayed
in an inverted rule form (injector pattern)Function
which holds parameter name and bodyWith
(:=
) that does not evaluate substitution expressionsIn 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.
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
Mod[val - min, max - min] + min
is equivalent to the simpler Mod[val, max - min, min]
.
$\endgroup$
Jan 7, 2015 at 11:36
Plot[myperiodic[Exp[2*t], {t, -Pi, Pi}] // Evaluate, {t, -20, 20}]
?
$\endgroup$
Mar 3, 2017 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 :)!
f[t_]
on the left hand side. Also, I don't know why you expect Mathematica to understandf[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$