MATH 302-010
Ordinary Differential Equations
Prof. D. A. Edwards
Copyright 2024

Fourier Sine and Cosine Series

We begin with our standard command:

Quit[]

Fourier Cosine and Sine Series

If you want to calculate the Fourier cosine or sine series of a function, Mathematica provides the shortcuts FourierSinCoefficient[function,x,n]:and FourierCosCoefficient[function,x,n]:

f=x^2+x
Integrate[2*f*Sin[n*x]/Pi,{x,0,Pi}];
Simplify[%,Element[n,Integers]]
FourierSinCoefficient[f,x,n]

Integrate[2*f*Cos[n*x]/Pi,{x,0,Pi}];
Simplify[%,Element[n,Integers]]
FourierCosCoefficient[f,x,n]

Note that with either form the case  n=0 has to be done separately:

Integrate[2*f,{x,0,Pi}]

If you want to explicitly calculate the series to p terms, use the  FourierSinSeries[function,x,p] or FourierCosSeries[function,x,p]command.  The caveats are the same as before.

fss=FourierSinSeries[f,x,3]
fcs=FourierCosSeries[f,x,3]

We may plot our functions together to show which series works better.  Note that we only plot from 0≤x≤π, since that’s the relevant region:

Plot[{f,fcs,fss},{x,0,Pi},PlotLegends->{"f","Cosine","Sine"}]

If we need to use a different value of L,  we do the same variable substitution as for regular Fourier series.  Suppose we wanted to expand f(x) in a Fourier sine series for 0≤x≤1.  This is the same as 0⩽(π x)⩽π, so we let y=π x:

fy=f/.(x->y/Pi)

FourierSinSeries[fy,y,3]
ssx =%/.(y->Pi*x)

Note that if we also compute the cosine series and then plot, we end up with exactly the same graph as before, just compressed:

FourierCosSeries[fy,y,3]
csx =%/.(y->Pi*x)

Plot[{f,csx,ssx},{x,0,1},PlotLegends->{"f","Cosine","Sine"}]

Exercises

1. Calculate the first 7 terms in the Fourier sine series for (x^3)-4xfor 0⩽ x⩽π.

Solution

f1= x^3-4*x
FourierSinSeries[f1,x,7]

2. Calculate the first 7 terms in the Fourier cosine series for (x^3)-4xfor 0⩽ x⩽1.

Solution

f2=f1/.(x->y/Pi)
FourierCosSeries[f2,y,7]
serx =%/.(y->Pi*x)