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

Fourier Series

We begin with our standard command:

Quit[]

Fourier Series

Let’s consider the following function:

f=x^2+x

Mathematica can calculate the Fourier series of a function using the FourierTrigSeries[function,x,p] command.  However, there are two important caveats:

1.  You can do computations only where L=π.
2.  You can calculate only the first p terms of the series, where p is in the argument.   Therefore, if you want to know the term for every n, you have to do the integral directly:

Integrate[f*Sin[n*x]/Pi,{x,-Pi,Pi}]

Note that this doesn’t automatically assume that n is an integer, so we can Simplify using the Element[n,Integers] option to have it evaluate that expression with that assumption:

Simplify[%,Element[n,Integers]]

But if you’re interested only in a certain number of properties (for example, for graphing, we can use the specialized command:

FourierTrigSeries[f,x,3]

Note that the expansion matches for the sine terms; we can also calculate the cosine terms:

Integrate[f*Cos[n*x]/Pi,{x,-Pi,Pi}]
Simplify[%,Element[n,Integers]]

Note that this term is undefined for n=0, which has to be computed separately:

Integrate[f/Pi,{x,-Pi,Pi}]

Dividing by 2, we get the desired result.

We may also plot our solutions to see how close the Fourier series approaches the function.

plist=Table[FourierTrigSeries[f,x,n],{n,1,3}]

We want to label our individual curves, so we use the PlotLegends command, where we just list each term in the legend.

Plot[{f,plist},{x,-Pi,Pi},PlotLegends->{"f","1-Term","2-Term","3-Term"}]

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

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

Then we do the Fourier series in y:

FourierTrigSeries[fy,y,3]

Then we substitute y=π x to get our final answer:

serx =%/.(y->Pi*x)

Exercises

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

Solution

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

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

Solution

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