1

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!

2 Answers2

2

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$.

enter image description here

Robert Z
  • 145,942
  • The function looks periodic, but it's not. – Jean-Claude Arbaut Dec 09 '18 at 10:51
  • I did, when there was only the hint with the heaviside function, which, being a step function, is totally useless alone to get a periodic one. I am now considering removing it at this answers the question of the plot, with a trick. – Jean-Claude Arbaut Dec 09 '18 at 10:52
  • @Jean-ClaudeArbaut Given an interval to be plot, the real periodic function and mine can be made the same. – Robert Z Dec 09 '18 at 10:56
  • Yes, that's why I removed the downvote. But notice it now all depends on the reange of the plot (can be adapted with a varying upper bound in the sum, but this is way too tricky to my taste). While there is a simple way to make the function mathematically periodic. – Jean-Claude Arbaut Dec 09 '18 at 10:59
  • 1
    @Jean-ClaudeArbaut I see your point and I edited my answer. Do you like it now? – Robert Z Dec 09 '18 at 11:27
  • @Robert Z, thank you for the answer, +1. Is your idea appliable to any piecewise periodic function or to this particular only? It seems to me that Heaviside step function can be used only if one of "pieces" is zero. Does a way to plot any periodic piecewise function exists? – Kelly Shepphard Dec 09 '18 at 12:02
  • 1
    @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. – Robert Z Dec 09 '18 at 12:12
  • @Robert Z, so the basic algorithm is to make for each function first two lines of code, and then plot f1+f2+f3...? If you don't mind, could you explain (or give a link), please, how to use these lines for any function? – Kelly Shepphard Dec 09 '18 at 12:18
  • 1
    if you have two pieces $f1$ on $[a,b]$ and $f2$ on $[c,d]$ then f=f1Heaviside((x-a)(b-x))+f2Heaviside((x-c)(d-x)). Is it clear? – Robert Z Dec 09 '18 at 12:22
  • Yes, thank you very much. And both "6" in "plot" are for period given? – Kelly Shepphard Dec 09 '18 at 12:23
  • 1
    Yes, that is the period. – Robert Z Dec 09 '18 at 12:28
  • Thanks for your help! I think I get it now. :) – Kelly Shepphard Dec 09 '18 at 12:30
1

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

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

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

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]);
acer
  • 5,293