I am trying to find some resources to help explain how to choose boundary conditions when using finite difference methods to solve PDEs.The books and notes which I currently have access to all say similar things:The general rules governing stability in the presence of boundaries are far too complicated for an introductory text; they require sophisticated mathematical machinery(A. Iserles A First Course in the Numerical Analysis of Differential Equations)For example, when trying to implement the 2-step leapfrog method for the advection equation:$u_i^{n+1} = u_i^{n-1} + \mu (u_{i+1}^n - u_{i-1}^n)$using MATLABM = 100; N = 100;mu = 0.5;c = [mu 0 -mu];f = @(x)(exp(-100*(x-0.5).^2));u  = zeros (M, N);x = 1/(M+1) * (1:M);u(:,1) = f(x);u(:,2) = f(x + mu/(M+1));for i = 3:N    hold off;    u(:,i) = conv(u(:,i-1),c,'same') + u(:,i-2);    plot(x, u(:,i));    axis( [ 0 1 0 2] )    drawnow;endThe solution behaves nicely until it reaches the boundary, when it very suddenly starts behaving badly.Where can I learn how to handle boundary conditions like this?
Boundary conditions for the advection equation discretized by a finite difference method
pde;finite difference;boundary conditions;advection
Sloede's response is very through and correct. I just wanted to add a few points to make is easier to grasp. Basically, any wave equation has an inherent wave speed and direction. For a one dimensional wave equation:  $$ u_t + a u_x = 0 $$ the wave speed is the constant $a$ which determines not only the speed at which the information is propagating in the domain, but also it's direction. If $a>0$, information is going from left to right and if $a<0$ it's the other way around.For the leap frog method, when you discretize the equations you get: $$\frac{u_i^{n} - u_i^{n-2}}{2 \Delta t} + a \frac{u_{i+1}^{n-1} - u_{i-1}^{n-1}}{2 \Delta x} = 0$$or:$$u_i^{n} = u_i^{n-2} + \mu (u_{i+1}^{n-1} - u_{i-1}^{n-1}) $$ where $\mu = -a \Delta t/\Delta x$. In your case, $\mu > 0$ which translates to a wave going to the left. Now if you think about it, a wave that is traveling to the left, will only need a boundary condition at the right boundary since all the values to the left are being updated via their right neighbors. In fact specifying any value at the left boundary is inconsistent with the nature of the problem. In certain methods, like simple upwind, this is taken care of automatically since the scheme also involves only the correct neighbors in it's stencil. In other methods, like the leap frog, you have to specify some correct value. This is usually done via extrapolation from the inside domain to find the missing value. For multi-dimensional and non-canonical  problems, this involves finding all the eigen-vectors of the flux Jacobian to determine which parts of the boundary actually need boundary conditions and which parts require extrapolation. 