How do you build the matrix for finite difference 2D Laplace equation on variable mesh?

1.1k Views Asked by At

Can someone explain how to build the matrix equation using finite difference on a variable mesh to solve the 2D Laplace equation using Dirichlet conditions?

Given the 2D equation $$\frac{\partial^2A}{\partial x^2}+\frac{\partial^2A}{\partial y^2}=0$$

I understand how you can use a central finite difference scheme on a variable mesh to obtain this equation:

$$\frac{2A_{i+1,\;j}}{(x_i-x_{i-1})(x_{i+1}-x_{i-1})}-\frac{2A_{i,\;j}}{(x_{i+1}-x_{i})(x_{i}-x_{i-1})}+\frac{2A_{i-1,\;j}}{(x_{i+1}-x_{i})(x_{i+1}-x_{i-1})}+\frac{2A_{i,\;j+1}}{(y_i-y_{i-1})(y_{i+1}-y_{i-1})}-\frac{2A_{i,\;j}}{(y_{i+1}-y_{i})(y_{i}-y_{i-1})}+\frac{2A_{i,\;j-1}}{(y_{i+1}-y_{i})(y_{i+1}-y_{i-1})}=0.$$

However, I am at a complete loss as to how to code this up into a matrix form which can be solved like $\mathbf{Fa}=\mathbf{b}$. My question here is, how do I build the $\mathbf{F}$ matrix given the above equation? Furthermore, what if the number of cells in the x- and y-domain are not equal (e.g. suppose in the x-domain we have N = 50 cells and in y-domain we have M = 40 cells)?

The mesh is a rectilinear but with variable grid cell sizes. Some cells are larger than others. It is a product grid (i.e. could be produced using a meshgrid command in MATLAB).

I could probably do it using a ton of for loops, but there must be a better and more efficient way. I have also found some sources which discuss the problem when using a uniform mesh but I am not sure how to generalize that to a non-uniform mesh.

Any help is appreciated.

Note: I am coding in MATLAB.