Integrate using the method of undetermined coefficients

If you differentiate once again, you get

$$P''(x)+9P(x)=3x^2$$ thus $$\deg(P)=2$$

and $$Q''(x)=3x^3-9Q(x)$$ then $$\deg(Q)=3$$


We know that the degree of $Q$ is one more than the degree of $P$. Taking the derivative of $P$ only makes the degree smaller, so in the expression $P'(x)+3Q(x)$, $3Q(x)$ has higher degree than $P'(x)$. This means that the highest power of $x$ appearing in $Q(x)$ cannot be cancelled out by $P'(x)$. So if $P'(x)+3Q(x)=x^3$, then the $x^3$ must come from the highest-degree term of $3Q(x)$, so $Q$ has degree $3$.

It should not be a surprise that you were still able to find the same answer by saying $Q$ has terms up to degree $5$, though. If you write $$Q(x)=ax^5+bx^4+cx^3+dx^2+ex+f,$$ this equation does NOT mean that $Q$ has degree $5$, since $a$ could be $0$. What this equation says is that $Q$ has degree at most $5$. So using this representation of $Q$, you can find any solution where $Q$ is a polynomial of degree at most $5$, including the case where $Q$ has degree $3$ (in that case you will just have $a=b=0$).