# Fourier Series of ODE

I am having trouble finding the Fourier series of a 2nd order ODE. Should I be using the piecewise function as well to set up the range for t?

Solve $$y'' + \omega^2 𝑦 = r(t)$$, where $$r(t) = |t|, -\pi < t < \pi$$ by using Fourier series.

So far I have set up the ODE and set equal to r[t]

r[t] = y''[t] + ω^2 y[t]
Plot(r[t], {t, -π, π}]


Any help with the Mathematica code would be greatly appreciated. How can I find An, Bn with the function being an ODE?

• Maybe this?: dsol = DSolve[{y''[t] + w^2 y[t] == RealAbs[t], y[Pi] == 0, y[-Pi] == 0}, y, t, Assumptions -> w \[Element] Integers]; Simplify[ FourierCoefficient[y[t] /. First[dsol], t, n], w \[Element] Integers] Nov 24, 2020 at 0:56
• If the original question is "Solve 𝑦′′ + 𝜔^2𝑦 = 𝑟(𝑡), where 𝑟(𝑡) = |𝑡|, -𝜋 < 𝑡 < 𝜋 using Fourier series", I'm afraid you've misunderstood the question. This doesn't mean "finding the Fourier series of a 2nd order ODE". Anyway, you may want to read the following: mathematica.stackexchange.com/q/155817/1871 Nov 24, 2020 at 2:55
• @MichaelE2 I'd guess the required b.c.s are y[-Pi]==y[Pi], y'[-Pi]==y'[Pi]. (This should be clarified by OP, of course. ) Nov 24, 2020 at 3:21
• @MichaelE2 yes those are the boundary conditions Nov 24, 2020 at 4:25
• By "those" you mean which b.c.s? Please clarify by editing the question. Nov 24, 2020 at 7:31

Develop the driving force Abs[t] into FourierSeries and solve diff equation for the general coefficients.

FourierSeries[Abs[t], t, 3, FourierParameters -> {1, 1}]

(*   -((2 E^(-I t))/\[Pi]) - (2 E^(I t))/\[Pi] - (2 E^(-3 I t))/(
9 \[Pi]) - (2 E^(3 I t))/(9 \[Pi]) + \[Pi]/2   *)

fc[n_] = FourierCoefficient[Abs[t], t, n,
FourierParameters -> {1 , 1 }]


Treat n==0 separately and proofing identity yields True

FourierSeries[Abs[t], t, 9, FourierParameters -> {1, 1}] ==
fc + Sum[fc[n] (E^(I n t) + E^(-I n t)), {n, 1, 9}] // Simplify


for comparison solve numericaly for Abs[t], since DSolve gave the wrong result in version 8.0, even for Sqrt[t^2]. And solve for n==0 separately.

ynr[w_] :=
y /. First@
NDSolve[{y == 0, y' == 0, y''[t] + w^2 y[t] == Abs[t]},
y, {t, -Pi, 3 Pi}]

ysol[0, w_] =
y /. First@
DSolve[{y == 0, y' == 0, y''[t] + w^2 y[t] == fc}, y, t]

ysol[n_ /; n >= 1, w_] =
y /. First@
DSolve[{y == 0, y' == 0,
y''[t] + w^2 y[t] == fc[n] (E^(I n t) + E^(-I n t))}, y, t] //
ExpToTrig

Manipulate[
Plot[Evaluate[{ynr[t],
ysol[0, 2][t] + Sum[ysol[n, 2][t], {n, 1, nmax, 2}]}], {t, -Pi,
3 Pi}, GridLines -> Automatic, PlotStyle -> {Red, Blue},
PlotRange -> {0, 1.5}], {nmax, 1, 7, 1, Appearance -> "Labeled"
}]


Developing to n== 5 seems sufficient. 