Fourier Series

Brown University, Applied Mathematics


Return to computing page
Return to MuPAD tutorial page for the first course
Return to MuPAD tutorial page for the second course
Return to the main page (APMA0340)

Fourier Series

Consider a periodic function \( f(x) \) with period \( T = 2\ell \). It can be expanded into Fourier series:
\[ f(x) \sim \frac{a_0}{2} + \sum_{n\ge 1} \left( a_n \cos \frac{n\pi x}{\ell} + b_n \sin \frac{n\pi x}{\ell} \right) ,\]

where the coefficients are defined according to Euler--Fourier formulas

\[ a_n = \frac{1}{\ell} \,\int_{-\ell}^{\ell} f(x)\, \cos \frac{n\pi x}{\ell} \, {\text d}x , \qquad n=0,1,2,\ldots , \] \[ b_n = \frac{1}{\ell} \,\int_{-\ell}^{\ell} f(x)\, \sin \frac{n\pi x}{\ell} \, {\text d}x , \qquad n=1,2,3,\ldots . \]

Let \( F_N (x) \) be the finite Fourier sum with N+1 terms:

\[ 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) \]
If a function f(x) has a discontinuity at the point \( x_0 \) of amount \( f(x_0 +0) - f(x_0 -0) \) then finite Fourier sums experience overshoot and undershoot in a neighborhood of this point. These undershoots or overshoots cannot be eliminated by increasing the the number of terms in the finite Fourier sum and they approach with \( N\mapsto \infty \) the value
\[ 1.1789797444721675\ldots \left\vert f(x_0 +0) - f(x_0 -0) \right\vert . \]
Such behavior of Fourier series is usually referred to as the Gibbs phenomenon, which was first noticed and analyzed by the English mathematician Henry Wilbraham (1825--1883) in 1848. The term "Gibbs phenomenon" was introduced by the American mathematician Maxime Bocher in 1906. The 'magic' number is the approximation of
\[ \frac{2}{\pi} \,\mbox{Si} (\pi ) \approx 1.1789797444721675\ldots \qquad\mbox{where } \mbox{Si} (x) = \int_0^x \frac{\sin t}{t} \,{\text d}t \]
is sine integral (special function), which is build-in MuPad as Si(x).

To clarify, we present two examples.

Example 1:

Consider a piecewise continuous function
\[ f(x) = \begin{cases} 1 , & \ \mbox{ if } -2 < x < 0, \\ x^2 , & \ \mbox{ if } 0 < x < 2. \end{cases} \]

This functions is assumed to be periodically extended with period \( T= 4 \) so \( \ell = 2 . \) Now we expand this function into the Fourier series using MuPaD:

f2:=1
f:=x^2
L:=2
a_0 := 1/L*(int(f2,x=-L..0)+int(f,x=0..L)):
cs := cos(k*PI*x/L):
sn := sin(k*PI*x/L):
a_k := simplify(1/L*(int(f2*cs,x=-L..0)+int(f*cs,x=0..L))):
b_k := simplify(1/L*(int(f2*sn,x=-L..0)+int(f*sn,x=0..L))):
Fourier := a_0/2 + sum(a_k*cs + b_k*sn,k=1..n):
Fplot:=evalAt(Fourier, n=5):
fpiece:=piecewise([x>-2 and x<0,1],[x>0 and x<2,x^2]): plot(fpiece,x=-2..2)

MuPAD graphics

 

Next we plot its finite sum approximations with \( N=5 , \ 10 \ \mbox{and}\ 30 \) terms.
plotfunc2d(fpiece, Fplot,x=-2..2)

MuPAD graphics

Fplot:=evalAt(Fourier, n=10): fpiece:=piecewise([x>-2 and x<0,1],[x>0 and x<2,x^2]): plotfunc2d(fpiece, Fplot,x=-2..2)

MuPAD graphics

Fplot:=evalAt(Fourier, n=30): fpiece:=piecewise([x>-2 and x<0,1],[x>0 and x<2,x^2]): plotfunc2d(fpiece, Fplot,x=-2..2)

MuPAD graphics

We can do the same job slightly different. First we define the piecewise function:
fofx:= piecewise([x<0,1],[x>0,x^2])
\[ \begin{cases} x^2 , & \ \mbox{ if } 0 < x < 2, \\ 1 , & \ \mbox{ if } -2 < x < 0. \end{cases} \]
plot(fofx,x=-2..2, LineWidth=1.1*unit::mm,Color = RGB::Blue)

MuPAD graphics

Next, we find the Fourier coefficients using the definition of each coefficient. We start with the first coefficient:
a0:=int(fofx,x=-2..2)/4
\( \displaystyle \frac{7}{6} \)

Defining n as a non negative integer

assume(n,Type::NonNegInt)
we calculate the Fourier coefficients:
an := simplify(int(fofx*cos(n*PI*x/2),x=-2..2)/2)
\( \displaystyle \frac{8 \,(-1)^n}{n^2 \pi^2} \)
bn := simplify(int(fofx*sin(n*PI*x/2),x=-2..2)/2)
\( \displaystyle -\frac{n^2 \pi^2 -8\,(-1)^n + 3\,(-1)^n n^2 \pi^2 +8}{n^3 \pi^3} \)

We can then define the summation for the Fourier series; to achieve this, we calculate the general term:

FourierSum1:=an*cos(n*PI*x/2)+bn*sin(n*PI*x/2)
To plot the function, we can use the summation function to tweak the n values.
Fourier1n5:=7/6+sum(FourierSum1,n=1..5):
plot(Fouriern5,fofx,x=-2..2)

MuPAD graphics

Similarly, we calculate and plot Fourier partial sums with \( n= 10 \) and \( n= 30 \) terms:
Fouriern10:=7/6+sum(FourierSum1,n=1..10):
plot(Fouriern10,fofx,x=-2..2)
and
Fouriern30:=7/6+sum(FourierSum1,n=1..30):
plot(Fouriern30,fofx,x=-2..2)
We can also plot the Cesaro partial sums for this Fourier series that reduces the Gibbs phenomenon.
Cesaro5:=7/6+sum((1-n/(5+1))*FourierSum1,n=1..5):
plot(Cesaro5,fofx,x=-2..2)

 

Points of discontinuity are at 4n (jump of 1) for \( n= 0, \pm 1, \pm 2, \ldots \) and 4k + 2 (jump of 3) for \( k= 0, \pm 1, \pm 2 \ldots . \)

fourierover:= 1*0.1789797/2
0.08948985
fournunder:= 1*0.1789797/2
0.08948985
fourplustwoover:=3*0.1789797/2
0.26846955
fourplustwounder:=3*0.1789797/2

0.26846955

Therefore, for example, at x=0, points are 1.08948985 and -0.08948985, and at x=2, points are 4.26846855 and 0.73153045. Therefore, for example, at x=0, points are 1.08948985 and -0.08948985, and at x=2, points are 4.26846855 and 0.73153045.

Example 2:

Let us choose \( g(x) = x^2 \) on the interval [0,2].

f := x^2:
f2 := (x+2)^2:
l := 1:
a_0 := 1/l*(int(f2,x=-1..0)+int(f,x=0..1)):
cs := cos(k*PI*x/l):
sn := sin(k*PI*x/l):
a_k := simplify(1/l*(int(f2*cs,x=-1..0)+int(f*cs,x=0..1))):
b_k := simplify(1/l*(int(f2*sn,x=-1..0)+int(f*sn,x=0..1))):
Fourier := a_0/2 + sum(a_k*cs + b_k*sn,k=1..n):
a := 100:
Cesaro := sum(Fourier,n=1..a)/a:
plot(Cesaro,x=-2..2)

Another approach:

f:=x->x;
L:=2;
a0:=int(f(x),x=-L..L)/(2*L)
assume(n,Type::NonNegInt)
an:=simplify(int(f(x)*cos(n*PI*x/L),x=-L..L)/L)
bn:=simplify(int(f(x)*sin(n*PI*x/L),x=-L..L)/L)
a:=m->subs(an,n=m);
a(0):=a0;
b:=m->subs(bn,n=m);
b(1)

Fourier series with N +1 terms:
S:=N->a(0) + sum(a(n)*cos(n*PI*x/L)+b(n)*sin(n*PI*x/L),n=1..N)
S(4)
plot(f,S(1),S(2),S(3),x=-L..L)
plot(f,S(10),x=-L..L)

Cesaro series:
C:=N->a(0) + (sum(a(n)*(N+1-n)*cos(n*PI*x/L)+b(n)*(N+1-n)*
sin(n*PI*x/L),n=1..N))/(N+1)
plot(f,C(10),x=-L..L)

Step-function on the interval [-1/2, 1/2]:
f:=piecewise([x<=-1/2,0],[x>-1/2 and x<1/2,1],[x>1/2,0])
plot(f,x=-1..1)

Another piecewise continuous function:
f:=piecewise([x<=-1/2,x],[x>-1/2 and x<1/2,x^2],[x>1/2,-x])
plot(f,x=-1..1)

 



Congrats! You now have just opened MuPAD. Click "Next" to continue.

Home

< Previous

Next >