CITE THIS NOTEBOOK: Studying periodic functions in three different ways by Athanasios Paraskevopoulos. Wolfram Community JAN 31 2023.

Introduction

The Fourier series is a tool for solving partial differential equations. It is used most often when the system in question has periodic behavior. The basic idea is similar to the power series – a periodic function is modeled as an infinite sum of sine and cosine functions. Like with the power series, the more terms in the series that we use, the closer the series converges onto the original function.
​
Another use for the Fourier series is to analyze the frequencies present in a system. Because each coefficient is related to a particular integer multiple of some base frequency, they give an indication how much each frequency contributes to the overall function.
The computation and study of Fourier series is known as harmonic analysis and is extremely useful as a way to break up an arbitrary periodic function into a set of simple terms that can be plugged in, solved individually, and then recombined to obtain the solution to the original problem or an approximation to it to whatever accuracy is desired or practical .
The definition of a Fourier series can get complicated, so it is instructive to begin with a simple case . Let f[x] be a periodic function with period { L} . thus :
f[x]=
a
0
+
∞
∑
n=1

a
n
Cos
2nπx
L
+
b
n
Sin
2nπx
L

a
0
=
1
L
L
∫
0
f[x]x
a
n
=
2
L
L
∫
0
f[x]Cos
2nπx
L
x
b
n
=
2
L
L
∫
0
f[x]Sin
2nπx
L
x

Experimental study in Mathematica

Let’s consider that we have the following periodic function
In[]:=
f[x_]:=Which[-2<x<0,x+2,0<x<2,2-2x]​​P1=Plot[f[x],{x,-2,2},PlotStyle->Thick]
Out[]=
-2
-1
1
2
-2
-1
1
2
In[]:=
L=4;(*period*)​​a[n_]:=(2/L)*Integrate[f[x]*Cos[2n*Pi*x/L],{x,-L/2,L/2}]​​a[0]:=(1/L)*Integrate[f[x],{x,-L/2,L/2}]​​b[n_]:=(2/L)*Integrate[f[x]*Sin[2n*Pi*x/L],{x,-L/2,L/2}]​​F[x_,Nmax_]:=a[0]+Sum[a[n]*Cos[2n*Pi*x/L]+b[n]*Sin[2n*Pi*x/L],{n,1,Nmax}]​​p[Nmax_,a_]:=Plot[Evaluate[F[x,Nmax]],{x,-a,a},PlotRange->All,PlotPoints->200,PlotStyleOrange]​​f[x_]:=If[x>0,2-2x,x+2];​​a[n]​​a[0]​​b[n]​​Simplify[%,n∈Integers]​​
Out[]=
-
2(-3+3Cos[nπ]+nπSin[nπ])
2
n
2
π
Out[]=
1
2
Out[]=
4nπCos[nπ]-4Sin[nπ]
2
2
n
2
π
Out[]=
2
n
(-1)
nπ
To illustrate multiple partial sums of an increasing number of terms will be graphed .
In[]:=
​​curve1=p[5,2]​​curve2=p[10,2]​​curve3=p[15,2]​​curve4=p[20,2]
Out[]=
-2
-1
1
2
-1.5
-1.0
-0.5
0.5
1.0
1.5
2.0
Out[]=
-2
-1
1
2
-2
-1
1
2
Out[]=
-2
-1
1
2
-2
-1
1
2
Out[]=
-2
-1
1
2
-2
-1
1
2
Bellow we observe the differences the plot of the initial function and its approximations.
In[]:=
Show[P1,curve1,PlotRange->All]
Out[]=
-2
-1
1
2
-2
-1
1
2
In[]:=
Show[{P1,curve2},PlotRange->All]
Out[]=
-2
-1
1
2
-2
-1
1
2
In[]:=
Show[{P1,curve3},PlotRange->All]
Out[]=
-2
-1
1
2
-2
-1
1
2
And a more interactive representation

MSE (mean square error)

We calculate the MSE (mean square error) to compare the performance of the approximation given by Fourier series against the real function .
We can see that mean square error goes down as more terms are added. Most of the error comes due to Gibbs. In theory, there will always be such an error at the ends, and this Gibbs error can’t be eliminated no matter how many terms are used.
​
Our next step is to study Fourier series at specific x points of partition 10, then one way is (note the definition of f(x) is changed a little below from the question in order to include the end points and zero.
​
For example for 10 partitions, using 7 number of terms
We could also to add error column (pointwise error, not MSE error)

FourierTrigSeries

Now we will calculate the expansion of the Fourier series with the command and create its plot
What do you notice?

FourierSeries

Now we are going to find the the 5th,10th,15th,20th Fourier coefficients, choosing the right FourierParameters
We want to find the coefficient of the general term:
Users should be aware that this command is for the complex exponential form .
Greetings :​
I would like to thank my coordinator Zach Shelton at the WSA program and the user Nasser from Mathematica Stack Exchange for their help and for solving my questions
REFERENCES :
1
.
https://mathworld.wolfram.com/FourierSeries.html
2
.
https://en.wikipedia.org/wiki/Fourier_series
3
.
Vafa Fakhri (2019) : Fourier Series programming with Wolfram Mathematica https://www.researchgate.net/publication/331687599_Fourier _Series _programming _with _Wolfram _Mathematica
4
.
Craig Beasley (2012) : Fourier Series in Mathematica https://classes.engineering.wustl.edu/jemt3170/Mathematica%20-%20 Fourier %20 Series.pdf
​