Eigenfunction expansion of second order PDE with time dependent boundary conditions I want to solve this 2nd order PDE-
$$
u_t = K u_{xx} \\
u(x=0,t)=f(t) \\
u(x=L,t)=g(t) \\
u(x,t=0)=\phi(x)
$$
Based on this answer - Heat equation with time dependent boundary conditions?, I solved it and got the answer as mentioned in the question as far as I understood -
$$
u(x,t)=\sum_{n=1}^\infty u_n(t) sin(\lambda_n x) \\
\lambda_n=\frac{n \pi}{L} \\
u_n(t)=e^{-\lambda_n^2Kt}\int e^{\lambda_n^2Kt}F(t)dt \\
F(t)=\frac{2\lambda_n}{L}[f(t)+(-1)^{n+1}g(t)] \\
u_n(0)=\frac{2}{L}\int_o^L \phi(x) sin(\lambda_nx)dx
$$
What I don't understand is how does this solution satisfy the boundary conditions? At $x=0$, $sin(\lambda_n x) $ will always be zero and thus $u(x=0,t)=0$ instead of $u(x=0,t)=f(t)$.
I thought I understood the solution but I guess not and I don't know if I am doing something wrong entirely. Because no matter how I solve $u_n(t)$ and what the solution for that is, $u(x=0,t)$ is always zero.
 A: From @Cretin2's comment I figured what the original answer was missing was splitting the equation into homogenous and inhomogeneous parts.So I start with that:-
$$
u_t=Ku_{xx} \\
u(x=0,t)=f(t) \\
u(x=L,t)=g(t) \\
u(x,t=0)=\phi(x)\\
$$
$u$ is composed of a homogenous and inhomogeneous term, represented by $u_h$ and $u_{ih}$ respectively
$$
u=u_{ih}+u_h \\
\frac{\partial^2u_{ih}}{\partial x^2}=0 \\
\implies u_{ih}=c_1x+c_2 \\
$$
Substituting-
$$
u_{ih}(x=0,t)=f(t) \\
u_{ih}(x=L,t)=g(t) \\
$$
we get,
$$
c_2=f(t) \\
c_1=\frac{g(t)-f(t)}{L} \\
\implies u_{ih}=\frac{g(t)-f(t)}{L}x+f(t)
$$
Plugging this into the equation for $u$, we get,
$$
u=\frac{g(t)-f(t)}{L}x+f(t)+u_h \\
\frac{\partial u}{\partial t}=\frac{\dot{g(t)}-\dot{f(t)}}{L}x+\dot{f(t)} + \frac{\partial u_h}{\partial t} \\
\frac{\partial^2 u}{\partial x^2}=\frac{\partial^2 u_h}{\partial x^2} \\
\implies \frac{\partial u_h}{\partial t}=K\frac{\partial^2 u_h}{\partial x^2}+\frac{\dot{f(t)}-\dot{g(t)}}{L}x-\dot{f(t)}
$$
Let $\frac{\dot{f(t)}-\dot{g(t)}}{L}x-\dot{f(t)}=F(x,t)$ which is now the "source term"
$$
\frac{\partial u_h}{\partial t}=\frac{\partial^2 u_h}{\partial x^2}+F(x,t)
$$
with boundary conditions :-
$$
u_h(x=0,t)=0 \\
u_h(x=L,t)=0 \\
u_h(x,t=0)=u(x,t=0)-u_{ih}(x,t=0) \\
\implies u_h(x,t=0)=\phi(x)-\frac{g(0)-f(0)}{L}x-f(0)=\psi(x)
$$
To solve this PDE, I can now use eigenfunction epansion. If there was no source term, by using separation of variables we could solve $u_h$
$$
u_h(x,t)=\sum_{n=1}^\infty u_n(t) \sin(\lambda_n x) \\
\lambda=\frac{n \pi}{L}
$$
Where $u_n(t)$ is the time dependent factor of $u_h(x,t)$.
$$
\frac{\partial u_h}{\partial t}=\sum_{n=1}^\infty v_n(t) \sin(\lambda_n x) \\
\frac{\partial^2 u_h}{\partial x^2}=\sum_{n=1}^\infty w_n(t) \sin(\lambda_n x) \\
$$
where,
$$
v_n(t)=\frac{d u_n}{dt} \\
w_n(t)=-\lambda_n^2 u_n(t)
$$
Let $F(x,t)=\sum_{n=1}^\infty f_n(t) \sin(\lambda_n x)$
Wecan substitute all of these in the original equation and after some simplification we get,
$$
\frac{du_n}{dt}=-K\lambda_n^2u_n(t)+f_n(t)
$$
This is a simple ODE in $u_n$ which I solved by multiplying by an integrating factor to get -
$$
u_n(t)=e^{-\lambda_n^2Kt}\int_0^t e^{\lambda_n^2 K\tau}f_n(\tau) d\tau + e^{-\lambda_n^2Kt} c
$$
where $c$ is a constant of integration
$$
u_n(0)=0+c
$$
So we need to now find u_n(0) and f_n(t). We know,
$$
\int_0^L \sin(\lambda_mx)\sin(\lambda_nx) dx=0, m\neq n \\
= L/2, m=n
$$
we have,
$$
u_h(x,t)=\sum_{n=1}^\infty u_n(t) \sin(\lambda_n x) \\
\int_0^L u_h(x,t)\sin(\lambda_n x) dx = u_n(t) L/2 \\
u_n(0)=\frac{2}{L}\int_0^L u(x,t=0) \sin(\lambda_nx) dx \\
\implies u_n(0)=\frac{2}{L}\int_0^L \psi(x) \sin(\lambda_nx) dx
$$
Similarly for $f_n(t)$ we have,
$$
f_n(t)=\frac{2}{L}\int_0^L F(x,t) \sin(\lambda_n x) dx
$$
Plugging everything in, we get,
$$
u(x,t)=\frac{g(t)-f(t)}{L}x+f(t)+\sum_{n=1}^\infty u_n(t) \sin(\frac{n \pi}{L} x) \\
u_n(t)=e^{-(\frac{n \pi}{L})^2 Kt}\int_0^t e^{(\frac{n \pi}{L})^2 K\tau}\frac{2}{L}\int_0^L \frac{\dot{f(\tau)}-\dot{g(\tau)}}{L}x-\dot{f(\tau)} \sin(\frac{n \pi}{L} x) dx d\tau \\
+ e^{-(\frac{n \pi}{L})^2Kt} \frac{2}{L}\int_0^L \psi(x) \sin(\frac{n\pi}{L}x) dx
$$
This satisfies all the BCs without the need to introduce any additional conditions
A: The equation is $$\partial^2_x u(x,t)=K\partial_t u(x,t)$$
Boundary : $$u(\pm ct,t)=0$$
Eigenfunctions of the lhs operator with the boundary : $$v_n(x,t)=\cos\left((2n+1)\frac{\pi x}{2ct}\right)$$
Expansion in the base obtained : $$u(x,t)=\sum_{n=0}^\infty a_n(t)v_n(x,t)$$
Plugging in the original function :
$$-\sum_n (2n+1)^2\frac{\pi^2}{4c^2t^2} a_n(t)v_n(x,t)=K\sum_n \dot{a}_n(t)v_n(x,t)+a_n(t)(2n+1)\frac{\pi x}{2ct^2}\sin\left((2n+1)\frac{\pi x}{2ct}\right)$$
Projection : $$\int_{-ct}^{ct}v_m(x,t)\cdot dx$$ based on :
$$\int_D\cos(ax)\cos(bx)dx=\frac{a\sin(ax)\cos(bx)}{a^2-b^2}-b\frac{\cos(ax)\sin(bx)}{a^2-b^2}=0,a\neq b$$ due to vanishing of cosines at boundary
$$\int_D \cos(ax)\cos(ax)dx=\frac{\sin(ax)\cos(ax)}{2a}+x/2=ct$$
$$\int_D x\sin(ax)\cos(bx)dx=1/2\left(\frac{\sin(x(a-b))}{(a-b)^2}+\frac{\sin(x(a+b))}{(a+b)^2}-\frac{x\cos(x(a-b))}{(a-b)}-\frac{x\cos(x(a+b))}{(a+b)}\right)=c(-1)^{m+n}\frac{2n+1}{(m+n)(m+n+1)}=C_{mn}, a\neq b$$
The sines vanish because sum and difference of 2 odd numbers is even.
You were right, This is where I made the mistake, the matrix is not diagonal but full.
Remains
$$\int_D x\sin(ax)\cos(ax)dx=ct/2a=\frac{c^2t^2}{(2n+1)\pi}=b_n(t)$$
Let $D_{mn}(t)=b_n(t)\delta_{mn}+C_{mn}$
The equation for the coefficients is :
$$-K^{-1}\left((2n+1)^2\frac{\pi^2}{4ct}\right)a_n(t)=\dot{a}_n(t)ct+\sum_{m=0}^\infty D_{nm}(t)a_m(t)$$
Hence we get an infinite system of odes written as a matrix system of the form : $$\dot{\vec{a}}(t)=F(t)\vec{a}(t)\Rightarrow \vec{a}(t)=e^{\int_0^t F(s)ds}\vec{a}(0)$$
