Solve the ODE using complex variables $$y''+4y=1+\cos2x$$
The homogeneous solution is 
$$y_H=A\cos 2x+B\sin 2x$$
I'm strugling to find the correct trial solution for the inhomogeneous part of the ODE. $y_P=Ce^{2ix}$ does not work because the constant cancels out after inputing the solution into the ODE (and the real part is already a fundamental solution of the ODE). $y_P=Cxe^{2ix}$ leaves me with $Ce^{2ix}(4iC-1)=1$.
I could, of course, splitthe ODE into two inhomogeneous ODEs, solve for them, and sum their solution for the particular solution of the initial ODE, but that's not the point of this exercise.
 A: The equation you have is the real part of
$$ z'' + 4z = 1 + e^{2ix} $$
where $z$ is complex-valued and $y$ is the real part of $z$. Since $e^{2ix}$ is already a homogeneous solution, you will need to have a particular solution of the form
$$ z_p(x) = A + Bxe^{2ix} $$
Taking derivatives
\begin{align} {z_p}' &= B\big[e^{2ix} + 2ixe^{2ix}\big] = B(1+2ix)e^{2ix} \\
 {z_p}'' &= B\big[2ie^{2ix} + 2i(1+2ix)e^{2ix}\big] = B(4i-4x)e^{2ix} \end{align}
Plugging this in, we find
$$ {z_p}'' + 4{z_p} = 4A + 4iBe^{2ix} = 1 + e^{2ix} $$
this gives
$$ A = \frac14, \ B = \frac{1}{4i}=-\frac{i}{4} $$
Then
\begin{align} z_p(x) &= \frac14 - \frac{i}{2}xe^{2ix}\\ 
&= \frac14 - \frac{i}{4}x\big[\cos 2x + i\sin 2x\big] \\
&= \frac14 + \frac{1}{4}x\sin 2x - i\frac{1}{2}x\cos 2x \end{align}
Taking the real part
$$ y_p(x) = \frac14 + \frac{1}{4}x\sin 2x $$
A: $\newcommand{\bbx}[1]{\,\bbox[15px,border:1px groove navy]{\displaystyle{#1}}\,}
 \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack}
 \newcommand{\dd}{\mathrm{d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,}
 \newcommand{\ic}{\mathrm{i}}
 \newcommand{\mc}[1]{\mathcal{#1}}
 \newcommand{\mrm}[1]{\mathrm{#1}}
 \newcommand{\pars}[1]{\left(\,{#1}\,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,}
 \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}}
 \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$

Lets $\ds{\xi \equiv y' + 2\ic y\quad}$ such that
  $\ds{\quad y = {1 \over 2}\,\Im\pars{\xi}}$.
  
  $\ds{\xi' = y'' + 2\ic y' =
\pars{y'' + 4y} + 2\ic\pars{y' + 2\ic y} = \pars{y'' + 4y} + 2\ic\xi}$
  
  $\ds{\implies \bbx{y'' + 4y = \xi' - 2\ic\xi = 1 + \cos\pars{2x}}}$. 

\begin{align}
\totald{\pars{\expo{-2\ic x}\xi}}{x} & =
\expo{-2\ic x} + \expo{-2\ic x}\cos\pars{2x}
\\[5mm]
\expo{-2\ic x}\xi & = {1 \over 2}\,\ic\expo{-2\ic x} +
{1 \over 8}\,\ic\expo{-4\ic x} + {1 \over 2}\,x + 2a\,,
\qquad\pars{a \in \mathbb{C}\ \mbox{is a}\ constant}
\\[5mm]
\xi & = {1 \over 2}\,\ic +
{1 \over 8}\,\ic\expo{-2\ic x} + {1 \over 2}\,x\expo{2\ic x} + 2a\expo{2\ic x}
\\[5mm]
y & = \bbx{{1 \over 4} +
{1 \over 16}\,\cos\pars{2x} + {1 \over 4}\,x\sin\pars{2x} +
\Im\pars{a\expo{2\ic x}}}
\end{align}

Lets $\ds{a \equiv a_{r} + \ic a_{i}}$ with $\ds{a_{r}, a_{i} \in \mathbb{R}}$.

Then,
\begin{align}
y & =
{1 \over 4} +
{1 \over 16}\,\cos\pars{2x} + {1 \over 4}\,x\sin\pars{2x}\ +\
\overbrace{\quad\bracks{\rule{0pt}{5mm}a_{r}\sin\pars{2x} + a_{i}\cos\pars{2x}}\quad}^{\ds{Particular\ Solution}}
\\[5mm] &
a_{r}\ \mbox{and}\ a_{i}\ \mbox{are}\ determined\ \mbox{by the}\
Initial\ Conditions.
\end{align}
