Solve the BVP $$\begin{cases} u''+a^2u=\sin\pi x, 0<x<1 \\ u(0)=1,\quad u(1)=-2 \end{cases}$$ for all $a\in\mathbb{R}$.
**
My attempt
**
The homogeneous equation is $$u_h=C_1\cos(ax)+C_2\sin(ax).$$
The particular equation is $$u_p=C_3x\sin(\pi x)+C_4x\cos(\pi x),$$ $$u_p'=-C_3\pi x\cos(\pi x)+C_3\sin(\pi x)-C_4\pi x\sin(\pi x)+C_4\cos(\pi x),$$ $$u_p''=-C_3\pi^2 x\sin(\pi x)+2C_3\pi\cos(\pi x)-2C_4\pi\sin(\pi x)-C_4\pi^2 x\cos(\pi x).$$
Substituting for the given ODE, I have \begin{multline*} u_p''+\pi^2u = -C_3\pi^2 x\sin(\pi x)+2C_3\pi\cos(\pi x)-2C_4\sin(\pi x)-C_4\pi^2 x\cos(\pi x) \\ +\pi^2(C_3x\sin(\pi x)+C_4x\cos(\pi x))=\sin(\pi x), \end{multline*}
and I get $C_3=0$ and $C_4=-\frac{1}{2\pi}$. Then the general solution is, $$u=u_h+u_p=C_1\cos(ax)+C_2\sin(ax)-\frac{1}{2\pi}x\cos(\pi x)$$
Using the given initial conditions, I get $C_1=1$ and $C_2=\frac{-2-\frac{1}{2\pi}-\cos(a)}{\sin(a)}$. Then the general solution is,
$$u=u_h+u_p=\cos(ax)+\frac{-2-\frac{1}{2\pi}-\cos(a)}{\sin(a)}\sin(ax)-\frac{1}{2\pi}x\cos(\pi x)$$
Note that if $a=\pm\pi$ then $\sin(\pm\pi)=0$, which indicates $u$ is undefined at $a=\pm\pi$.
I am not confident that I defined $C_2$ correctly.
$$\begin{cases} u''+a^2u=\sin\pi x, 0<x<1 \\ u(0)=1,\quad u(1)=-2 \end{cases}$$
OK for $\quad u_h=C_1\cos(ax)+C_2\sin(ax).$
FIRST CASE : $a\neq\pm\pi$
$u_h(x)$ isn't linearly related to the nonhomogeneous term $\sin(\pi x)$. So, $u_p$ isn't on the form $C_3x\sin(\pi x)+C_4x\cos(\pi x)$ but on the form : $$u_p=C_3\sin(\pi x)+C_4\cos(\pi x)$$ I suppose that you can take it from here. You will get to : $$u_p=\frac{1}{a^2-\pi^2}\sin(\pi x)$$ $$u=C_1\cos(ax)+C_2\sin(ax)+\frac{1}{a^2-\pi^2}\sin(\pi x)$$ The condition $u(0)=0$ leads to $C_1=0$
The condition $u(1)=-2=C_2\sin(a)+\frac{1}{a^2-\pi^2}\sin(\pi)=C_2\sin(a)$ leads to $C_2=\frac{-2}{\sin(a)}$.
SECOND CASE : $a=\pm\pi$
$$u_h=C_1\cos(\pi x)+C_2\sin(\pi x).$$
$u_h(x)$ is linearly related to the nonhomogeneous term $\sin(\pi x)$. So, $u_p$ is on the form $C_3x\sin(\pi x)+C_4x\cos(\pi x)$
That is what you correctly did, up to $$u=u_h+u_p=C_1\cos(\pi x)+C_2\sin(\pi x)-\frac{1}{2\pi}x\cos(\pi x)$$ Don't let $a$ into it since the value of $a$ is known.
The condition $u(0)=0$ leads to $C_1=0 \quad\to\quad u=C_2\sin(\pi x)-\frac{1}{2\pi}x\cos(\pi x)$
The condition $u(1)=-2=u=C_2\sin(\pi )-\frac{1}{2\pi}\cos(\pi )=\frac{1}{2\pi}$ is not consistent. Thus in the case $a=\pm\pi$ there is no solution.