# How to plot "chainsaw" functions in Maple?

I want to plot piecewise function. For example,

$$\begin{cases} 0, &\ 0\le t

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


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!

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

## 2 Answers

You may try:

 f:=t->12*exp(-t+4)*Heaviside((t-4)*(6-t)):
s:=t->t-floor(t):
plot(f(6*s(t/6)),t=0..25);


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

• The function looks periodic, but it's not. Dec 9, 2018 at 10:51
• @Jean-ClaudeArbaut I see your point and I edited my answer. Do you like it now? Dec 9, 2018 at 11:27
• @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. Dec 9, 2018 at 12:12
• 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? Dec 9, 2018 at 12:22
• Yes, that is the period. 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:

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


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

foo(5);
4.414553294

foo(11);
4.414553294


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

foo(x);
foo(x)


Now for some plots,

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


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


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


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

• Thank you very much for new detailed approach to solving this task! Dec 10, 2018 at 9:52