Do I use initial condition or boundary condtion? Notation: 


*

*$w$ denotes approximate solution of $u$

*$f(x)$ denotes initial condition.

*$l(t)$ and $r(t)$ are the boundary conditions.


Right now I'm trying to write some code to solve a simple heat equation: 
$$u_t = D\cdot u_{xx},\,\ 0\leq x\leq M, \,t\geq0$$
The method I'm using is a Forward Difference Method to step forward in time. For the very first time step, I have to use the extremes, $w_{0,0}$ and $w_{M,0}$ in order to calculate $w_{1,1}$ and $w_{M-1,1}$. I have to choose between using the initial conditions $f(x_0)$ and $f(x_M)$, or the boundary conditions $l(0)$ and $r(0)$, which should I use?
 A: I'd think you'd need both, with a caveat.
The initial condition $f(x)$, I'd think, would be for all positions $x_i$ that you're considering. You need to know what the heat distribution looks like everywhere at the beginning. The boundary conditions $l(t), r(t)$ should be consistent with the initial conditions at the beginning ($t=0$). They're the "forcing function" for how the heat is applied at each end, and serve to change the values only at $0$ and $M$ as a function of time.
So ... set your $f(x)$ at the beginning everywhere on $[0,M]$ and then run from there.
A: There are different ways to handle boundary conditions in spatial finite difference methods. The cleanest way I know to deal with the Dirichlet case is to think of the boundary values $w_{0,0}$ and $w_{N,0}$ as frozen, and never take spatial derivatives there. (I write $N$ instead of $M$ because there might be more or fewer than $M+1$ spatial grid points depending on the fineness of the grid.) Then the second spatial derivatives at $(1, t)$ and $(N-1, t)$ are determined using $l( t)$ and $r( t)$, respectively.
There should usually not be any ambiguity here at time $0$, because you should have $f(0)=l(0)$ and $f(M)=r(0)$. If you do not, then the solution is not strong (there is a breakdown of regularity at the corners), so you should not expect naive finite differencing to behave properly. In such a case you should either:


*

*Use a method which makes explicit reference to the weak formulation of the problem, such as the finite element method.

*Solve a heuristically regularized version of your problem. For example, if $l(0)=f(0)$ but $r(0) \neq f(M)$, then you could consider $\tilde{r}(t)=e^{-t/\epsilon}f(M)+(1-e^{-t/\epsilon})r(t)$ where $0<\epsilon \ll 1$ is a regularization parameter. 


Note that this does actually happen. For example, if $u(t,x)$ is the probability for a Brownian motion started at $x$ to hit $0$ before hitting $1$ and before time $t$ has elapsed, then it satisfies the problem:
$$u_t=\frac{1}{2} u_{xx} \\
u(0,x)=0 \\
u(t,0)=1 \\
u(t,1)=0$$
which is badly behaved at $(t,x)=(0,0)$.
