Fouries Series


This tutorial was made solely for the purpose of education and it was designed for students taking Applied Math 0340. It is primarily for students who have some experience using Mathematica. If you have never used Mathematica before and would like to learn more of the basics for this computer algebra system, it is strongly recommended looking at the APMA 0330 tutorial. As a friendly reminder, don't forget to clear variables in use and/or the kernel.

Finally, the commands in this tutorial are all written in bold black font, while Mathematica output is in regular fonts. This means that you can copy and paste all commands into Mathematica, change the parameters and run them. You, as the user, are free to use the scripts to your needs for learning how to use the Mathematica program, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately.

Return to computing page for the first course APMA0330
Return to computing page for the second course APMA0340
Return to Mathematica tutorial for the first course APMA0330
Return to Mathematica tutorial for the second course APMA0340
Return to the main page for the course APMA0340
Return to the main page for the course APMA0330
Fourier series is a particular case of a more general orthogonal expansion with respect to eigenfunctions of differential operators. We start with the classical Fourier expansion of a periodic function and then present some important expansions that are widely used in applications.

Fourier Series


A Fourier series is a way to represent a function as the sum of simple sine waves. More formally, a Fourier series is a way to decompose a periodic function or periodic signal with a finite period \( 2\ell \) into an infinite sum of its projections onto an orthonormal basis that consists of trigonometric polynomials. Therefore, a Fourier series provides a periodic extension of a function initially defined on a finite interval of length \( 2\ell . \) They are named in honor of Jean-Baptiste Joseph Fourier (1768--1830), who used trigonometric series in representing solutions of partial differential equations, after preliminary investigations by Leonhard Euler (1707--1783), Jean le Rond d'Alembert (1717--1783), and Daniel Bernoulli (1700--1782).

There are two equivalent forms of Fourier series---one is complex form

\[ f(x) \,\sim\, \sum_{k=-\infty}^{\infty} \alpha_k e^{k{\bf j} \pi x/\ell} \]
where
\[ \alpha_k = \frac{1}{2\ell} \int_{-\ell}^{\ell} f(x)\, e^{-k{\bf j} \pi x/\ell} \,{\text d} x , \qquad k=0, \pm 1, \pm 2, \ldots ; \]

and another is trigonometric form

\[ f(x) \,\sim\, \frac{a_0}{2} + \sum_{k=1}^{\infty} \left[ a_k \cos \frac{k \pi x}{\ell} + b_k \sin \frac{k \pi x}{\ell} \right] , \]
where coefficients are defined according to Euler-Fourier formulas (they were discovered by Leonhard Euler many years before Fourier used them in his research):
\begin{align*} a_0 &= \frac{1}{\ell} \int_{-\ell}^{\ell} f(x)\,{\text d} x , \\ a_k &= \frac{1}{\ell} \int_{-\ell}^{\ell} f(x)\, \cos \frac{k \pi x}{\ell} \,{\text d} x , \qquad k= 1, 2, 3, \ldots ; \\ b_k &= \frac{1}{\ell} \int_{-\ell}^{\ell} f(x)\, \sin \frac{k \pi x}{\ell} \,{\text d} x , \qquad k= 1, 2, 3, \ldots . \end{align*}

The trigonometric form is based on Euler's formulas:

\[ \sin \theta = \frac{1}{2{\bf j}} \,e^{{\bf j}\theta} - \frac{1}{2{\bf j}} \,e^{-{\bf j}\theta} = \Im \,e^{{\bf j}\theta} = \mbox{Im} \,e^{{\bf j}\theta}, \qquad \cos \theta = \frac{1}{2} \,e^{{\bf j}\theta} - \frac{1}{2} \,e^{-{\bf j}\theta} = \Re \,e^{{\bf j}\theta} = \mbox{Re} \,e^{{\bf j}\theta}. \]

Here, j is the unit vector in the positive vertical direction on the complex plane, so \( {\bf j}^2 =-1. \) Mathematica has four default commands to calculate Fourier series:

FourierSeries        (* to calculate complex coefficient expansion *)
FourierTrigSeries  (* to calculate standard Fourier expansion via sine and cosine *)
FourierCosSeries (* to calculate cosine Fourier series *)
FourierSinSeries   (* to calculate sine Fourier series *)

 

Sometimes, it is convenient to use polar form:

\[ f(x) \,\sim\, \frac{a_0}{2} + \sum_{k=1}^{\infty} A_k \sin \left( \frac{k \pi x}{\ell} + \phi_k \right) = \frac{a_0}{2} + \sum_{k=1}^{\infty} A_k \cos \left( \frac{k \pi x}{\ell} - \varphi_k \right) , \]
where \( A_k = \sqrt{a_k^2 + b_k^2} \quad\mbox{and} \quad \varphi_k = \arctan \left( b_k / a_k \right) , \quad \phi_k = \arctan \left( a_k / b_k \right) . \)

Before we get into the topic of convergence, we need to define first a couple of terms that we’ll run into in the rest of the section. First, we say that f(x) has a jump discontinuity at x = a if the limit of the function from the left, denoted f(a-0) or f(a-) and the limit of the function from the right, denoted f(a+0) or f(a+), both exist and \( f(a+0) \ne f(a-0) . \) A function f(x) is called piecewise continuous on an interval [a,b], if it is continuous on {a,b] except for at most finitely many points \( x_1 , x_2 , \ldots , x_n \) at each of them the function has both the left-hand and right-hand limits: \( f(x_k -0) \ \mbox{ and }\ f(x_k +0) , \quad k=1,2,\ldots , n . \)

Next, we say that f(x) is piecewise smooth if the function can be broken into distinct pieces and on each piece both the function and its derivative, f'(x), are continuous. A piecewise smooth function may not be continuous everywhere; however, the only discontinuities that are allowed are a finite number of jump discontinuities.

Example. The function

\[ f(x) = \begin{cases} 2x , & \ \mbox{ if } 0 < x < 1, \\ 1, & \ \mbox{ if } 1 < x < 2, \end{cases} \]
is piecewise smooth on [0,2], but is not continuous on [0,2]. However, \( f(x) = |x|^{1/2} \) is continuous on [-1,1], but is not piecewise smooth on [-1,1]. ■

The finite sum
\[ F_N (x) = \frac{a_0}{2} + \sum_{k=1}^{N} \left[ a_k \cos \frac{k \pi x}{\ell} + b_k \sin \frac{k \pi x}{\ell} \right] \]
is called a trigonometric polynomial of order N. There are three known (another one will be considered later in Cesàro section) kinds of convergence related to the Fourier series: It is obvious from the definition that uniform convergence is the strongest of the three, since uniformly convergent series will clearly converge pointwise, as well as in the L2 sense. The converse is not true, since not every pointwise or L2 convergent series is uniformly convergent. Between the pointwise and L2 convergence, neither is stronger than the other, since there are series that converge pointwise, but not in L2, and vice versa.

Theorem: Let f(x) be a square integrable function on the interval \( [- \ell , \ell ] , \) that is, \( \| f \|^2 = \int_{_\ell}^{\ell} |f(x)|^2 {\text d}x < \infty . \) Then the Fourier series converges to f(x) in the mean square sense. ■

Andrey Kolmogorov (1903--1987) from Moscow University (Russia), as a student at the age of 19, in his very first scientific work, constructed an example of an absolutely integrable function whose Fourier series diverges almost everywhere (later improved to diverge everywhere).

The first sufficient conditions for the pointwise convergence of the Fourier series were discovered by a German mathematician Johann Peter Gustav Lejeune Dirichlet (1805--1859).

Theorem (P. Dirichlet, 1829): Suppose that f(x) is a piecewise monotone (or has finite variation) periodic real-valued function with period \( 2 \ell . \) Then the function f(x) is represented by a convergent to \( \frac{1}{2} \left( f(a-0) + f(a+0) \right) \) Fourier series at every point a.

Theorem: Assume that

\[ F(x) \,=\, \frac{a_0}{2} + \sum_{k= 1}^{\infty} a_k \cos \left( \frac{k \pi x}{\ell} \right) + b_k \sin \left( \frac{k \pi x}{\ell} \right) \]
is the Fourier series for a piecewise continuous function f(x) over \( [- \ell , \ell ] . \) If its derivative f'(x) is piecewise continuous on the interval \( [- \ell , \ell ] \) and has both a left- and right-hand derivative at each point in this interval, then F(x) is pointwise convergent for all \( x \in [- \ell , \ell ] . \) The relation \( F(x) = f(x) \) holds at all points \( x \in [- \ell , \ell ] , \) where f(x) is continuous. If x = a is a point of discontinuity of f, then
\[ F(x) \,=\, \frac{f(a+0)+ f(a-0)}{2} , \]
where f(a-0) and f(a+0) denote the left- and right-hand limits, respectively.

Theorem: Suppose that \( \left\{ (x_k, y_k ) \right\}_{k=0}^N \) are N+1 points, where \( y_k = f(x_k ) , \) and the abscissas are equally spaced:

\[ x_k = -\ell + \frac{2\ell \,k}{N} \qquad \mbox{for} \quad k=0,1,2,\ldots N. \]
If f(x) is periodic with period \( 2\ell \) and 2M < N, then there exists a trigonometric polynomial
\[ F_M (x) = \frac{a_0}{2} + \sum_{j=1}^M \left[ a_j \cos \left( j\pi x \right) + b_j \sin \left( j\pi x \right) \right] \]
that minimizes the quantity
\[ \sum_{k=1}^N \left\vert f(x_k ) - F_M (x_k ) \right\vert^2 . \]
The coefficients ak and bk of this trigonometric polynomial are computed with the formulas:
\begin{align*} a_j &= \frac{2}{N} \,\sum_{k=1}^N f(x_k ) \,\cos \left( j\,\pi \,x_k \right) \qquad\mbox{for}\quad j=0,1,\ldots , M , \\ b_j &= \frac{2}{N} \,\sum_{k=1}^N f(x_k ) \,\sin \left( j\,\pi\, x_k \right) \qquad\mbox{for}\quad j=1,2,\ldots , M . \end{align*}

Example. Consider the function \( f (x) = x^2 \) on the interval [-1,1]. Let us take 16 equally spaced points \( x_k = -1 + \frac{2k}{15} , \quad k=0,1,2,\ldots , 15. \) Suppose we want to find the trigonometric polynomial approximation for M = 6 to the 15 data points \( \left\{ (x_k, y_k ) \right\}_{k=1}^{15} . \)

Since the periodic extension is assumed, at a point of discontinuity x = 1, the function value f(1) must be computed using the formula

\[ f(1) = \frac{f(1-0) + f(1+0)}{2} = \frac{1+1}{2} =1. \]
The function \( f (x) = x^2 \) is an even continuous function that is extended periodically; hence the coefficients for the sine terms are all zero (i.e. \( b_j =0 \) for all j). The trigonometric polynomial of degree M = 6 involves only the cosine terms, and we get
\[ a_j = \frac{2}{15} \,\sum_{k=1}^{15} \left( \frac{2k}{15} -1 \right)^2 \cos \left( j\,\pi\, x_k \right) \qquad\mbox{for}\quad j=0,1,2,\ldots , 6 . \]
Now we ask Mathematica for help:
Do[a[j_] := (2/15)* Sum[(2*k/15 - 1)^2 *Cos[j*Pi*(2*k/15 - 1)], {k, 1, 15}], {j, 0, 6}]
Then we calculate the trigonometric polynomial and plot it.
C6[x_] := a[0]/2 + Sum[a[j]*Cos[j*Pi*x], {j, 1, 6}]
Plot[{x^2 , FC6[x]}, {x, -1, 1}, PlotRange -> {-0.1, 1}, PlotStyle -> {{Thick, Blue}, {Thick, Orange}}]
As we see from the above graph, six term approximation gives relatively good representation of the given function. ■

Computing an integral in Mathematica is fairly painless, and it's tempting to simply use a partial Fourier sum depending to the number of terms n. Let's consider the following half-wave rectifier:

f[t_] = Piecewise[{{Cos[t], -Pi/2 < t < Pi/2}, {0, -Pi < t < -Pi/2 && Pi/2 < t < Pi}}]
Out[1]= { Cos[t] - Pi/2 < t < Pi/2
             { 0 True
Plot[f[t], {t, -Pi, Pi}, PlotStyle -> Thick]

Mathematica will treat the function f[t] outside the interval [-π , π ] as zero

Plot[f[t], {t, -3*Pi, 3*Pi}, PlotStyle -> Thick]

As we see, Mathematica rescales vertical and horizontal lines so that the picture becomes almost symmetric. You can return to the proper picture by using AspectRatio option:

Plot[f[t], {t, -3*Pi, 3*Pi}, PlotStyle -> Thick, AspectRatio -> 1/3/Pi, Ticks -> {True, None}]

So we get exactly the same graph. Now we are ready to expand this function into Fourier series by finding its Fourier coefficients:

\begin{align*} a_0 &= \frac{1}{\pi} \int_{-\pi /2}^{\pi /2} \cos x\,{\text d} x = \frac{2}{\pi} , \\ a_k &= \frac{1}{\pi} \int_{-\pi /2}^{\pi /2} \cos x\, \cos \left( k\, x \right) {\text d} x = -\frac{2}{\pi}\, \frac{1}{k^2 -1} \, \cos \frac{k\pi}{2} , \qquad k= 2, 3, \ldots ; \\ b_k &= \frac{1}{\pi} \int_{-\pi /2}^{\pi /2} \cos x\, \sin \left( k\, x \right) {\text d} x =0, \qquad k= 1, 2, 3, \ldots . \end{align*}
Then the corresponding Fourier series becomes
\[ f(t) \,=\,\frac{1}{\pi} + \frac{1}{2}\, \cos t - \frac{2}{\pi} \, \sum_{k\ge 1} \frac{1}{k^2 -1} \,\cos \left( k\, x \right) . \]
A partial sum with N = 20 terms gives a very good approximation:

We show how these commands work with other examples.

Example: We consider a piecewise continuous function that cannot be extended into Taylor series because it is identically zero on the interval (1,2). However, we can find its Fourier series.

f[x_] = Piecewise[{{1 - x, 0 < x < 1}, {0, 1 < x < 2}}]
Out[2]= { 1-x, if 0<x<1 and 0 for 1<x<2.

The standard Mathematica command FourierTrigSeries provides you the Fourier series of the function that is extended by zero outside the given interval (0<x<2 in our case):

FourierTrigSeries[f[x], x, 3]
Out[2]= 1/(4 \[Pi]) + (2 Cos[x] Sin[1/2]^2)/\[Pi] + (Cos[2 x] Sin[1]^2)/(2 \[Pi]) + (2 Cos[3 x] Sin[3/2]^2)/(
9 \[Pi]) + ((2 - 2 Sin[1]) Sin[x])/(2 \[Pi]) - ((-2 + Sin[2]) Sin[2 x])/(4 \[Pi]) - ((-3 + Sin[3]) Sin[3 x])/(9 \[Pi])

We can plot the partial sum with say 50 terms:

curve = FourierTrigSeries[f[x], x, 50];
Plot[curve, {x, -1.5, 3.5}, PlotRange -> 1]

However, it is not what we want: we need the Fourier series of the function f[x] extended periodically from the interval of length 2:

FourierTrigSeries[f[x], x, 3, FourierParameters -> {1, Pi}]
Out[3]= 1/4 + (2 Cos[\[Pi] x])/\[Pi]^2 + (2 Cos[3 \[Pi] x])/(9 \[Pi]^2) + Sin[\[Pi] x]/\[Pi] + Sin[2 \[Pi] x]/(2 \[Pi]) + Sin[3 \[Pi] x]/( 3 \[Pi])

To check, we find the Fourier coefficients manually:

Integrate[f[x]*Cos[n*Pi*x], {x, 0, 2}, Assumptions -> Element[n, Integers]]
Out[4]= (1 - Cos[n \[Pi]])/(n^2 \[Pi]^2)
So
\[ a_0 = \frac{1}{2} , \qquad a_n = \frac{1- (-1)^n}{n^2 \pi^2} , \quad n=1,2,\ldots . \]
Integrate[f[x]*Sin[n*Pi*x], {x, 0, 2}, Assumptions -> Element[n, Integers]]
Out[5]= (n \[Pi] - Sin[n \[Pi]])/(n^2 \[Pi]^2)

which is 1/(n*π). Hence, its Fourier series becomes

\[ f(x) = \frac{1}{4} + \sum_{n\ge 1} \left[ \frac{1- (-1)^n}{n^2 \pi^2} \, \cos \left( n \pi x \right) + \frac{1}{n\pi}\, \sin \left( n \pi x \right) \right] \]
We can also represent f(x) in other forms:
\[ f(x) = \frac{1}{4} + \sum_{n\ge 1} \sqrt{ \left( \frac{1- (-1)^n}{n^2 \pi^2} \right)^2 + \frac{1}{n^2 \pi^2}} \, \sin \left( n \pi x + \arctan \frac{1- (-1)^n}{n \pi} \right) = \frac{1}{4} + \sum_{n\ge 1} \sqrt{ \left( \frac{1- (-1)^n}{n^2 \pi^2} \right)^2 + \frac{1}{n^2 \pi^2}} \, \cos \left( n \pi x - \mbox{arccot} \frac{1- (-1)^n}{n \pi} \right) . \]
Now we plot some partial sums, starting with the given function itself:

Plot[f[x], {x, -1, 3}, PlotRange -> {0, 1}, AspectRatio -> Automatic, Axes -> {True, False}, PlotStyle -> Thick]

Then we plot some finite sums:

curve5Pi = FourierTrigSeries[f[x], x, 5];
Plot[%, {x, -1, 3}]

Then we repeat calculations with 50 terms:


Next we plot the correct Fourier series partial sums:

curve5=FourierTrigSeries[f[x], x, 50, FourierParameters -> {1, Pi}];
Plot[%, {x, -1, 3}]

 

The option FourierParameters has two parameters and when applied, it looks as FourierParameters->{a,b}
In trigonometric form, with setting \( {\bf FourierParameters} \,-> \,\{ a, b \} \) the following series is returned:

\[ \left\vert \frac{b}{\pi} \right\vert^{(1+a)/2} \left[ \frac{a_0}{2} + \sum_{k=1}^n \left( a_k \,\cos (bkt) + b_k \,\sin (bkt) \right) \right] , \]
with
\[ a_k = \left\vert \frac{b}{\pi} \right\vert^{(1+a)/2} \,\int_{-\pi/|b|}^{\pi/|b|} f(t)\,\cos (bkt) \,{\text d}t , \qquad k=0,1,2,\ldots , \]
\[ b_k = \left\vert \frac{b}{\pi} \right\vert^{(1+a)/2} \,\int_{-\pi/|b|}^{\pi/|b|} f(t)\,\sin (bkt) \,{\text d}t , \qquad k=1,2,\ldots . \]

Syntax for the FourierSeries command is: FourierSeries [ function , variable , number of terms ]

Changing the FourierParameters setting allows control over the limits of integration on the coefficients, and therefore, the base frequency of the series. Some work will go into calculating what value for “b” will give the proper limits of integration. There is ordinarily no reason to change “a” from its default setting, which is 1.

\[ \cos \left( a\,\sin x \right) = J_0 (a) + \sum_{k\ge 1} J_{2k} (a) \,\cos \left( 2kx \right) . \]