Fourier series in Mathematica

After my last post—the one about using Fourier series—I started thinking about how to use Mathematica to develop Fourier series.1 I could, of course, use the Integrate function to determine the Fourier coefficients, but Mathematica has other functions that can do the job directly once you understand how they work.

Mathematica has several Fourier functions, but I’m going to stick with the ones associated with sine series, FourierSinCoefficient and FourierSinSeries. They’re meant to be easy to use, and they are, but you need to know how they’re defined.

The coefficients used in both of these functions are defined this way:

fn=2π0πf(x)sinnxdx

This doesn’t match up exactly with the definition I used for getting the Fourier coefficients of a loading function, q(x):

qn=2L0Lq(x)sinnπxLdx

To use the Mathematica functions to get the qn, we have to do a change of variable. Let

z=πxL

so

x=Lzπanddx=Lπdz

(Here, z is just another variable name; I’m not using it to represent a complex number.)

This changes the expression for qn to

qn=2π0πq(Lzπ)sin(nz)dz

which means we can use the Mathematica functions as long as we substitute Lz/π in for x in the expression for q.

Let’s give it a try on this parabolic loading function:

q=4qmaxx(Lx)

It shouldn’t take many Fourier terms to get a good approximation of this.

Parabolic loading

Here are the Mathematica commands I used:

q = 4 qmax x (L - x)
qn = FourierSinCoefficient[q /. x -> L z/Pi, z, n]

You can see the substitution in the first argument to FourierSinCoefficient.

After simplification, the results were

qn={32L2qmaxn3π3for odd n0for even n

We could also get the series (through n=7) directly with FourierSinSeries:

qapprox2 = FourierSinSeries[q /. x -> L z/Pi, z, 7] /. z -> Pi x /L

where the inverse substitution comes at the end to put the expression back in the form we want. Here’s a screenshot from the Mathematica notebook:

Mathematica notebook screenshot of Fourier series

You can see that the coefficients match what we got earlier.

A quick plot of the difference between this truncated Fourier series and the original parabolic function shows that, as expected, the series does a good job of replicating the original:

Error of Fourier series estimate

In this plot, the horizontal axis is the fraction of L and the vertical axis is the fraction of qmax.

If you’re interested, here’s the entire Mathematica notebook:

Now that I understand the way these functions work, I can do more complicated Fourier analysis in Mathematica without questioning myself on whether I’m using them correctly.


  1. Something I couldn’t do in any of the posts in that series, as that would be breaking the rules I had set up for myself.