$$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.
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 $$