NotebookDirectory[]

Examples of Fourier series

Preliminaries

Below is the definition of a periodic extension of a function defined on (-L,L]. This definition takes a function as a variable. The function has to be inputted as a so called pure function (that is instead of the variable we put # and the formula ends with &).

Clear[ff,x,lL];

fft[ff_,x_,lL_]:=ff[x-(Ceiling[((x-(-lL))/(2lL))]-1)(2lL)]

(((#)^2))&[2]

fft[((#)^2)&,x,1]

Plot[fft[((#)^2)&,x,1],{x,-4,4}]

Example -1

Clear[ca0,ca,cb,ff,n,lL,nn];

ff[x_]=Sign[x]; 

nn=30;

cb[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Sin[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

ca0=FullSimplify[(1/(2 lL))Integrate[ff[x],{x,-lL,lL}],And[lL>0]]

ca[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Cos[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

ca0+Sum[ca[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cb[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]

lL=1;Module[{pic1,pic2,pic2a,pic3},pic1=Plot[{ff[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.01],Blue}},PlotRange->{{-4,7},{-1.5,1.5}}];pic2=Plot[{fft[ff[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-10,14,1],PlotRange->{{-4,7},{-1.5,1.5}},AspectRatio->Automatic];

pic2a=Graphics[{
{PointSize[0.02],Green,{Point[{#,-1}],Point[{#,1}],Point[{#,0}]}&/@Range[-10,13,1]},{PointSize[0.014],White,{Point[{#,-1}],Point[{#,1}]}&/@Range[-10,13,1]}
}];pic3=Plot[Evaluate[{ca0+Sum[ca[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cb[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]}],{x,-12,14},PlotStyle->{{Thickness[0.003],Black}},PlotRange->{{-4,7},{-1.5,1.5}}];Show[pic1,pic2,pic2a,pic3]]

lL=1;Module[{pic1,pic2,pic2a,pic3},pic1=Plot[{ff[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.01],Blue}},PlotRange->{{-1.5,1.5},{-1.5,1.5}}];pic2=Plot[{fft[ff[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-10,14,1],PlotRange->{{-1.5,1.5},{-1.5,1.5}},AspectRatio->Automatic];

pic2a=Graphics[{
{PointSize[0.02],Green,{Point[{#,-1}],Point[{#,1}],Point[{#,0}]}&/@Range[-10,13,1]},{PointSize[0.014],White,{Point[{#,-1}],Point[{#,1}]}&/@Range[-10,13,1]}
}];pic3=Plot[Evaluate[{ca0+Sum[ca[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cb[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]}],{x,-12,14},PlotStyle->{{Thickness[0.003],Black}},PlotPoints->200,PlotRange->{{-1.5,1.5},{-1.5,1.5}}];Show[pic1,pic2,pic2a,pic3]]

It might be nicer to choose a specific L and write the Fourier series with the coefficients that we actually calculated: The Fourier series

(4/π)(∑_(k=1)^∞)(1/(2k-1))Sin[(2k-1)x]

converges pointwise to the Fourier 2π-periodic extension of Sign[x].

Module[{pic1,pic2,pic2a,pic3,nn},nn=10;pic1=Plot[{ff[x]},{x,-Pi,Pi},PlotStyle->{{Thickness[0.01],Blue}},PlotRange->{{-2.5 Pi,2.5Pi},{-1.5,1.5}}];pic2=Plot[{fft[ff[#]&,x,Pi]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-10,14,1],PlotRange->{{-2.5Pi,2.5Pi},{-1.5,1.5}},AspectRatio->Automatic];

pic2a=Graphics[{
{PointSize[0.02],Green,{Point[{# Pi,-1}],Point[{#Pi,1}],Point[{# Pi,0}]}&/@Range[-10,13,1]},{PointSize[0.014],White,{Point[{# Pi,-1}],Point[{#Pi,1}]}&/@Range[-10,13,1]}
}];pic3=Plot[Evaluate[{(4/π)(∑_(k=1)^nn)(1/(2k-1))Sin[(2k-1)x]}],{x,-12,14},PlotStyle->{{Thickness[0.003],Black}},PlotPoints->200,PlotRange->{{-2.5Pi,2.5 Pi},{-1.5,1.5}}];Show[pic1,pic2,pic2a,pic3]]

Or, the same picture with Manipulate

Module[{pic1,pic2,pic2a,pic3,nn},Manipulate[pic1=Plot[{ff[x]},{x,-Pi,Pi},PlotStyle->{{Thickness[0.01],Blue}},PlotRange->{{-2.5 Pi,2.5Pi},{-1.5,1.5}}];pic2=Plot[{fft[ff[#]&,x,Pi]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-10 Pi,14 Pi,Pi],PlotRange->{{-2.5Pi,2.5Pi},{-1.5,1.5}},AspectRatio->Automatic];

pic2a=Graphics[{
{PointSize[0.02],Green,{Point[{# Pi,-1}],Point[{#Pi,1}],Point[{# Pi,0}]}&/@Range[-10,13,1]},{PointSize[0.014],White,{Point[{# Pi,-1}],Point[{#Pi,1}]}&/@Range[-10,13,1]}
}];pic3=Plot[Evaluate[{(4/π)(∑_(k=1)^nn)(1/(2k-1))Sin[(2k-1)x]}],{x,-12,14},PlotStyle->{{Thickness[0.003],Black}},PlotPoints->200,PlotRange->{{-2.5Pi,2.5 Pi},{-1.5,1.5}}];Show[pic1,pic2,pic2a,pic3],{{nn,10,"n"},Join[Range[10],Range[15,30,5]]},ControlType->Setter]]

What is important to point out here, is that for a specific x from the convergence theorem we KNOW the sum of this numerical series

(4/π)(∑_(k=1)^∞)(1/(2k-1))Sin[(2k-1)(π/2)]

or

(4/π)(∑_(k=1)^∞)((((-1))^(k-1))/(2k-1))=1

Mathematica knows this

(4/π)(∑_(k=1)^∞)((((-1))^(k-1))/(2k-1))

In other words

(∑_(k=1)^∞)((((-1))^(k-1))/(2k-1))=(π/4)

which is the famous Leibniz formula for π.

But we can get more numerical series sums from the above Fourier series

(4/π)(∑_(k=1)^∞)(1/(2k-1))Sin[(2k-1)*(Pi/3)]

FullSimplify[(4/π)(∑_(k=1)^∞)(1/(2k-1))Sin[(2k-1)*(Pi/3)]]

Table[{k,Sin[(2k-1)*(Pi/3)]},{k,1,20}]

Table[3 Floor[j/2]+((1+(((-1))^(j-1)))/2),{j,1,10}]

Table[2(3 Floor[j/2]+((1+(((-1))^(j-1)))/2))-1,{j,1,10}]

Table[{j,3 Floor[j/2]+((1+(((-1))^(j-1)))/2),(((-1))^(j-1))},{j,1,10}]

Table[{j,2(3 Floor[j/2]+((1+(((-1))^(j-1)))/2))-1},{j,1,10}]

Table[{j,3 (j-((1+(((-1))^(j-1)))/2))+(((-1))^(j-1))},{j,1,10}]

Table[{j,((6 j-3-(((-1))^(j-1)))/2)},{j,1,10}]

((4(Sqrt[3]))/π)(∑_(j=1)^∞)((((-1))^(j-1))/( 6 j-3-(((-1))^(j-1))))

N[((4(Sqrt[3]))/π)(∑_(j=1)^1200)((((-1))^(j-1))/( 6 j-3-(((-1))^(j-1))))]

Example 0

Clear[ca0,ca,cb,ff,n,lL,nn];

ff[x_]=UnitStep[x]; 

nn=10;

cb[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Sin[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

ca0=FullSimplify[(1/(2 lL))Integrate[ff[x],{x,-lL,lL}],And[lL>0]]

ca[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Cos[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

lL=1;Module[{pic1,pic2,pic2a,pic3},pic1=Plot[{ff[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.01],Blue}},PlotRange->{{-4,7},{-.5,1.5}}];pic2=Plot[{fft[ff[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-10,14,1],PlotRange->{{-4,7},{-.5,1.5}},AspectRatio->Automatic];

pic2a=Graphics[{
{PointSize[0.02],Green,{Point[{#,0}],Point[{#,1}],Point[{#,1/2}]}&/@Range[-10,13,1]},{PointSize[0.014],White,{Point[{#,0}],Point[{#,1}]}&/@Range[-10,13,1]}
}];pic3=Plot[Evaluate[{ca0+Sum[ca[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cb[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]}],{x,-12,14},PlotStyle->{{Thickness[0.003],Black}},PlotRange->{{-4,7},{-.5,1.5}}];Show[pic1,pic2,pic2a,pic3]]

Example 1

Clear[ca0,ca,cb,ff,n,lL,nn];

ff[x_]=x; 

nn=20;

cb[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Sin[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

lL=2;Module[{pic1,pic2,pic2a,pic3},pic1=Plot[{ff[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.01],Blue}},PlotRange->{{-4,11},{-3,3}}];pic2=Plot[{fft[ff[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-10,14,4],PlotRange->{{-4,11},{-3,3}},AspectRatio->Automatic];

pic2a=Graphics[{
{PointSize[0.02],Green,{Point[{#,-2}],Point[{#,2}],Point[{#,0}]}&/@Range[-10,13,4]},{PointSize[0.014],White,{Point[{#,-2}],Point[{#,2}]}&/@Range[-10,13,4]}
}];pic3=Plot[Evaluate[{Sum[cb[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]}],{x,-12,14},PlotStyle->{{Thickness[0.003],Black}},PlotRange->{{-4,11},{-3,3}}];Show[pic1,pic2,pic2a,pic3]]

Example 2

Clear[ca0,ca,cb,ff,n,lL,nn];

ff[x_]=Abs[x]; 

nn=10;

cb[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Sin[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

ca0=FullSimplify[(1/(2 lL))Integrate[ff[x],{x,-lL,lL}],And[lL>0]]

ca[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Cos[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

lL=2;Module[{pic1,pic2,pic2a,pic3},pic1=Plot[{ff[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.01],Blue}},PlotRange->{{-4,11},{-1,3}}];pic2=Plot[{fft[ff[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-10,14,4],PlotRange->{{-4,11},{-1,3}},AspectRatio->Automatic];pic2a=Graphics[{
{PointSize[0.02],Green,{Point[{#,-2}],Point[{#,2}],Point[{#,0}]}&/@Range[-10,13,4]},{PointSize[0.014],White,{Point[{#,-2}],Point[{#,2}]}&/@Range[-10,13,4]}
}];pic3=Plot[Evaluate[{ca0+Sum[ca[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cb[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]}],{x,-12,14},PlotStyle->{{Thickness[0.003],Black}},PlotRange->{{-4,11},{-1,3}}];Show[pic1,pic2,pic3]]

Example 3

Clear[ca0,ca,cb,ff,n,lL,nn];

ff[x_]=x UnitStep[x]; 

nn=10;

cb[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Sin[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

ca0=FullSimplify[(1/(2 lL))Integrate[ff[x],{x,-lL,lL}],And[lL>0]]

ca[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Cos[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

lL=2;Module[{pic1,pic2,pic2a,pic3},pic1=Plot[{ff[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.01],Blue}},PlotRange->{{-4,11},{-1,3}}];pic2=Plot[{fft[ff[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-10,14,4],PlotRange->{{-4,11},{-1,3}},AspectRatio->Automatic];

pic2a=Graphics[{
{PointSize[0.02],Green,{Point[{#,0}],Point[{#,2}],Point[{#,1}]}&/@Range[-10,13,4]},{PointSize[0.014],White,{Point[{#,0}],Point[{#,2}]}&/@Range[-10,13,4]}
}];pic3=Plot[Evaluate[{ca0+Sum[ca[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cb[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]}],{x,-12,14},PlotStyle->{{Thickness[0.003],Black}},PlotRange->{{-4,11},{-1,3}}];Show[pic1,pic2,pic2a,pic3]]

Example 4

Clear[ca0,ca,cb,ff,n,lL,nn];

ff[x_]=(x^2) UnitStep[x]; 

nn=20;

cb[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Sin[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

ca0=FullSimplify[(1/(2 lL))Integrate[ff[x],{x,-lL,lL}],And[lL>0]]

ca[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Cos[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

lL=1;Module[{pic1,pic2,pic2a,pic3},pic1=Plot[{ff[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.01],Blue}},PlotRange->{{-4,7},{-.1,1.1}}];

pic2=Plot[{fft[ff[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-11,14,2],PlotRange->{{-4,7},{-.1,1.1}},AspectRatio->Automatic];

pic2a=Graphics[{
{PointSize[0.02],Green,{Point[{#,0}],Point[{#,1}],Point[{#,1/2}]}&/@Range[-11,13,2]},{PointSize[0.014],White,{Point[{#,0}],Point[{#,1}]}&/@Range[-11,13,2]}
}];pic3=Plot[Evaluate[{ca0+Sum[ca[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cb[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]}],{x,-12,14},PlotStyle->{{Thickness[0.003],Black}},PlotRange->{{-4,7},{-.1,1.1}}];Show[pic1,pic2,pic2a,pic3]]

Example 5

Clear[ca0,ca,cb,ff,n,lL,nn];

ff[x_]=(x^2); 

nn=10;

cb[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Sin[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

ca0=FullSimplify[(1/(2 lL))Integrate[ff[x],{x,-lL,lL}],And[lL>0]]

ca[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Cos[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

lL=1;Module[{pic1,pic2,pic2a,pic3},pic1=Plot[{ff[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.01],Blue}},PlotRange->{{-4,7},{-.1,1.1}}];

pic2=Plot[{fft[ff[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-11,14,2],PlotRange->{{-4,7},{-.1,1.1}},AspectRatio->Automatic];

pic2a=Graphics[{
{PointSize[0.02],Green,{Point[{#,-2}],Point[{#,2}],Point[{#,0}]}&/@Range[-10,13,4]},{PointSize[0.014],White,{Point[{#,-2}],Point[{#,2}]}&/@Range[-10,13,4]}
}];pic3=Plot[Evaluate[{ca0+Sum[ca[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cb[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]}],{x,-12,14},PlotStyle->{{Thickness[0.003],Black}},PlotRange->{{-4,7},{-.1,1.1}}];Show[pic1,pic2,pic3]]

Example 6

Clear[ca0,ca,cb,ff,n,lL,nn];

ff[x_]=(x^2) Sign[x]; 

nn=10;

cb[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Sin[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

ca0=FullSimplify[(1/(2 lL))Integrate[ff[x],{x,-lL,lL}],And[lL>0]]

ca[n_,lL_]=FullSimplify[(1/lL)Integrate[ff[x] Cos[((n Pi)/lL)x],{x,-lL,lL}],And[lL>0,n∈Integers,n>0]]

lL=1;Module[{pic1,pic2,pic2a,pic3},pic1=Plot[{ff[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.01],Blue}},PlotRange->{{-4,7},{-1.1,1.1}}];

pic2=Plot[{fft[ff[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-11,14,2],PlotRange->{{-4,7},{-1.1,1.1}},AspectRatio->Automatic];

pic2a=Graphics[{
{PointSize[0.02],Green,{Point[{#,-1}],Point[{#,1}],Point[{#,0}]}&/@Range[-11,13,2]},{PointSize[0.014],White,{Point[{#,-1}],Point[{#,1}]}&/@Range[-11,13,2]}
}];pic3=Plot[Evaluate[{ca0+Sum[ca[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cb[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]}],{x,-12,14},PlotStyle->{{Thickness[0.003],Black}},PlotRange->{{-4,7},{-1.1,1.1}}];Show[pic1,pic2,pic2a,pic3]]