# How do I find the n-th Fourier coefficient of a function?

I have to define the function f:

• π for x ∈ (-1,1)
• 0 for all the others.

How do I find the n-th coefficient of this function in the Fourier series expansion for (-3π, 3π)?

• Is this a question about the computing software Mathematica? In that case, look up the functions Piecewise and Integrate in the documentation for proper syntax for defining piecewise-defined function and how to integrate functions. If not, perhaps you meant to ask this on Mathematics. – march Jan 23 at 19:19

Define the function,

f[x_] := If[x < 1 && x > -1, \[Pi], 0]


or

f[x_] := Piecewise[{{\[Pi], x > -1 && x < 1}}, 0]


Do the series expansion, b=1/3 for the required region $$(-3\pi,3\pi)$$ (check documentation of FourierSeries and FourierParameters) for details, here choosing a=0, and up to the 30th order

fx = FourierSeries[f[x], x, 30, FourierParameters -> {0, 1/3}];


You may plot it to see the approximation

Plot[fx, {x, -3 \[Pi], 3 \[Pi]}, PlotRange -> All] and to collect the terms by order, use

fxc = Collect[fx, E^((I x)/3) ]


1/3 + E^(-((I x)/3)) Sin[1/3] + E^((I x)/3) Sin[1/3] + 1/2 E^(-((2 I x)/3)) Sin[2/3] + 1/2 E^((2 I x)/3) Sin[2/3] + 1/3 E^(-I x) Sin1 + 1/3 E^(I x) Sin1+... (truncated)

where you may then access each one by array index e.g. fxc[] (notice the grouping of negative and positive n)

• Okay, but I have to make the draw of expansion of Fourier series of n-th order in (-3π,3π). How am I suppose to do that? n=1,3,5,7...15 – Crunchips Jan 23 at 19:41
• @Crunchips something like Plot[fxc[] // ReIm // Evaluate, {x, -3 \[Pi], 3 \[Pi]}] – egwene sedai Jan 23 at 19:44
• Why is there a difference in FourrierParamers in in our answers? Did you look in Mma help? – user64494 Jan 24 at 14:44
• @user64494 FourierSeries gives the whole series expansion, FourierCoefficient outputs the coefficient before the $n$-th order $e^{int}$, so there's actually no difference. notice for the whole expansion, the n in FourierCoefficient  goes from negative "cut" $-n_c$ to the positive $n_c$ – egwene sedai Jan 24 at 15:20

This can be done as follows.

f[x_] := Piecewise[{{\[Pi], x > -1 && x < 1}}, 0];
FourierCoefficient[f[x], x, n, FourierParameters -> {1, 1/3}] // TeXForm


$$\begin{cases} \frac{1}{3} & n=0 \\ \frac{\sin \left(\frac{n}{3}\right)}{n} & \text{True} \end{cases}$$

See Mathematica help to FourierCoefficient for more info.