Solve the DE by using the Laplace transform:

x''+x=cos(3t), x(0)=x'(0)=0

L{x"} = s^2*F(s) - s*f(0) - f'(0)

= s^2*F(s)

so,

s^2 F(s) + F(s) = s/(s^2+9)

F(s) = s/((s^2+9)(s^2+1))
= 1/8 (s/(s^2+1) - s/(s^2+9))

f(t) = 1/8 (cos(t) - cos(3t))

check:
x' = 1/8 (3sin(3t)-sin(t))
x" = 1/8 (9cos(3t)-cos(t))

x'+x = 1/8 (8cos(3t)) = cos(3t)