I want to plot piecewise function. For example,

\begin{cases} 0, &\ 0\le t<t_1 \\ 12e^{-(t-t_1)}, & \ t_1\le t<t_2 \end{cases}

The problem is that function is periodic (let $t_1$=4 and $t_2$=6, period $6$). For one period, it's pretty easy:

f := 12*exp(-t+4);
g := 0;
piece := piecewise(0 <= t and t < 4, g, 4 < t and t <= 6, f);
plot(piece, t = 0 .. 6);

enter image description here

But I don't know how to make it periodic. Of course, I can add some new functions in piecewise, but I'm sure that more adequate way to plot periodic fuctions exists. Any help appreciated!

  • 1
    $\begingroup$ Consider $\frac6\pi\arctan(\tan(\frac\pi6x+\frac\pi2))+3$. $\endgroup$ Dec 9, 2018 at 10:44

2 Answers 2


You may try:


It is easy to modify it with other values of $t_1$ and $t_2$.

enter image description here

  • $\begingroup$ The function looks periodic, but it's not. $\endgroup$ Dec 9, 2018 at 10:51
  • 1
    $\begingroup$ @Jean-ClaudeArbaut I see your point and I edited my answer. Do you like it now? $\endgroup$
    – Robert Z
    Dec 9, 2018 at 11:27
  • 1
    $\begingroup$ @KellyShepphard Yes we can generalize to more pieces. For each piece you use a Heaviside step function and then you add together all the pieces. $\endgroup$
    – Robert Z
    Dec 9, 2018 at 12:12
  • 1
    $\begingroup$ if you have two pieces $f1$ on $[a,b]$ and $f2$ on $[c,d]$ then f=f1*Heaviside((x-a)(b-x))+f2*Heaviside((x-c)(d-x)). Is it clear? $\endgroup$
    – Robert Z
    Dec 9, 2018 at 12:22
  • 1
    $\begingroup$ Yes, that is the period. $\endgroup$
    – Robert Z
    Dec 9, 2018 at 12:28

You should not have to manually copy any part of the original expression into a new operator (manually, eg, cut & paste), to accomplish this. The unapply command is useful for that kind of thing.

You started out by giving us this:

f := 12*exp(-t+4):
g := 0:
piece := piecewise(0 <= t and t < 4, g, 4 < t and t <= 6, f):

plot(piece, t = 0 .. 6, size=[200,200]);

enter image description here

Now let's construct an operator from that, which behaves like the supplied piecewise, with a period of our choice.

We'll use a re-usable constructor for this purpose.

makeperiodic := proc(expr, var, skip)
  local T, r;
  r := skip/2;
  unapply( 'eval'(expr, var=r+'frem'(T+r,skip)), [T], numeric);
end proc:

Here is the construction of the periodic operator, and quick check.

foo := makeperiodic( piece, t, 6 ):



This operator returns unevaluated when its argument is not numeric, by design.


Now for some plots,

# operator form
plot(foo, -12 .. 12, size=[600,200]);

enter image description here

# expression form (unevaluated function call)
plot(foo(x), x=-12 .. 12, size=[600,200]);

enter image description here

# shift two to the left
plot(foo(x+2), x=-12 .. 12, size=[600,200]);

enter image description here

And we could do a similar thing for some other choice of period,

bar := makeperiodic( piece, t, 5 ):
plot(bar, -10 .. 10, size=[500,200]);
plot(bar(t), t=-10 .. 10, size=[500,200]);
# shift 4 to the right
plot(bar(t-4), t=-10 .. 10, size=[500,200]);
  • $\begingroup$ Thank you very much for new detailed approach to solving this task! $\endgroup$ Dec 10, 2018 at 9:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .