Consider $\mathcal{L}u(x) = -\Delta u(x,y) = b(x,y)$ on the domain $\Omega = (0,1)^2$ with boundary conditions $u(x,y) = g(x,y)$.
Exercise: Set-up the discrete problem (matrix and right-hand side) for the standard five-point discretization of $\mathcal{L}$ and $h = \frac{1}{3}$
a) without elimination of boundary conditions.
b) with elimination of boundary conditions.
using a lexicographic ordering of grid points in both.
Question: how do I handle/solve this?
Thanks in advance!
Imagine a square grid with inner grid distance $1/3$. Since we are working on $(0,1)^2$ we this grid will consist of $4 \times 4$ grid points.
We start numbering them row by row: the left bottom corner corresponding to $u(0,0)$ is first, the right bottom corner $u(1,0)$ will be fourth and so on. See the image below.
Now, lets figure out what must hold for the different grid points. The outer "ring" of points corresponds to the BC and there we simply want $u(x,y) = g(x,y)$. In the discrete scheme (which will consist of a $4^2 \times 4^2$ matrix A, $4^2 \times 1$ vector $u$ we want to solve for and RHS vector $b$) we can set a $1$ on the main diagonal in the row corresponding to the point number $k$ and the we set $k$'th value of $b$ equal to $g(x,y)$.
For example: 5'th row of matrix $A$ will read $[0, 0, 0, 0, 1, 0, .... 0]$ and 5'th entry of vector $b$ would be $g(0, 1/3)$.
For the interior points we must have: $-\nabla^2 u = b$. We approximate $\nabla^2 u$ with the $5$ point stencil. It states:
$$\nabla^2 u_{i,j} \approx \frac{u_{i-1,j} + u_{i+1,j} + u_{i,j-1} + u_{i,j+1} - 4 u_{i,j}}{h^2}$$
Where $i,j$ correspond to rows and columns of the grid. How to translate $i,j$ into our lexicographical ordering? Well, neighbors to the left and right ($u_{i-1,j}, u_{i+1,j}$ are easy enough - if we are on node $k$ then $k-1, k+1$ are nodes you are looking for. To get a step up or down we need to go through a whole row - so in our case $4$. Thus, we are looking at $k - 4, k+4$.
The row of $A$ corresponding to a interior point should therefore read: $\frac{1}{h^2} [0, ..., 1, 0, 0, 1, -4, 1, 0, 0, -1, 0.... 0]$ and the RHS side vector will read $b_{i,j}$.
We are now only left with figuring out which nodes are interior, which ones are boundaries. This is lucky easy to do: first $4$ nodes ($\frac{1}{h} +1$) are boundaries, then you get one boundary, $2$ ($\frac{1}{h} -1$) interior and again a boundary. This is repeated until we finish with 4 boundary nodes.
Thus, we will have: $5-2-2-2-5$ pattern of boundary/interior nodes. We can now write down the set of equations as shown below.
You should note something here: the calculations done on the boundary points are trivial. Furthermore, if $h$ becomes very small you the entries of your matrix will get small and this can lead to issues. We will now try to exclude the boundary points.
As mentioned, we have $(\frac{1}{h}-1)^2 = 4$ interior points. Thus, our matrix will only be $4\times4$. Lets for example write out the 6'th equation of our system. $$\frac{1}{h^2}(u(\frac{1}{3},0) + u(0,\frac{1}{3}) - 4u(\frac{1}{3},\frac{1}{3}) + u(\frac{1}{3}, \frac{2}{3}) + u(\frac{2}{3},\frac{2}{3})) = b(\frac{1}{3},\frac{1}{3})$$
The thing is, since our bc are trivial we actually know the first two terms! They must equal, $\frac{1}{h^2} (g(\frac{1}{3},0) + g(0,\frac{1}{3}))$. So, we set them to the RHS and are left with: $$\frac{1}{h^2}(-4u(\frac{1}{3},\frac{1}{3}) + u(\frac{1}{3}, \frac{2}{3}) + u(\frac{2}{3},\frac{2}{3})) = b(\frac{1}{3},\frac{1}{3})-\frac{1}{h^2} (g(\frac{1}{3},0) + g(0,\frac{1}{3}))$$
We do it with every interior point that has a boundary point as an neighbor. (in this example - that would be every interior point).
We are left with: