# MATLAB TUTORIAL for the Second Cource. Part 6: Periodic extension.

Periodic extension.
Let a and b be real numbers such that a < b. A function $$f\, : \, [a,b] \mapsto \,\mathbb{R}$$ is said to be piecewise continuous on $$[a,b]$$ if the following conditions are satisfied:
• there exists a finite set $$\{ x_1 , x_2 , \ldots , x_n \} \subset (a, b)$$ such that $$x_1 < x_2 < \cdots < x_n$$ and f is continuous on each interval
$\left( a , x_1 \right) , \quad \left( x_k , x_{k+1} \right) , \quad k=1,2,\ldots , n-1, \quad \left( x_n , b \right) ;$
• all the following one-sided limits exist
$\lim_{x\downarrow a} f \left( x \right) , \quad \lim_{x\uparrow x_k} f\left( x \right) , \quad \lim_{x\downarrow x_k} f\left( x \right) ,\quad k=1,2,\ldots , n-1, \quad \lim_{x \uparrow b} f\left( x \right) .$
A function $$f\, : \, \mathbb{R} \mapsto \,\mathbb{R}$$ is piecewise continuous on $$\mathbb{R} ,$$ if it is piecewise continuous on every finite subinterval of $$\mathbb{R} .$$

Let a and b be real numbers such that a < b. A function $$f\, : \, [a,b] \mapsto \,\mathbb{R}$$ is said to be piecewise smooth on $$[a,b]$$ if the following conditions are satisfied:
• there exists a finite set $$\{ x_1 , x_2 , \ldots , x_n \} \subset (a, b)$$ such that $$x_1 < x_2 < \cdots < x_n$$ and f is continuous and it has a continuous derivative f' on each interval on each interval
$\left( a , x_1 \right) , \quad \left( x_k , x_{k+1} \right) , \quad k=1,2,\ldots , n-1, \quad \left( x_n , b \right) ;$
• all the following one-sided limits exist
$\lim_{x\downarrow a} f \left( x \right) , \quad \lim_{x\uparrow x_k} f\left( x \right) , \quad \lim_{x\downarrow x_k} f\left( x \right) ,\quad k=1,2,\ldots , n-1, \quad \lim_{x \uparrow b} f\left( x \right) .$
• all the following one-sided limits exist
$\lim_{x\downarrow a} f' \left( x \right) , \quad \lim_{x\uparrow x_k} f' \left( x \right) , \quad \lim_{x\downarrow x_k} f' \left( x \right) ,\quad k=1,2,\ldots , n-1, \quad \lim_{x \uparrow b} f' \left( x \right) .$
If derivatives in the above condition are replaced by more weak condition so that function f is piecewise monotonic, then such function is said to satisfy the Dirichlet conditions. A function $$f\, : \, \mathbb{R} \mapsto \,\mathbb{R}$$ is piecewise smooth on $$\mathbb{R} ,$$ if it is piecewise smooth on every finite subinterval of $$\mathbb{R} .$$

Let a and b be real numbers such that a < b, and let f be a function $$f\, : \, (a,b] \mapsto \,\mathbb{R} .$$ The function $$F\, : \, \mathbb{R} \mapsto \,\mathbb{R}$$ defined by

$F(x) = f \left( x - \left( \left\lfloor \frac{x-a}{b-a} \right\rfloor -1 \right)\left( b-a \right) \right) , \quad x \in \mathbb{R} ,$
is called the periodic extension of f. Here $$\lfloor {\text a} \rfloor$$ is the floor of a real number a, which is the largest integer that does not exceed a.

Let a and b be real numbers such that a < b, and let f be a piecewise continuous function $$f\, : \, (a,b] \mapsto \,\mathbb{R} .$$ Let the function $$F\, : \, \mathbb{R} \mapsto \,\mathbb{R}$$ be the periodic extension of f. The Fourier periodic extension of f is the following function

$F_{\scriptstyle Fourier} (x) = \begin{cases} F \left( x \right) , & \quad \mbox{if F(x) is continuous at } x , \\ \frac{1}{2} \left[ F(x+0) + F(x-0) \right] , & \quad \mbox{if F is discontinuous at } x, \end{cases} \qquad x \in \mathbb{R} .$

Here the expression F(a + 0) means the limit value from the right: $$F(a+0) = \lim_{\epsilon \mapsto 0>0} F(a+\epsilon ) .$$ Similarly, $$F(a-0) = \lim_{\epsilon \mapsto 0>0} F(a-\epsilon ) .$$ The condition at the point of discontinuity follows from the fact that Fourier series, if it converges at this point, is equal to the average values of left and right limits. Therefore, it does not matter what value f(a) is assigned initially at the point of discontinuity x = a, its Fourier series will define it to be $$\frac{1}{2} \left[ f(a+0) + f(a-0) \right] .$$

In each example below we start with a function defined on an interval, plotted in blue; then we present the periodic extension of this function, plotted in other color; then we present the Fourier expansion of this function, which is a Fourier periodic extension of the given function. The last figure in each example shows in one plot the Fourier expansion and the approximation with the partial sum with 20 terms of the corresponding Fourier series.

Example: Consider the following functions:

• $$f_1 (x) = \mbox{sign}(x)\,: \, (-1,1] \mapsto \, \mathbb{R})$$
• a = Plot[{Sign[t]}, {t, -2, 2}, PlotStyle -> {Thick, Blue}, AspectRatio -> 1/2]
b = Graphics[{Blue, Thick, Circle[{0, 0}, 0.02]}, AspectRatio -> 1]
Show[a, b]
However, it is not what we want because signum function is defined on infinite real line $$(-\infty , \infty ) ,$$ but we need its periodic extension with period 2.
a = Graphics[{Red, Arrowheads[0.05], Arrow[{{-1, -1}, {0, -1}}]}]
b = Graphics[{Red, Arrowheads[0.05], Arrow[{{1, 1}, {0, 1}}]}]
c = Graphics[{Red, Thick, Circle[{0, 0}, 0.05]}, AspectRatio -> 1/2]
b2 = Graphics[{Red, Arrowheads[0.05], Arrow[{{3, 1}, {2, 1}}]}]
a1 = Graphics[{Red, Arrowheads[0.05], Arrow[{{1, -1}, {2, -1}}]}]
c2 = Graphics[{Red, Thick, Circle[{2, 0}, 0.05]}, AspectRatio -> 1/2]
a3 = Graphics[{Red, Arrowheads[0.05], Arrow[{{3, -1}, {4, -1}}]}]
ac = Graphics[{Red, Thick, Circle[{-1, -1}, 0.05]}, AspectRatio -> 1/2]
ac1 = Graphics[{Red, Thick, Circle[{1, -1}, 0.05]}, AspectRatio -> 1/2]
ac3 = Graphics[{Red, Thick, Circle[{3, -1}, 0.05]}, AspectRatio -> 1/2]
c4 = Graphics[{Red, Thick, Circle[{4, 0}, 0.05]}, AspectRatio -> 1/2]
b4 = Graphics[{Red, Arrowheads[0.05], Arrow[{{5, 1}, {4, 1}}]}]
Show[a, b, c, c2, b2, a1, a3, ac, ac1, ac3, c4, b4]
Therefore, we define a periodic extension of signum function:
fbas[x_] := -1 /; -L <= x < 0 (*square wave*)
fbas[x_] := 1 /; 0 <= x < L
L = 1;
f[x_] := fbas[Mod[x, 2*L, -L]]
SetOptions[Plot, ImageSize -> 300];
Plot[f[x], {x, -3*L, 3*L}, PlotRange -> {-1.2, 1.2}, AxesLabel -> {"x", "f(x)"}, PlotStyle -> Thickness[0.008]]
The function Mod[x, 2*L, -L] (1) adds L to x, (2) calculates the remainder on dividing (1) by 2*L, (3) subtracts L from (2). This has the effect of shifting the original x by ±2L until it falls into the range [-L,L). This modified x is then used as the argument of the original function f[x].

We plot the periodically extended function over three periods -- that is over [-3L, 3L].
Strictly speaking the vertical lines shown on the graph at the discontinuity are not part of the graph. In some ways they make it easier to visualize the discontinuous function, but on those days when we are feeling fussy, so we can exclude those segments. This is done by an Option for plot called Exclusions. The plot without the vertical lines is constructed below. The argument of the Option Exclusions is a list of points to be excluded.

Plot[f[x], {x, -3*L, 3*L}, PlotRange -> {-1.2, 1.2}, AxesLabel -> {"x", "f(x)"}, Exclusions -> {-2.0, -1.0, 0.0, 1.0, 2.0}, Epilog -> {Red, PointSize[Medium], Point[{{-2, 0}, {-1.0, 0}, {0, 0}, {1, 0}, {2, 0}, {3, 0}}]}]
Then we plot Fourier approximation with 20 terms:
f = FourierTrigSeries[Sign[x], x, 20, FourierParameters -> {1, Pi}]
A=Plot[f, {x, -5, 5}, PlotStyle -> {Thick, Orange}, AspectRatio -> 2/5]
cm1 = Graphics[{Red, Thick, Disk[{-1, 0}, 0.07]}, AspectRatio -> 1/4]
Show[A, c4, c3, c2, c1, c0, cm1, cm2, cm3, cm4]
because Fourier coefficients are
$a_k = \int_{-1}^1 \mbox{sign} (x)\,\cos \left( k\pi x \right) {\text d}x = 0, \qquad b_k = \int_{-1}^1 \mbox{sign} (x)\,\sin \left( k\pi x \right) {\text d}x = \frac{2}{k\pi} \left[ 1- (-1)^k \right] = \frac{4}{k\pi} \times \begin{cases} 1, & \ \mbox{if k is odd}, \\ 0, & \ \mbox{if k is even} . \end{cases}$
Then
$\mbox{sign}(x) = \frac{4}{\pi}\, \sum_{n\ge 1} \frac{1}{2n-1} \,\sin (2n-1)\pi x , \qquad -1 \le x \le 1.$
• $$f_2 (x) = x^2 \,: \, (1,3] \mapsto \, \mathbb{R})$$ We plot its periodic extension:
fbas[x_] := x^2 /; 1 <= x < 3
f[x_] := fbas[Mod[x, 2, 1]]
SetOptions[Plot, ImageSize -> 300];
Plot[f[x], {x, -3, 4}, PlotRange -> {-1.2, 9.2}, AxesLabel -> {"x", "f(x)"}, PlotStyle -> Thickness[0.008]]
Since on symmetrical interval $$(-1,1)$$ the given function can be written as $$f(x) = (x+2)^2 ,$$ we find Fourier coefficients and build its Fourier approximation (with 20 terms)
a0 = Integrate[(x + 2)^2 , {x, -1, 1}]
an = Integrate[Cos[n*Pi*x]*(x + 2)^2 , {x, -1, 1}, Assumptions -> Element[n, Integers]]
bn = Integrate[Sin[n*Pi*x]*(x + 2)^2 , {x, -1, 1}, Assumptions -> Element[n, Integers]]
fourier[m_] = a0/2 + Sum[((4/n^2/Pi^2)*Cos[n*Pi*x] - (8/n/Pi)*Sin[n*Pi*x])*(-1)^ n, {n, 1, m}]
Plot[{fourier[20], f[x]}, {x, -2, 2}, PlotStyle -> {{Thick, Blue}, {Thick, Orange}}]
$f_2 (x) = \frac{13}{3} + \sum_{n\ge 1} \left[ \frac{4}{n^2 \pi^2} \, \cos \left( n\pi x \right) - \frac{8}{n\pi} \,\sin \left( n\pi x \right) \right] (-1)^n .$
• $$f_3 (x) = x\,: \, (-1,1] \mapsto \, \mathbb{R})$$
• $$f_4 (x) = H(x)\,: \, (-1,1] \mapsto \, \mathbb{R}) ,$$ where H(x) is the Heaviside function restricted on the finite interval.
• $$f_5 (x) = |x|\,: \, (-1,1] \mapsto \, \mathbb{R})$$
• $$f_6 (x) = 1-|x|\,: \, (-2,2] \mapsto \, \mathbb{R})$$
• $$f_7 (x) = \lfloor x \rfloor \,: \, (-2,2] \mapsto \, \mathbb{R})$$

We describe periodic extension and its Fourier approximation in the following example.

Example: Consider the piecewise continuous function, defined on the interval $$[-2,2] .$$

$\left\{ \begin{array}{ll} 1 , & \ x<0 \\ x^2 , & \ x>0 \end{array} \right. \qquad\mbox{on the interval } (-2,2).$

Then we calculate its Fourier coefficients:

Clear[x,n,ak1,ak2,ak,bk1,bk2,bk,Sn]
ak1 = Integrate[1/2*Cos[n*Pi*x/2], {x,-2,0}];
ak2 = Integrate[1/2*x^2 * Cos[n*Pi*x/2], {x,0,2}];
bk1 = Integrate[1/2*Sin[n*Pi*x/2], {x,-2,0}];
bk2 = Integrate[1/2*x^2 * Sin[n*Pi*x/2], {x,0,2}];
ak = FullSimplify[ak1+ak2, Element[n, Integers]]
bk = FullSimplify[bk1+bk2, Element[n, Integers]]
a0 = FullSimplify[1/2* Integrate[1, {x,-2,0}] + 1/2 * Integrate[x^2 , {x,0,2}]]
Out[6]= (8 (-1 + (-1)^n) - (1 + 3 (-1)^n) n^2 $Pi]^2)/(n^3 \[Pi]^3) Out[7]= (8 (-1)^n)/(n^2 \[Pi]^2) Out[8]= 7/3 Now we define its periodic expansion. This could be achieved in many ways, and we give some approches to find a periodic expansion. f[x_] := Which[x > 2, f[x - 2*2], x < -2, f[x + 2*2], -2 < x < 0, 1 + x - x, 0 < x < 2, x^2] Plot[f[x], {x, -5, 5}, PlotStyle -> {Thick, Black}] Other options to define periodic function: f[x_] = Piecewise[{{1, x < 0}, {x^2, x > 0}}] Out[11]= \[ \left\{ \begin{array}{ll} 1 & \ x<0 \\ x^2 & \ x>0 \\ 0 & \ \mbox{True} \end{array} \right.$

We define a subroutine that provide periodic extension of a function:

myperiodic[func_, {val_Symbol, min_?NumberQ, max_?NumberQ}] := func /. (val :> Mod[val - min, max - min] + min)
Then we plot it:
Plot[myperiodic[f[x], {x, -2, 2}] // Evaluate , {x, -5, 5}, PlotStyle -> {Thick, Black}]
We demonstrate other options to determine a periodic extension on our old piecewise continuous function.
Clear[f];
f[x_] = Piecewise[{{1, x < 0}, {x^2, x > 0}}]
f[t_ /; t >= 2] := f[t - 4]; f[t_ /; t < -2] := f[t + 4]; Plot[f[t], {t, -10, 10}] , PlotStyle -> {Thick}]
f[x_] = Piecewise[{{1, x < 0}, {x^2, x > 0}}]
g[x_] = f@Mod[x, 4 , -2] (* somehow slow *)
Plot[{f[t], g[t]}, {t, -6, 6}, PlotStyle -> Thick]

A periodic extension can be defined for arbitrary period as the following example shows.
T = 2.5
g[x_ /;0<= x <= T] := x^2 +1;
f[x_] := g[Mod[x, T]]
Plot[f[t], {t, -6, 6}, PlotRange -> {{-5,5}, {-0.1, 8}}, PlotStyle -> Thick]

Example: Consider the following function that we met previously

$f(x) = \left\{ \begin{array}{ll} \cos x, & \ \mbox{within interval [-\pi /2 , \pi /2 ]}, \\ 0 , & \ \mbox{outside the interval within } [-\pi , \pi ]. \end{array} \right.$

We can also define the periodic expansion of any function using Mod command. If we need to define a function with period $$2*\pi ,$$ we type:

periodicExtension[func_] := func[Abs[Mod[t, 2*Pi, -1*Pi]]]
per = periodicExtension[f]
Out[2] = { Cos[Abs[Mod[t,2*Pi , -Pi]]] -Pi/2 < Abs[Mod[t, 2*Pi , -Pi]] < Pi/2
0 True
Plot[per, {t, -10, 10}, PlotStyle -> Thick]
FourierTrigSeries[f[t], t, 5]
Out[6] = 1/\[Pi] + Cos[t]/2 + (2 Cos[2 t])/(3 \[Pi]) - (2 Cos[4 t])/(15 \[Pi])
Plot[%, {t, -3*Pi, 3*Pi}, PlotStyle -> Thick, AspectRatio -> 1/5]

 Complete

#### II. Plotting

Separable equations

Enter text here

Equations reducible to separable equations
Exact equations
Integrating Factors
Linear and Bernoulli equations
Riccati equation
Existence and Uniqueness
Qualitative analysis
Applications