# Find Fourier Series/Coefficients with Mathematica

Hello I want to find the Fourier series and/or the coefficients for a function like the following:

or

or

For the first one I did the following:

FourierTrigSeries[
Piecewise[{{0, -Pi <= x <= -Pi/2}, {Cos[x], -Pi/2 <= x <= Pi/2}, {0,
Pi/2 < x < Pi}}], x, 5 ]


Which seems to be correct. How can I convert this to summation form?
For Taylor Series I'm using something like this:

series[expr_, x_, x0_] :=
Defer[expr = Sum[#, {n, 0, ∞}]] &[
FullSimplify@
SeriesCoefficient[expr, {x, x0, n},
Assumptions -> n ⩾ { 0}] (x - x0)^n]

series[1/2*(x^2 - 2 x + 5)/(x^2 - 6 x + 9), x, 1]


The goal would be to get something like this: Which would be the solution for the first one.

• Hi TimSch -- it would help if you could put your equations into Mathematica code and show exactly what you tried when taking the Fourier coefficients. Sometimes the problem can be with syntax, and sometimes with particular assumptions that need to be made -- for instance, you haven't specified what $\hat{u}$ is, is it real-valued, and does Mathematica know to make this assumption? – bill s Sep 6 '18 at 15:08
• û is a constant factor. I'm completely missing the approach. Neither I know how to say that all values are real nor how to say that f is this for some x and f is that for some other values. – TimSch Sep 6 '18 at 15:29
• Finally I may have found a solution. I updated my question and would appreciate your feedback for my solution. – TimSch Sep 6 '18 at 16:27
• Have a look at A more convenient Fourier series – rmw Sep 6 '18 at 18:26

You may have to do this the old fashion way. Generally if there is more than one form of a correct answer, you probably will not get the exact one you are looking for without some manipulation.

Clear["Global*"]
f[x_] = Cos[x]*(UnitStep[x + Pi/2] - UnitStep[x - Pi/2])


\$Assumptions = n \[Element] Integers


Set f[x] to be the form

eqn1 = f[x] == an Cos[n  x] + bn Sin[n  x]


to get an

Simplify[Integrate[eqn1[[1]]*Cos[n*x], {x, -Pi, Pi}] == Integrate[eqn1[[2]]*Cos[n*x], {x, -Pi, Pi}]];

an = an /. Solve[%, an][[1]] // FullSimplify;
(*(2*Cos[(Pi*n)/2])/(Pi - Pi*n^2)*)


Except for Pi in the denominator, these are the same coefficients that bills computed in his answer. For n = 0, a0 is 1/2 the general n coefficient.

a0 = 1/2 an /. n -> 0
(*1/Pi*)


For n = 1, the denominator of an is 0, so take the limit.

a1 = Limit[an, n -> 1]
(* 1/2 *)


solve for bn

Simplify[Integrate[eqn1[[1]]*Sin[n*x], {x, -Pi, Pi}] == Integrate[eqn1[[2]]*Sin[n*x],
{x, -Pi, Pi}]]
(*bn==0*)

bn = 0;


The Sin terms had better go away for an even function. Look at the first few terms of the series for n > 1.

Table[an Cos[n x], {n, 2, 10}]
(*{(2*Cos[2*x])/(3*Pi), 0, -((2*Cos[4*x])/(15*Pi)), 0,(2*Cos[6*x])/(35*Pi)}*)


Terms for odd values of n are 0, so simplify by changing n to 2n and starting the series at n = 1.

an = an /. n -> 2 n // Simplify
(*(2*(-1)^n)/(Pi - 4*Pi*n^2)*)


series = a0 + a1*Cos[x] + Sum[an*Cos[2*n*x], {n, 1, Infinity}]
(*Sum[(2*(-1)^n*Cos[2*n*x])/(Pi - 4*Pi*n^2), {n, 1, Infinity}] + Cos[x]/2 + 1/Pi*)


which looks like the answer you are looking for.

As mentioned in the comment above, you can try the functions in A more convenient Fourier series. To be specific, easyFourierTrigSeries therein is your friend:

expr = Piecewise[{{0, -Pi <= x <= -Pi/2}, {Cos[x], -Pi/2 <= x <= Pi/2}, {0,
Pi/2 < x < Pi}}];

expansion = easyFourierTrigSeries[expr, {x, -Pi, Pi}, n]


The form of the result is different from the one given in your question, but it's easy to show they're equivalent. Let's extract the summand:

summand = expansion[[2, 1, 1]]


By setting \[FormalK] to 1 we obtain the 2nd term in your result:

secondterm = summand /. \[FormalK] -> 1


When \[FormalK] is odd, the summand is 0:

oddterm = FullSimplify[summand /. \[FormalK] -> 2 k + 1, {k > 0, k ∈ Integers}]
(* 0 *)


When \[FormalK] is even and larger than 1, the summand is equivalent to:

eventerm = FullSimplify[summand /. \[FormalK] -> 2 k, {k > 0, k ∈ Integers}]


So expansion can be rewritten as

firstterm = expansion[[1]]

firstterm + secondterm + HoldForm[Sum[#, {k, Infinity}]] &@eventerm


which is (almost) the same as yours.

If you want the general term of the Fourier expansion, you might be better off using FourierTransform instead of FourierTrigSeries. For example, define a function that is the FT

ft[w_] = FourierTransform[Piecewise[{{0, -Pi <= x <= -Pi/2},
{Cos[x], -Pi/2 <= x <= Pi/2}, {0,Pi/2 < x < Pi}}],
x, w, FourierParameters -> {1, -1}];

ft[w]
-((2 Cos[(\[Pi] w)/2])/(-1 + w^2))


To recapture the first 10 (say) coefficients:

ft[#] & /@ Range[2, 10]
{2/3, 0, -(2/15), 0, 2/35, 0, -(2/63), 0, 2/99}
`

which match the coefficients of the Cos terms in the FourierTrigExpansion.

• Thanks for your answer! Unfortunately it's not yet what I need. I updated my question to clarify this. I tried the approach of "A more convenient Fourier series" and tried the easyFourierSeries but I got a complex result. – TimSch Sep 6 '18 at 19:46