How do I find an $A_0$ and $A_n$ which satisfy the initial conditions of this heat equation? Let's say I have the heat equation $\frac {\partial u}{\partial t} = k\frac {\partial^2 u}{\partial x^2}$, $0 \lt x \lt L$, $t \gt 0$, subject to the boundary conditions
$$\begin{cases} \frac {\partial u}{\partial x}(0, t) = 0, & t \gt 0 \\ \frac {\partial u}{\partial x}(L, t) = 0, & t \gt 0, \end{cases}$$
$L$ of course being the length of the rod in one dimension.
I also have the initial conditions $\begin{equation}
  u(x, 0)  = \left \{
  \begin{aligned}
    &0 && \ x \lt L/2 \\
    &1 && \ x \gt L/2
  \end{aligned} \right.
\end{equation} $
Now I know how to solve for a general solution (everything but the initial conditions satisfied) for $u(x, t)$, and I have that
$u(x, t)$ = $A_0 + \sum ^\infty _{n=1} A_n\cos(\frac {n \pi x}{L})e^{-k \lambda t} $.
I also know how to show by the orthogonality of cosines that 
$A_0 = \frac {1}{L} \int^L _0 f(x)dx $ and $A_n = \frac {2}{L} \int ^L _0 f(x)\cos(\frac {n \pi x}{L})dx$.
How do I find an $A_0$ and $A_n$ which satisfy the initial conditions?
From what I can tell, I need to find $f(x)$ such that 
$$\frac{1}{L} \int^{\frac {L}{2}}_0 f(x)dx + \sum^ \infty _{n=1}(\frac{2}{L} \int^{\frac{L}{2}}_0 f(x)\cos (\frac{n \pi x}{L})dx) \cos( \frac {n \pi x}{L})) = 0$$ 
and
$$\frac{1}{L} \int^{L}_\frac {L}{2} f(x)dx + \sum^ \infty _{n=1}(\frac{2}{L} \int^{L}_\frac{2}{L} f(x)\cos (\frac{n \pi x}{L})dx) \cos( \frac {n \pi x}{L})) = 1$$
and then use whatever $f(x)$ this is to find $A_0$ and $A_n$ above.
Also, I happen to know that the solution is that $A_0 = \frac {1}{2}, A_n = -\frac {2}{n \pi}\sin(\frac{n \pi}{2})$ when $n \neq 0$.
Why is this true though and how do I show that this is the solution ?
 A: Carry out the procedure of separation of variables, which starts by finding solutions of the form $u(x,t)=X(x)T(t)$
$$
                           \frac{T'}{T}=k\frac{X''}{X},\;\;\; X'(0)=X'(L)=0.
$$
The equation separates
$$
                   T'=\lambda T, \;\;X''=\frac{\lambda}{k}X.
$$
If $\lambda > 0$, then $X(x)=Ae^{\sqrt{\lambda}x}+Be^{-\sqrt{\lambda}x}$, and you don't get solutions for which $X'(0)=X'(L)=0$. If $\lambda \le 0$, then you can start by solving for $X$ such that $X(0)=1$, $X'(0)=0$ in order to narrow to functions
$$
                          X(x)=\cos(\sqrt{-\lambda/k}x)
$$
Then the equation for $\lambda$ is determined by the requirement that $X'(L)=0$:
$$
                        \sqrt{-\lambda/k}\sin(\sqrt{-\lambda/k}L)=0,\\
                 \sqrt{-\lambda/k}L = 0,\pi,2\pi,3\pi,\cdots,\\
              \lambda = -\frac{kn^2\pi^2}{L^2},\;\;\; n=0,1,2,3,\cdots.
$$
The solutions in $T$ are then
$$
                         T(t) = e^{-\lambda t} = e^{-kn^2\pi^2 t/L^2}
$$
The full solution is
$$
              u(x,t) = \sum_{n=0}^{\infty}C_n e^{-kn^2\pi^2 t/L^2}\cos(n\pi x/L)
$$
The initial condition $u(x,t)$ determines $C_n$:
$$
                 u(x,0)=\sum_{n=0}^{\infty}C_n\cos(n\pi x/L),
$$
where $u(x,0)=\chi_{[L/2,L]}$ is $0$ for $0 \le x < L/2$ and is $1$ for $L/2 \le x \le L$. All of the $\cos(n\pi x/L)$ are mutually orthogonal in $L^2[0,L]$. So multiply by $\cos(m\pi x/L)$ and integrate over $[0,L]$ to obtain the coefficent $C_m$:
$$
  \int_{0}^{L}\chi_{[L/2,L]}\cos(m\pi x/L)dx=C_m\int_{0}^{L}\cos^2(m\pi x/L)dx \\
     \int_{L/2}^{L}\cos(m\pi x/L)dx=C_m \frac{L}{2}
$$
For $m=0$, you get
$$
                     L/2 = LC_0/2 \implies C_0 = 1
$$
For $m\ne 0$, you get
$$
             \left. \frac{\sin(m\pi x/L)}{m\pi/L}\right|_{x=L/2}^{x=L} = C_m\frac{L}{2}\\
                -2\frac{\sin(m\pi/2)}{m\pi}=C_m, \;\;\; m=1,2,3,\cdots.
$$
