My first comment is that, using brute force, you could waste a lot of time and probably not get any result.
Let us apply the method to the differential equation $$y''-2y=x^2e^{x^2}$$ using $$y=\sum_{n=0}^\infty a_nx^n\implies y''=\sum_{n=0}^\infty n(n-1)a_nx^{n-2}$$ For the rhs, we shall use the classical $$e^z=\sum_{n=0}^\infty\frac {z^n}{n!}$$ in which we shall replace $z$ by $x^2$. Using all the above, the differential equation write $$\sum_{n=0}^\infty n(n-1)a_nx^{n-2}-2\sum_{n=0}^\infty a_nx^n=\sum_{n=0}^\infty\frac {x^{2n+2}}{n!}$$ The very first thing we can notice is that the rhs only contains even powers of $x$; this means that we shall have two recurrence relations, one for even values of the exponent and another one for the odd values of the exponent.
Let us start with an odd value of the exponent $m$ and consider the terms. To have $x^m$ in the first summation we need to make $n-2=m$ that is to say $n=m+2$. So, for this case, $$(m+1)(m+2)a_{m+2}-2a_m=0$$ which gives $$a_{m+2}=\frac{2}{(m+1)(m+2)}a_m$$
For an even value of the exponent $m$, the same will apply but the term to be used in the rhs will be such that $2n+2=m$ that is to say $n=\frac m2-1$. So,for this case, $$(m+1)(m+2)a_{m+2}-2a_m=\frac 1{(\frac m2 -1)!}$$which gives$$a_{m+2}=\frac{2a_m+\frac 1{(\frac m2 -1)!}}{(m+1)(m+2)}$$
To make it clearer, let us replace $m$ by $2k-1$ for odd values of $m$ and by $2k-2$ for even values of $m$. So,$$a_{2k+1}=\frac{1}{k(2k+1)}a_{2k-1}$$ $$a_{2k}=\frac{2a_{2k-2}+\frac 1{(k-2)!}}{2k(2k-1)}$$ and, as usual, $a_0$ and $a_1$ would be later fixed by conditions.
I hope and wish that this will clarify things for you. If this is not the case, just post.
$$ y''+ a[\sin(y)]+ b[\cos(y)] =f(x) \tag 1$$
$a[\sin(y)]+ b[\cos(y)]=\rho \sin(y+\phi)\quad\text{with}\quad \begin{cases}\rho=\sqrt{a^2+b^2}\\ \tan(\phi)=\frac{b}{a} \end{cases}$
Change of function :$\quad y(x)=u(x)-\phi\quad$ transform Eq.(1) into Eq.(2) :
$$u''+\rho\sin(u)=f(x) \tag 2$$
Change of variable :$\quad t=\sqrt{\rho}\:x\quad\to\quad \frac{d^2u}{dt^2}+\sin(u)=\frac{1}{\rho}f\left( \frac{t}{\sqrt{\rho}}\right)$
Let $\quad F(t)=\frac{1}{\rho}f\left( \frac{t}{\sqrt{\rho}}\right).\quad$ Since $\rho$ and $f$ are known, $F(t)$ is a known function.
$$\frac{d^2u}{dt^2}+\sin\left(u(t)\right)=F(t) \tag 3$$
Probably, no simpler form of equation (with no parameter inside) can be derived.
In the particular case $F(t)=0$ the solution can be expressed in terms of Jacobi elliptic function.
In the case $F(t)=C\neq 0$, Eq.(3) is an ODE of the autonomous kind. But the integration cannot be done in term of standard special functions.
A fortiori, in the general case $F(t)$ not constant, there is no closed form for the solution of the ODE.
This doesn't mean that closed form never exists in some particular cases. Just look backwards : Put a given function $y(x)$ into Eq.(1). This gives a particular function $f(x)$. For this function $f(x)$ , at least a closed form solution exist for Eq.(1) : the $y(x)$ a-priori chosen.
Thus, no definitive answer can be given to the question raised. This depends on the explicit definition of the function $f(x)$. But it is clear that analytical solving Eq.(1) is not possible in general.
Best Answer
Here's the general procedure. Where are you up to?