# How to extend a function by period and display it [duplicate]

I want to extend this function into a new function according to its period

Plot[-((E^x + E^(-x))/2), {x, -1, 1}, AxesOrigin -> {0, 0}]


and then draw this function as follows

Here is a somewhat more general approach. It allows the basic period of the periodic extension to be any interval in its source function's domain.

Clear[f, xf]
f[x_] := -((E^x + E^(-x))/2)
f[x_, lo_, hi_] /; lo ≤ x < hi := f[x]
xf[x_, lo_, hi_] :=
With[{span = hi - lo},
Piecewise[{
{f[x + span Quotient[hi - x, span], lo, hi], x < lo},
{f[-x + span Quotient[x - lo, span], -hi, -lo], x > hi}},
f[x, lo, hi]]]


The plot you request is then:

Plot[xf[x, -1, 1], {x, -5, 5}, AxesOrigin -> {0, -1.54}]


But a plot making the period of xf the asymmetric interval {-1, .5] is just as easy.

Plot[xf[x, -1, .5], {x, -4, 5}, AxesOrigin -> {0, -1.54}]


One possible way

T = 2; (*period*)
f[x_] := -((E^x + E^(-x))/2);
fExtended[x_] := Piecewise[{{f[x], -T/2 < x < T/2},
{fExtended[x - T], x > T/2},
{fExtended[x + T], x < T/2}}];
Plot[fExtended[x], {x, -1, 1}, AxesOrigin -> {0, 0}, AxesOrigin -> {0, 0}]


Plot[fExtended[x], {x, -2 T, 2 T}, AxesOrigin -> {0, 0}, AxesOrigin -> {0, 0}]


Let's replace x with Mod[x, 2, -1]

Plot[-((E^Mod[x, 2, -1] + E^(-Mod[x, 2, -1]))/2), {x, 0, 10}]


We can get the result we want.

T = 4; (* period *)
x0 = -2; (* start of the function sampling*)

f[x_] := -((E^x + E^(-x))/2);
xExt[x_] := Mod[x - x0, T] + x0;
fExt = f@*xExt;

Plot[fExt[x], {x, -5, 5}]


fExt now repeats f in the interval $$[x_0,x_0+T]$$. The '@*' means function composition. If you define h=f@*g; then h[x]==f[g[x]].

Edit: made a function out of it for ease of use. This gives the same output as above.

MakePeriodic[f_, T_, x0_: 0] := Module[{xExt},
xExt = Function[x, Mod[x - x0, T] + x0];
f@*xExt
]

fExt = MakePeriodic[f,4,-2];
Plot[fExt[x], {x,-5, 5}]