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[0] + 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] == 0, y'[0] == 0, y''[t] + w^2 y[t] == Abs[t]},
y, {t, -Pi, 3 Pi}]
ysol[0, w_] =
y /. First@
DSolve[{y[0] == 0, y'[0] == 0, y''[t] + w^2 y[t] == fc[0]}, y, t]
ysol[n_ /; n >= 1, w_] =
y /. First@
DSolve[{y[0] == 0, y'[0] == 0,
y''[t] + w^2 y[t] == fc[n] (E^(I n t) + E^(-I n t))}, y, t] //
ExpToTrig
Manipulate[
Plot[Evaluate[{ynr[2][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.
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]
$\endgroup$y[-Pi]==y[Pi], y'[-Pi]==y'[Pi]
. (This should be clarified by OP, of course. ) $\endgroup$