Why do I encounter 1/0 expression

I am trying to write my periodical function $f(t)$ in Fourier Series $$f(t)=\frac{a_0}{2}+\sum_{j=1}^Na_j\cos(j 2\pi t/\tau)+\sum_{j=1}^Nb_j\sin(j 2\pi t/\tau)$$ and I know that there is a really nice function FourierSeries yet I have to explicitly find a formula for coefficients $a_j$ and $b_j$. So the theory says $$a_0=\frac 2\tau \int_{0}^\tau f(t)dt$$ $$a_j=\frac 2\tau \int_{0}^\tau f(t)\cos(j2\pi t/\tau)dt$$ $$b_j=\frac 2\tau \int_{0}^\tau f(t)\sin(j2\pi t/\tau)dt$$ and I did exactly that in my code

M1[t_] := M0 (1 + Sin[ω0 t]) /. ω0 -> 2 Pi/τ
M2[t_] :=
M0 (1 + Sin[ω0 t - (2 Pi)/3]) /. ω0 -> 2 Pi/τ
M3[t_] :=
M0 (1 + Sin[ω0 t - (4 Pi)/3]) /. ω0 -> 2 Pi/τ

moment[t_] =
Piecewise[{{Simplify[M2[t] + M3[t]],
0 <= t < τ/2}, {Simplify[M1[t] + M2[t] + M3[t]], τ/2 <=
t <= τ}}]

a0 = Simplify[
2/τ Integrate[moment[t], {t, 0, τ},
Assumptions -> {τ ∈ Reals, τ > 0}]]

aj[j_] = Assuming[j ∈ Integers,
Simplify[2/τ Integrate[
Cos[j 2 Pi t/τ] moment[t], {t, 0, τ},
Assumptions -> {τ ∈ Reals, τ > 0}]]]

bj[j_] = Assuming[j ∈ Integers,
Simplify[2/τ Integrate[
Sin[j 2 Pi t/τ] moment[t], {t, 0, τ},
Assumptions -> {τ ∈ Reals, τ > 0}]]]

momnetSeries[num_, t_] :=
a0/2 + Sum[aj[j] Cos[j 2 Pi t/τ ], {j, 1, num}] +
Sum[bj[j] Sin[j 2 Pi t/τ ], {j, 1, num}]


Note that a[j] has unexpected and unwanted singularity$$a_j=\frac{\left((-1)^j+1\right) \text{M}_0}{\pi \left(j^2-1\right)}$$ at $j=1$ therefore the following code doesn't work

M0 = 102(*Nm*);
Jn = 0.108 (*kg m^2*);
δ = 0.23;
obr = 4500;
τ = Pi/omega /. omega -> obr 2 Pi /60;

Plot[{momnetSeriest[100, t], moment[t]}, {t, 0, τ},
PlotRange -> All]


I wonder what is so badly wrong with my code or where does that singularity come from? I can't simply leave out $j=1$ from the Fourier Series formula.

• do you realize your function (moment) is discontinuous? Commented Apr 15, 2016 at 20:01

It turns out your coefficients have finite values for j=1 which you can get at by taking a limit:

M0 = 1
\[Tau] = 1
a0 = Simplify[
2/\[Tau] Integrate[moment[t], {t, 0, \[Tau]},
Assumptions -> {\[Tau] \[Element] Reals, \[Tau] > 0}]]
aj[j_] = Assuming[j \[Element] Integers,
Simplify[2/\[Tau] Integrate[
Cos[j 2 Pi t/\[Tau]] moment[t], {t, 0, \[Tau]},
Assumptions -> {\[Tau] \[Element] Reals, \[Tau] > 0}]]]
bj[j_] = Assuming[j \[Element] Integers,
Simplify[2/\[Tau] Integrate[
Sin[j 2 Pi t/\[Tau]] moment[t], {t, 0, \[Tau]},
Assumptions -> {\[Tau] \[Element] Reals, \[Tau] > 0}]]]
aj[1] = Limit[aj[x], x -> 1]
bj[1] = Limit[bj[x], x -> 1]
momnetSeries[num_, t_] :=
a0/2 + Sum[aj[j] Cos[j 2 Pi t/\[Tau]], {j, 1, num}] +
Sum[bj[j] Sin[j 2 Pi t/\[Tau]], {j, 1, num}]
Plot[{moment[t], momnetSeries[20, t]}, {t, 0, 1}]


• I do realize that my function is discontinuois, yet i had absolutely no idea that a simple limit does that job. Nicely done! Thanks! Commented Apr 15, 2016 at 20:41

Although george2079 has answerd your question, I will show the pitfals which can appear with the calculation aj[j] and bj[j].

M0 = 102(*Nm*);
Jn = 0.108 (*kg m^2*);
δ = 0.23;
obr = 4500;
τ = Pi/(obr 2 Pi/60);
num = 100;

M1[t_] = M0 (1 + Sin[ω0 t]) /. ω0 -> 2 Pi/τ;
M2[t_] = M0 (1 + Sin[ω0 t - (2 Pi)/3]) /. ω0 -> 2 Pi/τ;
M3[t_] = M0 (1 + Sin[ω0 t - (4 Pi)/3]) /. ω0 -> 2 Pi/τ;
moment[t_] =
Piecewise[{{Simplify[M2[t] + M3[t]], 0 <= t < τ/2}, {Simplify[M1[t] + M2[t] + M3[t]], τ/2 <= t <= τ}}];


It is not advisable to simplify aj[j] and bj[j]. One can now see the term (-1+j^2) in the dominator, hence one has to bild the limit.

a0 = 2/τ Integrate[moment[t], {t, 0, τ}] // Simplify
aj[j_] = 2/τ Integrate[moment[t] Cos[2 j π t/τ], {t, 0, τ}]
bj[j_] = 2/τ Integrate[moment[t] Sin[2 j π t/τ], {t, 0, τ}]


aj[1] = Limit[aj[k], k -> 1];
bj[1] = Limit[bj[k], k -> 1];

momnetSeries[t_] =
a0/2 + Sum[aj[j] Cos[j 2 Pi t/τ] + bj[j] Sin[j 2 Pi t/τ], {j, 1, num}];

Plot[{momnetSeries[t], moment[t]}, {t, 0, τ}, PlotRange -> All]


Edit

To make it clear, I simplify now aj and bj:

ajj[j_] = Simplify[aj[j], j ∈ Integers]
bjj[j_] = Simplify[bj[j], j ∈ Integers]


ajj[1] = Limit[ajj[k], k -> 1];
momnetSeries[t_] =
a0/2 + Sum[ajj[j] Cos[j 2 Pi t/τ] + bjj[j] Sin[j 2 Pi t/τ], {j, 1, num}];

Plot[{momnetSeries[t], moment[t]}, {t, 0, τ}, PlotRange -> All]


Wow! What's wrong? Clearly, ajj[1] is imaginary in contrast to aj[1]

Limit[ajj[k], k -> 1]
(* -51 I *)

Limit[aj[k], k -> 1]
(* 0 *)


Be carefully with Simplifyin this case!

For completeness I do the same with Mathematicas FourierSeries. Your function moment[t] is defined in the range [0, τ] and not in [-Pi, Pi], that's why we have to build the series over moment[t] + moment[t +τ]. The period is further τ and not 2 Pi, hence we have to define the FourierParameters.

<< FourierSeries

fr = NFourierTrigSeries[moment[t] + moment[t + τ], t, 100, FourierParameters -> {1, 2 π/τ}, AccuracyGoal -> 5];
Plot[{fr, moment[t]}, {t, 0, τ}, PlotRange -> All]
`