Let $u(x,t)=\sum\limits_{n=1}^\infty C(n,t)\sin n\pi x$ so that it automatically satisfies $u(0,t)=u(1,t)=0$ ,
Then $\sum\limits_{n=1}^\infty C_{tt}(n,t)\sin n\pi x+\sum\limits_{n=1}^\infty n^2\pi^2C(n,t)\sin n\pi x=\cos2t$
$\sum\limits_{n=1}^\infty(C_{tt}(n,t)+n^2\pi^2C(n,t))\sin n\pi x=\sum\limits_{n=1}^\infty2\cos2t\int_k^{k+1}\sin n\pi x~dx~\sin n\pi x$ , $\forall k\in\mathbb{Z}$ , $x\in(k,k+1)$
$\sum\limits_{n=1}^\infty(C_{tt}(n,t)+n^2\pi^2C(n,t))\sin n\pi x=\sum\limits_{n=1}^\infty2\cos2t\left[-\dfrac{\cos n\pi x}{n\pi}\right]_k^{k+1}\sin n\pi x$ , $\forall k\in\mathbb{Z}$ , $x\in(k,k+1)$
$\sum\limits_{n=1}^\infty(C_{tt}(n,t)+n^2\pi^2C(n,t))\sin n\pi x=\sum\limits_{n=1}^\infty\dfrac{2((-1)^{nk}-(-1)^{n(k+1)})\cos2t\sin n\pi x}{n\pi}$ , $\forall k\in\mathbb{Z}$ , $x\in(k,k+1)$
$\sum\limits_{n=1}^\infty(C_{tt}(n,t)+n^2\pi^2C(n,t))\sin n\pi x=\sum\limits_{n=1}^\infty\dfrac{2(1-(-1)^n)\text{sgn}(\sin\pi x)\cos2t\sin n\pi x}{n\pi}$
$\therefore C_{tt}(n,t)+n^2\pi^2C(n,t)=\dfrac{2(1-(-1)^n)\text{sgn}(\sin\pi x)\cos2t}{n\pi}$
$C(n,t)=C_1(n)\sin n\pi t+C_2(n)\cos n\pi t+\dfrac{2(1-(-1)^n)\text{sgn}(\sin\pi x)\cos2t}{n\pi(n^2\pi^2-4)}$
$\therefore u(x,t)=\sum\limits_{n=1}^\infty C_1(n)\sin n\pi x\sin n\pi t+\sum\limits_{n=1}^\infty C_2(n)\sin n\pi x\cos n\pi t+\sum\limits_{n=1}^\infty\dfrac{2(1-(-1)^n)\text{sgn}(\sin\pi x)\sin n\pi x\cos2t}{n\pi(n^2\pi^2-4)}$
$u(x,0)=0$ :
$\sum\limits_{n=1}^\infty C_2(n)\sin n\pi x+\sum\limits_{n=1}^\infty\dfrac{2(1-(-1)^n)\text{sgn}(\sin\pi x)\sin n\pi x}{n\pi(n^2\pi^2-4)}=0$
$\sum\limits_{n=1}^\infty C_2(n)\sin n\pi x=\sum\limits_{n=1}^\infty\dfrac{2((-1)^n-1)\text{sgn}(\sin\pi x)\sin n\pi x}{n\pi(n^2\pi^2-4)}$
$C_2(n)=\dfrac{2((-1)^n-1)\text{sgn}(\sin\pi x)}{n\pi(n^2\pi^2-4)}$
$\therefore u(x,t)=\sum\limits_{n=1}^\infty C_1(n)\sin n\pi x\sin n\pi t+\sum\limits_{n=1}^\infty\dfrac{2((-1)^n-1)\text{sgn}(\sin\pi x)\sin n\pi x\cos n\pi t}{n\pi(n^2\pi^2-4)}+\sum\limits_{n=1}^\infty\dfrac{2(1-(-1)^n)\text{sgn}(\sin\pi x)\sin n\pi x\cos2t}{n\pi(n^2\pi^2-4)}=\sum\limits_{n=1}^\infty C_1(n)\sin n\pi x\sin n\pi t-\sum\limits_{n=1}^\infty\dfrac{2~\text{sgn}(\sin\pi x)\sin((2n-1)\pi x)\cos((2n-1)\pi t)}{(2n-1)\pi((2n-1)^2\pi^2-4)}+\sum\limits_{n=1}^\infty\dfrac{2~\text{sgn}(\sin\pi x)\sin((2n-1)\pi x)\cos2t}{(2n-1)\pi((2n-1)^2\pi^2-4)}$
$u_t(x,t)=\sum\limits_{n=1}^\infty n\pi C_1(n)\sin n\pi x\cos n\pi t+\sum\limits_{n=1}^\infty\dfrac{2(2n-1)\pi~\text{sgn}(\sin\pi x)\sin((2n-1)\pi x)\sin((2n-1)\pi t)}{(2n-1)\pi((2n-1)^2\pi^2-4)}-\sum\limits_{n=1}^\infty\dfrac{4~\text{sgn}(\sin\pi x)\sin((2n-1)\pi x)\sin2t}{(2n-1)\pi((2n-1)^2\pi^2-4)}$
$u_t(x,0)=\sum\limits_{n=1}^\infty\sin2n\pi x$ :
$\sum\limits_{n=1}^\infty n\pi C_1(n)\sin n\pi x=\sum\limits_{n=1}^\infty\sin2n\pi x$
$n\pi C_1(n)=\begin{cases}0&\text{when}~n~\text{is an odd integer}\\1&\text{when}~n~\text{is an even integer}\end{cases}$
$C_1(n)=\begin{cases}0&\text{when}~n~\text{is an odd integer}\\\dfrac{1}{n\pi}&\text{when}~n~\text{is an even integer}\end{cases}$
$\therefore u(x,t)=\sum\limits_{n=1}^\infty\dfrac{\sin2n\pi x\sin2n\pi t}{2n\pi}-\sum\limits_{n=1}^\infty\dfrac{2~\text{sgn}(\sin\pi x)\sin((2n-1)\pi x)\cos((2n-1)\pi t)}{(2n-1)\pi((2n-1)^2\pi^2-4)}+\sum\limits_{n=1}^\infty\dfrac{2~\text{sgn}(\sin\pi x)\sin((2n-1)\pi x)\cos2t}{(2n-1)\pi((2n-1)^2\pi^2-4)}$
FIrst solve the equation
$$
u''=-\sin \pi x-\sin 2\pi x,
$$
subject to the boundary conditions
$$
u(0)=u(1)=0.
$$
This problem has solution
$$
u_{st}=\frac{1}{\pi^2}\sin \pi x+\frac{1}{2^2\pi^2}\sin 2\pi x.
$$
Now put $u(x,t)=u_{st}+v(x,t)$. Make sure that you understand that $v(x,t)$ solves the problem
$$
v_t=v_{xx},\\
v(0,t)=0,\\
v(1,t)=0,\\
v(x,0)=-u_{st}.
$$
Note the initial condition.
Now you can solve it to find
$$
v(x,t)=-\frac{1}{\pi^2}e^{-\pi^2 t}\sin \pi x-\frac{1}{2^2\pi^2}e^{-2^2\pi^2t}\sin 2\pi x.
$$
Finally write
$$
u(x,t)=\underbrace{v(x,t)}_{\mbox{transient part}}+\underbrace{u_{st}}_{\mbox{stationary part}}
$$
Best Answer
In order to solve non-homogeneous heat equations, assume that $w(x,t)=u(x,t)+\phi(x)$, where $\phi$ is a function yet to be determined.
So, we've $u_{xx} = w_{xx} - \phi''$ and $u_{t} = w_{t}$. If we substitute this in to our equation, then we have to solve the new problem
$\begin{cases} w_t - c^2 w_{xx} - c^2 \phi'' - \sin{5 \pi x} = 0\\ w(0,t) - \phi(0) = 0\\ w(1,t) = \phi(1) = 0\\ w(x,0) - \phi(x) = 4\sin{3\pi x} + 9\sin{7 \pi x} \end{cases} $
So we really want to choose $\phi$ so that $-c^2\phi^2 - \sin{5 \pi x} = 0$, the choice of $\phi = \frac{1}{25 \pi^2 c^2}\sin{5 \pi x}$ works. And so we want to solve the new equation:
$\begin{cases} w_t - c^2 w_{xx} = 0\\ w(0,t) = 0\\ w(1,t) = 0\\ w(x,0) = 4\sin{3\pi x} +\frac{1}{25 \pi^2 c^2}\sin{5 \pi x} + 9\sin{7 \pi x} \end{cases} $
At this point, this should be easy to do, separation of variables yields that the resulting corresponding Fourier series is a sine series, and we can easily match what the coefficients according to our initial condition. We have
$$w(x,t) = \sum_{n=1}^{\infty} b_n e^{-(cn\pi)^2t}\sin{n\pi x}$$
Matching coefficients, we see $b_3 = 4$, $b_5 = \frac{1}{25 \pi^2 c^2}$, and $b_7 = 9$, and $b_n = 0$ otherwise.
Thus we have
$$w(x, t) = 4 e^{-(3c\pi)^2t}\sin{3\pi x} + \frac{1}{25 \pi^2 c^2} e^{-(5c\pi)^2t}\sin{5\pi x} + 9 e^{-(7n\pi)^2t}\sin{7\pi x}$$
Now, remember that $u(x, t) = w(x, t) - \phi(x)$, the full solution is
$$u(x, t) = 4 e^{-(3c\pi)^2t}\sin{3\pi x} + \frac{1}{25 \pi^2 c^2} e^{-(5c\pi)^2t}\sin{5\pi x} + 9 e^{-(7n\pi)^2t}\sin{7\pi x} - \frac{1}{25 \pi^2 c^2}\sin{5 \pi x}$$