To solve the heat equation, using the separation of variables and decomposition into Fourier series usually works well.
Consider the homogeneous equation
\begin{align*}
& u_t-u_{xx}=0\\
& u_x(0,t)=u_x(\pi,t)=0
\end{align*}
whitout bothering about the initial condition. We wish to find solutions of the form $u(x,t)=c(t)v(x)$. Plugging this into the equation, we get
$$
c'(t)v(x)-c(t)v''(x)=0.
$$
This equation has to be true for any $x,t$, which means there exists $k\geq0$ such that $c'=\pm k^2 c$ and $v''=\pm k^2 v$. We only seek physically relevant solutions, so we can disregard the case $c'=k^2c$, because it would lead to a diverging solution when $t\to+\infty$. The equation for $v$ becomes $v'+k^2v=0$, and the general solution of this equation is
$$
\{x\mapsto A\cos(kx)+B\sin(kx),\;A,B\in\mathbb{R}\}
$$
The condition $u_x(0,t)=u_x(\pi,t)=0$ implies $B=0$ and $k\in\mathbb{N}$. Thus, for $k\in\mathbb{N}$, let
$$
\boxed{v_k:x\mapsto A_k\cos(kx)}.
$$
Note that this will be very convenient for Fourier series.
Now, what if we plug a function $u_k(x,t)=c_k(t)v_k(t)$, where $c_k$ is to be determined, into $u_t-u_{xx}$? We get
$$
(c'_k(t)+k^2c_k(t))A_k\cos(kx)
$$
We want to find $u(x,t)=\sum{k\geq0} u_k(x,t)$ such that it verifies the inhomogeneous equation, so we seek $c_k$ such that
$$
(c'_k(t)+k^2c_k(t))A_k\cos(kx)=tA_k\cos(kx)
$$
which is equivalent, assuming continuity, to
$$
c'_k(t)+k^2c_k(t)=t
$$
If $k=0$, the solution is $\boxed{c_0(t)=t^2/2+B_0}$.
Assume that $k>0$.
One solution to the homogeneous version of this equation is $\lambda(t)=\exp(-k^2t)$. Using the variation of parameters, $c_k=y\lambda$ with $y'(t)=t\exp(k^2t)$, thus
$$
\boxed{c_k:t\mapsto\frac{1}{k^4}(tk^2-1)+B_k\exp(-k^2t)}.
$$
Consider $u(x,t)=\sum_{k\geq0}c_k(t)v_k(x)$. We want $u$ to be a solution of the PDE.
$$
u_t-u_{xx}=\sum_{k\geq0} tA_k\cos(kx)=t\sum_{k\geq0} A_k\cos(kx)=tx
$$
thus
$$
A_k=\frac{2}{\pi}\int_0^\pi x\cos(kx)\mathrm{d}x=\begin{cases}\pi&\text{if }k=0 \\ \frac{2}{\pi}\frac{(-1)^k-1}{k^2} & \text{if not}\end{cases}
$$
Finally, we need to make sure $u$ verifies the boundary conditions. We already have $u_x(0,t)=u_x(\pi,t)=0$, so the only remaining problem is $u(x,0)=1$.
$$
u(x,0)=B_0A_0+\sum_{k\geq1}\left(B_k-\frac{1}{k^4}\right)v_k(x).
$$
Therefore, setting
$$
B_k=\begin{cases}1/A_0=1/\pi & \text{if }k=0 \\ \frac{1}{k^4} &\text{if not}\end{cases}
$$
solves the problem.
Here is an attempt:
Let $v(x,t) := u_t + u_x$. Then, we have that
$$v_t - v_x = u_{tt} + u_{xt} - u_{xt} - u_{xx} = f(x,t)$$
where
\begin{align*}
v(x,0) &= u_t (x,0) + u_x (x,0) \\
&= h(x) + g'(x)
\end{align*}
Now we let $w(s) = v(x-s,t+s)$, so that
\begin{align*}
w'(s) &= -v_x(x-s,t+s) + v_t(x-s,t+s) \\
&=f(x-s,t+s)
\end{align*}
Hence,
$$w(\tau) - w(-t) = \int_{-t}^{\tau} f(x-s,t+s)ds$$
so that,
\begin{align*}
w(\tau) &= w(-t) + \int_{-t}^{\tau} f(x-s,t+s)ds \\
&= v(x+t,0) + \int_{-t}^{\tau} f(x-s,t+s)ds
\end{align*}
Therefore,
$$v(x,t) = w(0) = h(x+t) + g'(x+t) + \int_{-t}^0 f(x-s,t+s)ds$$
So that we're left with:
$$\begin{cases} u_t + u_x &= v(x,t) \\ u_x (x,0) &= g(x) \\ u_t (x,0) &= h(x) \end{cases}$$
Now if we let $z(s):=u(x+s,t+s)$, then:
\begin{align*}
z'(s) &= u_x(x+s,t+s) + u_t(x+s,t+s) \\
&= v(x+s,t+s)
\end{align*}
Therefore,
$$z(\tau) - z(-t) = \int_{-t}^{\tau} v(x+s,t+s)ds$$
\begin{align*}
z(0) = u(x,t) &= u(x-t,0) + \int_{-t}^0 v(x+s,t+s)ds \\
&= g(x-t) + \int_{-t}^0 v(x+s,t+s)ds
\end{align*}
where $v(x,t)=h(x+t) + g'(x+t) + \displaystyle\int_{-t}^0 f(x-s,t+s)ds$.
Is this correct?
Best Answer
The way I was taught to solve boundary value problems with non-homogeneous boundary value conditions is via the introduction of a second term to satisfy the boundary, i.e. set $$ u(x,t) = \phi(x,t) + v(x,t)$$ where $\phi(x,t)$ satisfies the boundary conditions.
So, we need $$\phi_x(0,t)=0 \text{ and }\phi(L,t)=\sin^2\frac{t}{2}.$$ The simplest function that satisfies these conditions is $$\phi(x,t)=\sin^2\frac{t}{2},$$ which means $u$ is of the form $$u(x,t)=\sin^2\frac{t}{2}+v(x,t).$$
The pde now required to solve, for $v$ after substituting into your equation is $$v_t=v_{xx}-\cos\frac{t}{2}.$$ Substituting into the boundary conditions also gives $$v_x(0,t)=0\text{ and } v(L,t)=0$$ and the initial condition condition $$v(x,0)=0.$$
This transforms the original problem with non-homogeneous boundary conditions into one with homogeneous boundary conditions, but a non-homogeneous pde, which is easier to solve.
Can you continue from this point?