Fourier Series

An example of a piecewise-defined function

Clear[f,L]
f[x_]:=(x+2)/;(-2≤x<0);
f[x_]:=(2-2x)/;(0≤x<2);
L=2;
Plot[f[x],{x,-L,L}]

Note that f is neither even nor odd, so we need all Fourier coefficients.

One proof that f is neither even nor odd:
	 f(1)=0 but f(-1)=1 so that 
	 neither f(1) = f(-1) 
	 nor f(1) = -f(-1).
Thus, we have found a value of x0 so that f(- (Subscript[x, 0])) is neither f( (Subscript[x, 0])) nor -f( (Subscript[x, 0])).

Alternatively, the graph of f is clearly neither symmetric wrt y-axis nor symmetric wrt origin. 
(This is more evidence than proof. I have not clearly demonstrated that I understand these terms.)

Fourier Coefficients

If f[x] is defined on [-L, L], its Fourier Series is 
	a[0]/2 + series(a[n] Cos[n π x/L), {n,1,∞}) + series(b[n] Sin[n π x/L], {n,1,∞}) 
with  coefficients 	a[n] = 1/L (∫_(-L)^L)f(x) Cos[n π x/L]dx, n≥ 0 and  
			b[n] = 1/L (∫_(-L)^L)f(x) Sin[n π x/L]dx, n≥1.

Since Cos[0 π x/L] = 1, we calculate a[0] separately.  These are similar to hand calculations.

Clear[a,b]
a[0]=(1/2)((∫_(-2)^0)(x+2)\[DifferentialD]x + (∫_0^2)(2-2x)\[DifferentialD]x)

a[n_]=(1/2)((∫_(-2)^0)(x+2)Cos[((n π x)/L)]\[DifferentialD]x + (∫_0^2)(2-2x)Cos[((n π x)/L)]\[DifferentialD]x)

b[n_]=(1/2)((∫_(-2)^0)(x+2)Sin[((n π x)/L)]\[DifferentialD]x + (∫_0^2)(2-2x)Sin[((n π x)/L)]\[DifferentialD]x)

Without simplification, here is the Fourier Series of f[x]

(1/2)(1) + (∑_(n=1)^∞)((1/2) (((4-4 Cos[n π])/((n^2) (π^2)))+((8-8 Cos[n π]-4 n π Sin[n π])/((n^2) (π^2)))) Cos[((n π x)/L)]) + (∑_(n=1)^∞)((1/2) (((4 (n π+n π Cos[n π]-2 Sin[n π]))/((n^2) (π^2)))+((4 (-n π+Sin[n π]))/((n^2) (π^2)))) Sin[((n π x)/L)])

partial sum for a Fourier Series

Clear[FSum,x,numTerms]
FSum[x_,numTerms_]:=a[0]/2 + Sum[a[n] Cos[((n π x)/L)] + b[n] Sin[((n π x)/L)],{n,1,numTerms}]

partial sum of order 3

The algebraic form of the partial sum of order 3

FSum[x,3]

The graphical form of the partial sum of order 3

Plot[FSum[x,3],{x,-L,L}]

a graphical comparison of the original function with the partial sum of order 3

Plot[{f[x],FSum[x,3]},{x,-L,L}]

partial sum of order 10

The algebraic form of the partial sum of order 10

FSum[x,10]

The graphical form of the partial sum of order 10

Plot[FSum[x,10],{x,-L,L}]

a graphical comparison of the original function with the partial sum of order 10

Plot[{f[x],FSum[x,10]},{x,-L,L}]

Graphical comparison of many partial Fourier sums with the original function

Slide the bar to see a sequence of partial Fourier sums compared to the original function

Manipulate[Plot[{f[x],FSum[x,n]},{x,-L,L}],
{{n, 4,"number of series terms"}, 1, 50, 1}]

Investigating patterns

some terms

Some coefficients.  Remember a[0] is defined but not b[0], so we'll begin at n=1.  Sometimes it is best to consider a[n] and b[n] separately, but here we look at them together.

TableForm[Table[{n, a[n], b[n]},{n,1,10}]]

pattern for sine coefficients

It seems that  b[even] is positive but b[odd] is negative.  Looks like b[n even] = + 1/( (n/2) π), b[n odd] = - 2/(n π).  
We could say b[n even] = + 2/( n π) to make it match b[n odd], so that b[n] = (-1)^n 2/(n π).  Let's test this conjecture.

TableForm[Table[{n, b[n], (-1)^n 2/(n π), b[n] -(-1)^n 2/(n π)},{n,1,10}]]

investigations for cosine coefficients

It seems that a[even] is zero, for n >= 2.  
a[0] often needs to be considered separately as it does here; a[0, which is even] ≠ 0.  
Also remember that although a pattern may hold for n=0 as well as for other n's, we will use a[0]/2 in the series instead of a[0].

You might notice the pattern for a[odd], but we'll derive it for practice.

TableForm[Table[{n,((n π )/L), a[n], a[n] Cos[((n π x)/L)]},{n,1,10}]]

a few manipulations of the cosine coefficients

Together[a[n]]

-((2 )/((n^2) (π^2)))(3 (Cos[n π]-1) +n π Sin[n π])

when n is an integer, sin(n π)=0

TableForm[Table[{n,n π, Sin[n π]},{n,1,10}]]

the portion with cos(n π) has an interesting pattern

TableForm[Table[{n,n π, Cos[n π],Cos[n π]-1},{n,1,10}]]

some manipulations

it looks like a[n] = 
-((2 )/((n^2) (π^2)))(3 (Cos[n π]-1) +n π Sin[n π]) = -((2 )/((n^2) (π^2)))(3 *((-2) for n odd, 0 for n even, n≥2)+n π *0)
In other words, a[2k-1] = ((12 )/((((2k - 1))^2) (π^2))) and a[2k] = 0 for k=1, 1, 2, ...

TableForm[Table[{k, a[2k-1],((12 )/((((2k - 1))^2) (π^2))),a[2k-1] -((12 )/((((2k - 1))^2) (π^2)))},{k,1,5} ]]

some manipulation of the partial sum

1/2 + Sum[a[n] Cos[((n π x)/L)],{n,1,numTerms}] + Sum[b[n] Sin[((n π x)/L)],{n,1,numTerms}] 

= 1/2 + Sum[a[n] Cos[((n π x)/L)],{n even,0,even value up to numTerms}] +Sum[a[n] Cos[((n π x)/L)],{n odd,0,odd  value up to numTerms}] + Sum[(((-1))^n)  (2/(n π)) Sin[((n π x)/L)],{n,1,numTerms}] 

= 1/2 + Sum[a[2k] Cos[((2k π x)/L)],{k,1,close to numTerms/2}] +Sum[a[2k-1] Cos[(((2k-1) π x)/L)],{k,1,close to numTerms/2}] + Sum[(((-1))^n)  (2/(n π)) Sin[((n π x)/L)],{n,1,numTerms}] 

=1/2 + Sum[0*Cos[((2k π x)/L)],{k,1,numTerms/2}] +Sum[ ((12 )/((((2k -1))^2) (π^2))) Cos[(((2k-1) π x)/L)],{k,1,numTerms/2}] + Sum[(((-1))^n)  (2/(n π)) Sin[((n π x)/L)],{n,1,numTerms}] 

= 1/2 + Sum[ ((12 )/((((2k -1))^2) (π^2))) Cos[(((2k-1) π x)/L)],{k,1,numTerms/2}] + Sum[(((-1))^n)  (2/(n π)) Sin[((n π x)/L)],{n,1,numTerms}]

a calculation based on using patterns

1/2+ Sum[ ((12 )/((((2k -1))^2) (π^2))) Cos[(((2k-1) π x)/L)],{k,1,5}] + Sum[(((-1))^n)  (2/(n π)) Sin[((n π x)/L)],{n,1,10}]

compare with our previous results

FSum[x,10]