The problem in question is
$$
u_t-u_{xx}=2 \\
u(0,t)=0,\; u_x(1,t)=1 \\
u(x,0)=-x^2
$$
This needs to be transformed to a homogeneous problem in order for separation of variables to work. One way to do this is to add a function $f(x)$ to $u$ so that the differential equation and endpoint conditions are homogeneous. That requires finding a function $f$ to satisfy the following:
$$
(u+f)_t-(u+f)_{xx}=0 \implies f''(x)=2 \\
u(0,t)+f(0)=0 \implies f(0)=0 \\
u_x(1,t)+f'(1)=0 \implies f'(1)=-1
$$
$f(x)=x^2-3x$ is such a solution. (Thank you @Luthier415Hz for pointing out my errors and confusion about this.)
The original problem for $u$ has now been transformed to a problem in $v=u+f$ that satisfies the following:
$$
v_t=v_{xx} \\
v(0,t)=0,\;\; v_x(1,t)=0, \\
v(x,0) = -3x.
$$
Separation of variables can be used to directly solve this problem because of the homogenous endpoint conditions.
The desired solution is $u=v-f$. To solve for $v$ using separation of variables, assume $v(t,x)=T(t)X(x)$. This will work because of the homogeneous endpoint conditions in $x$:
$$
\frac{T'(t)}{T(t)}=\lambda = \frac{X''(x)}{X(x)} \\
X(0)=0,\;\; X'(1)=0.
$$
The $X$ equation has solutions that are determined only up to a constant $C$:
$$
X_n(x) = C_n\sin((n+1/2)\pi x),\;\; n=0,1,2,3,\cdots.
$$
The corresponding eigenvalue parameter $\lambda$ is
$$
\lambda_n = -(n+1/2)^2\pi^2,\;\; n=0,1,2,3,\cdots.
$$
And the corresponding solution $T_n$ is any constant multiple of
$$
T_n(t) = \exp(-(n+1/2)^2\pi^2 t)
$$
This leads to the general solution for $v$:
$$
v(x,t) = \sum_{n=0}^{\infty}C_n \exp(-(n+1/2)^2\pi^2 t)\sin((n+1/2)\pi x).
$$
The constants $C_n$ are determined by the condition $v(x,0)=-3x$, through the orthogonality of the eigenfunctions $\{\sin((n+1/2)\pi x)\}_{n=0}^{\infty}$:
$$
-3x = v(x,0)= \sum_{n=0}^{\infty}C_n\sin((n+1/2)\pi x) \\
\frac{\int_0^1 (-3x)\sin((n+1/2)\pi x)dx}{\int_0^1\sin^2((n+1/2)\pi x)dx}= C_n,\;\;\; n=0,1,2,3,\cdots.
$$
What remains is to determine the constants $C_n$ by evaluating the integrals in the corresponding fractions above. The numerator for $C_n$ is
\begin{align}
&\int_0^1 (-3x)\sin((n+1/2)\pi x)dx \\
&= \left.\frac{3x\cos((n+1/2)\pi x)}{(n+1/2)\pi}\right|_{0}^{1} \\
& - \int_0^1 3\frac{\cos((n+1/2)\pi x)}{(n+1/2)\pi}dx \\
&= \left.-3\frac{\sin((n+1/2)\pi x)}{(n+1/2)^2\pi^2}\right|_{x=0}^{1} \\
&= \frac{3(-1)^{n+1}}{(n+1/2)^2\pi^2}
\end{align}
The denominator for $C_n$ is
\begin{align}
&\int_0^1\sin^2((n+1/2)\pi x)dx \\
& = \left.\frac{-\cos((n+1/2)\pi x)}{(n+1/2)\pi}\sin((n+1/2)\pi x)\right|_{x=0}^{1} \\
& + \int_0^1\cos^2((n+1/2)^2\pi x)dx \\
& = \int_0^1\cos^2((n+1/2)^2\pi x)dx
\end{align}
Therefore,
\begin{align}
&\int_0^1\sin^2((n+1/2)\pi x)dx \\
&=\frac{1}{2} \int_0^1\sin^2((n+1/2)\pi x)+\cos^2((n+1/2)^2\pi x) dx \\
&=\frac{1}{2}.
\end{align}
Finally,
$$
C_n = \frac{6(-1)^{n+1}}{(n+1/2)^2\pi^2}
$$
The full expression for $v$ is
\begin{align}
&v(x,t)=\sum_{n=0}^{\infty}C_n \exp(-(n+1/2)^2\pi^2 t)\sin((n+1/2)\pi x) \\
&= \sum_{n=0}^{\infty}\frac{6(-1)^{n+1}}{(n+1/2)^2\pi^2}\exp(-(n+1/2)^2\pi^2 t)\sin((n+1/2)\pi x).
\end{align}
The desired solution is $u=v-f=v-(x^2-3x)$:
\begin{align}
%% u(x,t)&=v(x,t)-(x^2-3x) \\
&u(x,t)= -x^2+3x \\
&+\sum_{n=0}^{\infty}\frac{6(-1)^{n+1}}{(n+1/2)^2\pi^2}\exp(-(n+1/2)^2\pi^2 t)\sin((n+1/2)\pi x)
\end{align}
NOTE: If I have all the details for this right, I'll be shocked!
Best Answer
It is interesting that your initial condition does not satisfy the endpoint conditions. But, that's okay. To have the desired periodicity in $x$, you can use $$ u(t,x) = \sum_{n=-\infty}^{\infty}a_n(t)e^{inx}. $$ This solution satisfies the requirements of periodicity. In order for this to be a full solution, you'll need $u_t=u_{xx}$, which leads to $$ a_n'(t) = -n^2a_n(t) \implies a_n(t) = C_ne^{-n^2 t}. $$ The constants $C_n$ are determined by the initial condition $u(0,x)=x$: $$ x = \sum_{n=-\infty}^{\infty}a_n(0)e^{inx}=\sum_{n=-\infty}^{\infty}C_n e^{inx}. \\ C_n = \frac{1}{2\pi}\int_{0}^{2\pi}xe^{-inx}dx. $$