# cryptosystem.org

Let's say you've got a 2D image (or matrix) of the Laplacian of some function, and you want to solve for the function. This is actually relatively easy to do with MATLAB, but searching for a step-by-step example I came up with nothing, so I thought I'd show how to do it here.

Poisson's equation relates the Laplacian () of some function with its result:

So in the discrete Poisson equation we have defined as the non-boundary subset of a 2-D rectangular matrix of dimensions . Here to allow for boundary conditions.

Transforming into natural ordered vectors allows us to rewrite as the linear system:

So, on to the juicy bit, how to express and solve this in MATLAB given you have a 2-D matrix defining (for consistency, we'll use variable names equivalent to what's in these equations).

First, we need to reshape into a vector that skips the boundaries:

b = -reshape(g(2:(m-1),2:(n-1)),(m-2)*(n-2),1);

If we have uniform Dirichlet boundary conditions , should now be correct. If you want other boundary conditions for , you'll need to add them to the appropriate positions in , which I won't cover here.

Thankfully, MATLAB has a nice built-in gallery of matrices which includes the sparse tridiagonal matrix we need for :

A = gallery('poisson',m-2);

We can now solve using the backslash operator:

U = A\b;

This gives us the result in vector form which we can reshape back to what we want:

u = reshape(U,m-2,n-2);

You can pad back your boundary conditions with:

u = padarray(u,[1 1]);

And verify the results with:

laplacian = del2(u)*4;

Which should correspond to (with, of course, border discrepancies). There is, perhaps, some entirely built-in way of doing all this, hidden somewhere in the terse documentation.