Heat equation with time dependent boundary conditions? Suppose $u(x,t)$ solves 
\begin{align}
u_t&=u_{xx}, \qquad 0 < x < \pi/2,\ t>0,\\
u(0,t)&=e^{-t},\\
u(\pi/2, t)&=t,\\
u(x,0)&=\cos(3x).
\end{align}
I was following a method outlined in Boundaries in heat equation and set
$$u(x,t) = v(x,t) + e^{-t} + {2x \over \pi }\left(t - e^{-t}\right),$$
obtaining:
$$v_t - v_{xx} = e^{-t} - {2x \over \pi }\left(1 + e^{-t}\right).$$
 with a new boundary condition:
$$v(x,0) = \cos(3x) - 1 + {2x \over \pi}.$$ 
I then set $v(x,t) = \sum_{n=1}^\infty c(n,t)\sin(2\,x n)$, which I understand now satisfies the new homogeneous boundary conditions. Differentiating $v(x,t)$:
\begin{align}
v_t &=  \sum_{n=1}^\infty c_t(n,t)\sin(2x n),\\
v_{xx} &= -\sum_{n=1}^\infty c(n,t)4s^2\sin(2x n),
\end{align}
and substituting into the new equation gives:
$$\sum_{n=1}^\infty c_t(n,t)\sin(2x n) + \sum_{n=1}^\infty c(n,t)2n^2\sin(2x n) =  e^{-t} - {2x \over \pi }\left(1 + e^{-t}\right).$$
How do I find these coefficients $c(n,t)$?
 A: Let's back up a little bit and provide a comprehensive answer to these types of problems.
Suppose $u(x,t)$ solves 
\begin{align}
u_t&=u_{xx}, \qquad 0 < x < \ell,\ t>0,\\
u(0,t)&=f(t),\\
u(\ell, t)&=g(t),\\
u(x,0)&=h(x).
\end{align}
In the subsequent work, we will impose whatever smoothness conditions on the initial and boundary data we need to get convergence of the involved series.
First, standard separation of variables shows that the solution to the problem with homogeneous BCs is $$u(x,t)=\sum_{n=1}^\infty b_n\sin(\sqrt{\lambda_n}\,x)e^{-\lambda_n  t},$$ where $\lambda_n=(n\pi/\ell)^2$, $n=1,2,\dots$ In other words, for each fixed $t>0$,
$$u(x,t)=\sum_{n=1}^\infty u_n(t)\sin(\sqrt{\lambda_n}\,x)\quad\text{where}\quad u_n(t)={2\over \ell}\int_0^\ell u(x,t)\sin(\sqrt{\lambda_n}\,x)\,dx.$$ (This is a key observation. I hope the notation isn't confusing: $u_n$ represents the coefficients in the series for $u$, not a partial derivative.)
Then, differentiating the series above, define $v_n(t)$ and $w_n(t)$ as  the coefficients in the series for ${\partial u\over \partial t}$ and ${\partial^2 u\over \partial x^2}$, respectively:
\begin{align}
{\partial u\over \partial t}&=\sum_{n=1}^\infty v_n(t)\sin(\sqrt{\lambda_n}\,x)
\quad\text{where}\quad 
v_n={2\over \ell}\int_0^\ell {\partial u\over \partial t}\sin(\sqrt{\lambda_n}\,x)\,dx={du_n\over dt},\\
{\partial^2 u\over \partial x^2}&=\sum_{n=1}^\infty w_n(t)\sin(\sqrt{\lambda_n}\,x)
\quad\text{where}\quad 
w_n={2\over \ell}\int_0^\ell {\partial^2 u\over \partial x^2}\sin(\sqrt{\lambda_n}\,x)\,dx.
\end{align}
Integrating the $w_n(t)$ by parts, simplifying the trig terms, and applying the BCs, we get
\begin{align}
w_n(t)&=-{2\over \ell}\int_0^{\ell} \lambda_n u(x,t)\sin(\sqrt{\lambda_n}\,x)\,dx\\
&\qquad\qquad+{2\over \ell}\left[u_x(x,t)\sin(\sqrt{\lambda_n}\,x)-\sqrt{\lambda_n}\,u(x,t)\cos(\sqrt{\lambda_n}\,x)\right]\Bigg|_{x=0}^{x=\ell}\\
&=-\lambda_nu_n(t)+\underbrace{{2\sqrt{\lambda_n}\over \ell}\left[f(t)+(-1)^{n+1}g(t)\right]}_{F(t)}.
\end{align}
From the PDE, $$u_t=u_{xx}\implies v_n(t)=w_n(t)\implies {du_n\over dt}=-\lambda_n u_n(t)+F(t),$$ and thus the coefficients $u_n(t)$ we seek are found by solving the (ODE!) initial-value problem
\begin{align}
{du_n\over dt}+\lambda_nu_n(t)&=F(t),\\
u_n(0)&={2\over \ell}\int_0^\ell h(x)\sin(\sqrt{\lambda_n}\,x)\,dx,
\end{align}
by the method of your choice.
This is called the method of eigenfunction expansions. Transform methods are also available, but that is a separate post.
Hope that helps.
A: I would like to suggest another way to solve this problem starting from:
$$v_t-v_{xx}=f(x,t).$$
For the sake of simplicity, notations are close to the ones used in the previous answer.
One may use the Duhamel's Principle [Duhamel][1] by considering a homogeneous problem with the following initial condition:
$$v_t-v_{xx}=0\\
v(x,s)=f(x,s).$$
The solution of the homogeneous problem may be written:
$$ v(x,t;s)=\sum_{n=1}^{\infty}b_n(s)\sin{(\sqrt{\lambda_n}x)}\exp{(-\lambda_n(t-s))},$$
considering:
$$b_n(s)=\frac{2}{\pi/2}\int_0^{\pi/2}f(x,s)\sin{(\sqrt{\lambda_n}x)}dx.$$
By application of the Duhamel's Principle, the solution of the inhomogeneous problem is:
$$v(x,t)=\sum_{n=1}^{\infty}\sin{(\sqrt{\lambda_n}x)}\int_0^tb_n(s)\exp{(-\lambda_n(t-s))}ds.$$
Some useful examples for the heat equation are given here [2].
I hope this can help.
[1] https://en.wikipedia.org/wiki/Duhamel%27s_principle
[2] https://people.math.gatech.edu/~xchen/teach/pde/heat/Heat-Duhamel.pdf
