Forced Harmonic Oscillator II: Arbitrary periodic driving
Force

\(TraditionalForm\`We\ consider\ a\ periodic, \ of\ period\ 2  π, \ 
    but\ arbitray\ forcing\ term.\ \ 
F \((t)\) = 
      ∑\+\(n = 0\)\%∞ c\_n\ Exp[i\ n\ t], \ 
    we\ can\ construct\ a\ particular\ solution\)\(TraditionalForm\`\(for\ every\ term\ n, \)
\)

\(\(yp\_n\)(t) = 
          \(2\ \((\(-n\^2\) + ω\^2)\)\ Cos[n\ t] + 
              4\ b\ n\ Sin[n\ t]\)\/\(4\ b\^2\ n\^2 + 
              \((\(-n\^2\) + ω\^2)\)\^2\)


        The\ most\ general\ solution\ is\ then


        y(t) = 
          yc(t) + \ ∑\+\(n = 0\)\%∞ c\_n\ \(\(yp\_n\)(t)\)
\)

\(TraditionalForm
    \`In\ this\ example\ we\ set\ the\ transient\ solution\ \(yc(t)\)\ to\ 
      zero, \ and\)focus on the particular solution;  
    As an example we take a squarepulse  as shown below,  
    the (Subscript[c, n]) coefficients are given by

\(TraditionalForm
    \`c\_n = \(1\/\(2  π\)\) 
        \(∫\_\(-π\)\%π\( F(t)\) Exp[\(-i\)\ n\ t] 
            dt\)\)

c[n_]:=(-I)^(n+1) ((-1)^n-1)/2/n/Pi

c0=1/2;

nmax=20;
f[t_]:=c0+Sum[2 c[n] Cos[n t],{n,1,nmax}]

Plot[{Evaluate[f[t]],Cos[t]},{t,-3 Pi,3 Pi}]

yp[n_,t_,ω_,b_]=((ω^2-n^2)2 Cos[n t]+
4 b n Sin[n t])/((ω^2-n^2)^2+4 b^2 n^2)

y[t_,ω_,b_]:=Sum[c[n] yp[n,t,ω,b],{n,1,nmax}]+c0

Table[Plot[Evaluate[y[t,ω,0.01]],{t,0,3Pi},PlotRange->{-4,4}],{ω,0.1,4,0.1}]

Table[Plot[y[t,ω,0.1],{ω,0.1,5},PlotRange->{-3.5,3.5}],{t,0,2  Pi,1}]