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,fft,x,lL,LL];
fft[ff_,x_,LL_]:=ff[x-(Ceiling[((x-(LL))/(2LL))])(2LL)]

Functions must be in the Pure Function form

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

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

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

Example -1

Clear[can1,cbn1,ffn1,n,LL,nn];

ffn1[x_]=Sign[x];

cbn1[n_,LL_]=FullSimplify[(1/LL)Integrate[ffn1[x] Sin[((n Pi)/LL)x],{x,-LL,LL}],And[LL>0,n∈Integers,n>0]]

can1[0,LL_]=FullSimplify[(1/(2 LL))Integrate[ffn1[x],{x,-LL,LL}],And[LL>0]]

can1[n_,LL_]=FullSimplify[(1/LL)Integrate[ffn1[x] Cos[((n Pi)/LL)x],{x,-LL,LL}],And[LL>0,n∈Integers,n>0]]

nn=10;can1[0,lL]+Sum[can1[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cbn1[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]

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

pic2a=Graphics[{
{PointSize[0.015],Green,{Point[{# lL,-1}],Point[{# lL,1}],Point[{# lL,0}]}&/@Range[-10,13,1]},{PointSize[0.01],White,{Point[{# lL,-1}],Point[{# lL,1}]}&/@Range[-10,13,1]}
}];pic3=Plot[Evaluate[{can1[0,lL]+Sum[can1[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cbn1[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,PlotRange->{{-3,5},{-1.5,1.5}},AspectRatio->Automatic,ImageSize->600]]

The Fourier series of the above function is  with L = 1

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

It converges pointwise to the Fourier periodic extension of Sign[x] with period 2.

Module[{pic1,pic2,pic2a,pic3,lL,nn},nn=20;lL=1;pic1=Plot[{ffn1[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.007],Blue}}];pic2=Plot[{fft[ffn1[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-10,14,1]];

pic2a=Graphics[{
{PointSize[0.015],Green,{Point[{# lL,-1}],Point[{# lL,1}],Point[{# lL,0}]}&/@Range[-10,13,1]},{PointSize[0.01],White,{Point[{# lL,-1}],Point[{# lL,1}]}&/@Range[-10,13,1]}
}];pic3=Plot[Evaluate[{can1[0,lL]+Sum[can1[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cbn1[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]}],{x,-12,14},PlotStyle->{{Thickness[0.001],Black}},PlotRange->{{-4,7},{-1.5,1.5}}];Show[pic1,pic2,pic2a,pic3,PlotRange->{{-3,5},{-1.5,1.5}},AspectRatio->Automatic,ImageSize->600]]

Or, the same picture with Manipulate

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

pic2a=Graphics[{
{PointSize[0.015],Green,{Point[{# lL,-1}],Point[{# lL,1}],Point[{# lL,0}]}&/@Range[-10,13,1]},{PointSize[0.01],White,{Point[{# lL,-1}],Point[{# lL,1}]}&/@Range[-10,13,1]}
}];pic3=Plot[Evaluate[{can1[0,lL]+Sum[can1[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cbn1[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,PlotRange->{{-3,5},{-1.5,1.5}},AspectRatio->Automatic,ImageSize->600]],{{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. For example for x=1/2

(4/π)(∑_(k=1)^∞)(1/(2k-1))Sin[(2k-1)π(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 Gregory-Leibniz-Madhava formula for π.

But we can get more numerical series sums from the above Fourier series. For x=1/3, the sum is also 1

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

Mathematica knows this.

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

But which numerical series is this?

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

We can factor out ((Sqrt[3])/2) and the the nonzero terms are

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

And the numerators are

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

or

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

or

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

And the signs are

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

Verify:

Table[{3 Floor[j/2]+((1+(((-1))^(j-1)))/2),(2/(Sqrt[3]))((1/(2k-1))Sin[(2k-1)*(Pi/3)])/.{k->3 Floor[j/2]+((1+(((-1))^(j-1)))/2)},((2(((-1))^(j-1)))/( 6 j-3+(((-1))^j)))},{j,1,10}]

Next command verifies the first 10000 terms:

Apply[And,Table[((2/(Sqrt[3]))((1/(2k-1))Sin[(2k-1)*(Pi/3)])/.{k->3 Floor[j/2]+((1+(((-1))^(j-1)))/2)})==((2(((-1))^(j-1)))/( 6 j-3+(((-1))^j))),{j,1,10000}]]

So, the sum of this series should be 1

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

So, Mathematica does not know the sum of this series. We can verify numerically

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

Numerical evidence shows that it is correct.

Does Mathematica know that the series that we found converges to Sign function on [-1,1]?

FullSimplify[(4/π)(∑_(k=1)^∞)(1/(2k-1))Sin[(2k-1)*π*x],And[x>0,x<1]]

The above formula is an interesting way to represent Sign

Plot[((2 I (ArcTanh[(E^(-I π x))]-ArcTanh[(E^(I π x))]))/π),{x,-1,5}]

TrigToExp[FullSimplify[(4/π)(∑_(k=1)^∞)(1/(2k-1))Sin[(2k-1)*π*x],And[x>0,x<1]]]

FullSimplify[Re[(4/π)(∑_(k=1)^∞)(1/(2k-1))Sin[(2k-1)*π*x]],And[x>0,x<1]]

Example 0

Clear[ca0,cb0,ff0,n,lL,nn];

ff0[x_]=UnitStep[x];

cb0[n_,LL_]=FullSimplify[(1/LL)Integrate[ff0[x] Sin[((n Pi)/LL)x],{x,-LL,LL}],And[LL>0,n∈Integers,n>0]]

ca0[0,LL_]=FullSimplify[(1/(2 LL))Integrate[ff0[x],{x,-LL,LL}],And[LL>0]]

ca0[0,3]

ca0[n_,LL_]=FullSimplify[(1/LL)Integrate[ff0[x] Cos[((n Pi)/LL)x],{x,-LL,LL}],And[LL>0,n∈Integers,n>0]]

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

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

Example 1

Clear[ca1,cb1,ff1,n,lL,nn];

ff1[x_]=x;

ca1[n_,LL_]=FullSimplify[(1/LL)Integrate[ff1[x] Cos[((n Pi)/LL)x],{x,-LL,LL}],And[LL>0,n∈Integers,n>0]]

ca1[0,LL_]=FullSimplify[(1/(2 LL))Integrate[ff1[x],{x,-LL,LL}],And[LL>0,n∈Integers,n>0]]

cb1[n_,LL_]=FullSimplify[(1/LL)Integrate[ff1[x] Sin[((n Pi)/LL)x],{x,-LL,LL}],And[LL>0,n∈Integers,n>0]]

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

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[cb1[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]],{x,-12,14},PlotStyle->{{Thickness[0.003],Black}}];Show[pic1,pic2,pic2a,pic3,PlotRange->{{-4,11},{-3,3}},AspectRatio->Automatic,ImageSize->800]]

Example 2

Clear[ca2,cb2,ff2,n,lL,nn];

ff2[x_]=Abs[x];

cb2[n_,LL_]=FullSimplify[(1/LL)Integrate[ff2[x] Sin[((n Pi)/LL)x],{x,-LL,LL}],And[LL>0,n∈Integers,n>0]]

ca2[0,LL_]=FullSimplify[(1/(2 LL))Integrate[ff2[x],{x,-LL,LL}],And[LL>0]]

ca2[n_,LL_]=FullSimplify[(1/LL)Integrate[ff2[x] Cos[((n Pi)/LL)x],{x,-LL,LL}],And[LL>0,n∈Integers,n>0]]

Module[{pic1,pic2,pic2a,pic3,nn,lL},lL=2;nn=10;pic1=Plot[{ff2[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.007],Blue}}];pic2=Plot[{fft[ff2[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-10,14,4]];pic3=Plot[Evaluate[{ca2[0,lL]+Sum[ca2[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cb2[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]}],{x,-12,14},PlotStyle->{{Thickness[0.002],Black}}];Show[pic1,pic2,pic3,PlotRange->{{-4,11},{-1,3}},AspectRatio->Automatic,ImageSize->800]]

We can clearly see the uniform convergence here.

One interesting numerical series when we substitute x = L in the Fourier Series for this function:

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

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

Therefore

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

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

And from here we can calculate

S=(∑_(n=1)^∞)(1/(n^2))=(∑_(k=1)^∞)(1/(((2 k -1))^2))+(∑_(k=1)^∞)(1/(((2 k ))^2))=(∑_(k=1)^∞)(1/(((2 k -1))^2))+(1/4)(∑_(k=1)^∞)(1/(k^2))=((π^2)/8)+(1/4)S

And from here one gets

((π^2)/6)=S=(∑_(n=1)^∞)(1/(n^2))

Example 3

Clear[ff3,n,LL,lL,nn];

ff3[x_]=x UnitStep[x];

For this function we do not need to calculate the Fourier Coefficients. We know that they are (1/2) of the coefficients in E2 and E1.

Module[{pic1,pic2,pic2a,pic3,nn,lL},nn=20;lL=2;pic1=Plot[{ff3[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.007],Blue}}];pic2=Plot[{fft[ff3[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.004],Green}},Exclusions->Range[-10,14,4]];

pic2a=Graphics[{
{PointSize[0.015],Green,{Point[{#,0}],Point[{#,2}],Point[{#,1}]}&/@Range[-10,13,4]},{PointSize[0.01],White,{Point[{#,0}],Point[{#,2}]}&/@Range[-10,13,4]}
}];pic3=Plot[Evaluate[{(1/2)ca2[0,lL]+Sum[(1/2)ca2[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[(1/2)cb1[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]}],{x,-12,14},PlotStyle->{{Thickness[0.00175],Black}}];Show[pic1,pic2,pic2a,pic3,PlotRange->{{-3,7},{-1,3}},AspectRatio->Automatic,ImageSize->800]]

Example 4

Clear[ca4,cb4,ff4,n,lL,nn];

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

cb4[n_,LL_]=FullSimplify[(1/LL)Integrate[ff4[x] Sin[((n Pi)/LL)x],{x,-LL,LL}],And[LL>0,n∈Integers,n>0]]

ca4[0,LL_]=FullSimplify[(1/(2 LL))Integrate[ff4[x],{x,-LL,LL}],And[LL>0]]

ca4[n_,LL_]=FullSimplify[(1/LL)Integrate[ff4[x] Cos[((n Pi)/LL)x],{x,-LL,LL}],And[LL>0,n∈Integers,n>0]]

Module[{pic1,pic2,pic2a,pic3,nn,lL},lL=1;nn=20;pic1=Plot[{ff4[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.008],Blue}}];

pic2=Plot[{fft[ff4[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.004],Green}},Exclusions->Range[-11,14,2]];

pic2a=Graphics[{
{PointSize[0.01],Green,{Point[{#,0}],Point[{#,1}],Point[{#,1/2}]}&/@Range[-11,13,2]},{PointSize[0.007],White,{Point[{#,0}],Point[{#,1}]}&/@Range[-11,13,2]}
}];pic3=Plot[Evaluate[{ca4[0,lL]+Sum[ca4[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[cb4[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]}],{x,-12,14},PlotStyle->{{Thickness[0.002],Black}}];Show[pic1,pic2,pic2a,pic3,PlotRange->{{-3,5},{-.1,1.1}},AspectRatio->Automatic,ImageSize->800]]

Example 5

Clear[ca5,cb5,ff5,n,lL,nn];

ff5[x_]=(x^2);

Since we already calculated E4 we do not need to calculate the Fourier coefficients for this function. We know that the sine coefficients are 0s and the cosine coefficients are double the cosine coefficients from E4.

Module[{pic1,pic2,pic2a,pic3,nn,lL},lL=1;nn=10;pic1=Plot[{ff5[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.007],Blue}}];

pic2=Plot[{fft[ff5[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-11,14,2]];

pic3=Plot[Evaluate[{2ca4[0,lL]+Sum[2 ca4[n,lL]Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[0 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,PlotRange->{{-3,5},{-.1,1.1}},AspectRatio->Automatic,ImageSize->800]]

Since the periodic extension is continuous, the convergence is uniform in this case.

Example 6

Clear[ff6,n,lL,nn];

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

Since we already did E4 we do not need to calculate the Fourier Coefficients for this even function. We know that the cosine coefficients are 0 and the since coefficients are double the sine coefficients in E4.

Module[{pic1,pic2,pic2a,pic3,lL,nn},lL=1;nn=20;pic1=Plot[{ff6[x]},{x,-lL,lL},PlotStyle->{{Thickness[0.007],Blue}}];

pic2=Plot[{fft[ff6[#]&,x,lL]},{x,-5,10},PlotStyle->{{Thickness[0.005],Green}},Exclusions->Range[-11,14,2]];

pic2a=Graphics[{
{PointSize[0.015],Green,{Point[{#,-1}],Point[{#,1}],Point[{#,0}]}&/@Range[-11,13,2]},{PointSize[0.01],White,{Point[{#,-1}],Point[{#,1}]}&/@Range[-11,13,2]}
}];pic3=Plot[Evaluate[{0+Sum[0Cos[((n Pi)/lL)x],{n,1,nn}]+Sum[2 cb4[n,lL]Sin[((n Pi)/lL)x],{n,1,nn}]}],{x,-12,14},PlotStyle->{{Thickness[0.002],Black}},PlotRange->{{-4,7},{-1.1,1.1}}];Show[pic1,pic2,pic2a,pic3,PlotRange->{{-3,5},{-1.1,1.1}},AspectRatio->Automatic,ImageSize->800]]