Fourier Series

Global Assumptions: Important!!

Mathematica does not think of n as an integer, so it cannot simplify certain quantities:

Simplify[Cos[n π]]

It's useful to set global assumptions, which we can do using the following command:

$Assumptions=Element[n,Integers]

Now, Mathematica can simplify since it knows that n is an integer:

Simplify[Cos[n π]]

If you want to see what assumptions are active, you can check using:

$Assumptions

You can set a bunch of assumptions at once:

$Assumptions={Element[{m,n},Integers],a>0, Element[a,Reals]}

$Assumptions

The following limit is zero since it is a decaying exponential when a>0:

Limit[Exp[-a t],t->Infinity]

but this limit cannot be evaluated since we don't know if m is positive or negative.

Limit[Exp[-m t],t->Infinity]

Danger!

We must be careful when using global assumption. Consider the following:

Simplify[(∫_(-π)^π)(1-Sin[x])Sin[n x]\[DifferentialD]x]

Which is actually WRONG if n=1:

Simplify[(∫_(-π)^π)(1-Sin[x])Sin[1 x]\[DifferentialD]x]

What happened? Mathematica simplified the numerator before the denominator, and missed that it actually had an indeterminant form. Clear the Assumptions and try the integral again to see this.

$Assumptions=True
Simplify[(∫_(-π)^π)(1-Sin[x])Sin[n x]\[DifferentialD]x]

From this form we see the integral is indeterminant if n=±1, and zero for all other values of n.

Bottom Line--if Mathematica seems to be doing something weird or unexpected, remove any global assumptions you have set and start over.

Fourier Series for f(x)=(e^(|x|)), -π≤x≤π.

Find the Fourier series for f(x) = (e^(|x|)), -π≤x≤π.

The function f is even, so expect all (Subscript[b, n])=0.

(Subscript[a, 0]):=(1/(2π))(∫_(-π)^π)f[x]\[DifferentialD]x
(Subscript[a, n_]):=Simplify[(1/π)(∫_(-π)^π)f[x]Cos[n x]\[DifferentialD]x]
(Subscript[b, n_]):=Simplify[(1/π)(∫_(-π)^π)f[x]Sin[n x]\[DifferentialD]x]
fourier[x_,k_]:=(Subscript[a, 0])+(∑_(n=1)^k)((Subscript[a, n])Cos[n x]+(Subscript[b, n])Sin[n x])

f[x_]=Exp[Abs[x]]
(Subscript[a, 0])
(Subscript[a, n])
(Subscript[b, n])
approx[x_]=fourier[x,5]

Do[

approx[x_]=fourier[x,k];
plot[k]=Plot[{f[x],approx[x]},{x,-π,π},PlotStyle->{Thick},PlotRange->{All,{-5,25}}]

,{k,1,10,1}]

Manipulate[Show[plot[k]],{k,1,10,1}]

Derivation of Fourier Transform Coefficients by hand

This is all about doing integrals using techniques from Calculus II. It's a bit tedious, but straight-forward.

(Subscript[a, 0])=(1/(2π))(∫_(-π)^π)(e^(|x|))dx
=(1/(2π))((∫_(-π)^0)(e^(-x))dx+ (∫_0^π)(e^x)dx)
=(1/(2π))((Subscript[(-(e^(-x))), -π]^0)+ (Subscript[((e^x)), 0]^π))
=(1/(2π))(-(e^0)+(e^π)+ (e^π)-(e^0))
=(1/π)(-1+(e^π))

(Subscript[a, 0])

The others require the parts-> parts-> algebra technique to solve, which yield the two useful integral formulas:

∫(E^(a x))Cos[ b x]\[DifferentialD]x

∫(E^(a x))Sin[ b x]\[DifferentialD]x

We'll do th (Subscript[a, n]) integral, and leave it at that since (Subscript[b, n]) would be similar.

(Subscript[a, n])=(1/π)(∫_(-π)^π)(e^(|x|))Cos[n x]dx
=(1/π)((∫_(-π)^0)(e^(-x))Cos[n x]dx+(∫_0^π)(e^x)Cos[n x]dx)
=(1/π)((Subscript[((((e^(- x)) (- Cos[n x]+n Sin[n x]))/((((-1))^2)+(n^2)))), -π]^0)+(Subscript[((((e^( x)) (a Cos[n x]+n Sin[n x]))/((((1))^2)+(n^2)))), 0]^π))
=(1/(π(1+(n^2))))((Subscript[((e^(- x)) (- Cos[n x]+n Sin[n x])), -π]^0)+(Subscript[((e^( x)) (a Cos[n x]+n Sin[n x])), 0]^π))
=(1/(π(1+(n^2))))(((e^0) (- Cos[0]+n Sin[0]))-((e^π) (- Cos[-nπ]+n Sin[-nπ]))+((e^( π)) (Cos[nπ]+n Sin[n π]))-((e^( 0)) (Cos[0]+n Sin[0])))
=(1/(π(1+(n^2))))( - 1+(e^π)  Cos[-nπ]+(e^( π)) a Cos[nπ]-1)
=(1/(π(1+(n^2))))( - 2+2(e^π) (((-1))^n))
=(( - 2+2(e^π) (((-1))^n))/(π(1+(n^2))))

(Subscript[a, n])

Fourier Series for f as the Sawtooth Function

(Subscript[a, 0]):=(1/(2π))(∫_(-π)^π)f[x]\[DifferentialD]x
(Subscript[a, n_]):=Simplify[(1/π)(∫_(-π)^π)f[x]Cos[n x]\[DifferentialD]x]
(Subscript[b, n_]):=Simplify[(1/π)(∫_(-π)^π)f[x]Sin[n x]\[DifferentialD]x]
fourier[x_,k_]:=(Subscript[a, 0])+(∑_(n=1)^k)((Subscript[a, n])Cos[n x]+(Subscript[b, n])Sin[n x])

f[x_]=(1/2) (π-x)+π Floor[(x/(2π))]
(Subscript[a, 0])
(Subscript[a, n])
(Subscript[b, n])
approx[x_]=fourier[x,5]

Do[

approx[x_]=fourier[x,k];
plot[k]=Plot[{f[x],approx[x]},{x,-2π,2π},PlotStyle->{Thick},PlotRange->{All,{-1.9,1.9}}]

,{k,1,10,1}]

Manipulate[Show[plot[k]],{k,1,10,1}]

Fourier Series for f as the Triangular Wave Function

(Subscript[a, 0]):=(1/(2π))(∫_(-π)^π)f[x]\[DifferentialD]x
(Subscript[a, n_]):=Simplify[(1/π)(∫_(-π)^π)f[x]Cos[n x]\[DifferentialD]x]
(Subscript[b, n_]):=Simplify[(1/π)(∫_(-π)^π)f[x]Sin[n x]\[DifferentialD]x]
fourier[x_,k_]:=(Subscript[a, 0])+(∑_(n=1)^k)((Subscript[a, n])Cos[n x]+(Subscript[b, n])Sin[n x])

f[x_]=2Abs[(1/2) (π-x)+π Floor[(x/(2π))]]
(Subscript[a, 0])
(Subscript[a, n])
(Subscript[b, n])
approx[x_]=fourier[x,5]

Do[

approx[x_]=fourier[x,k];
plot[k]=Plot[{f[x],approx[x]},{x,-2π,2π},PlotStyle->{Thick},PlotRange->{All,{-.5,3.3}}]

,{k,1,10,1}]

Manipulate[Show[plot[k]],{k,1,10,1}]