## Finite difference Method for 1D Laplace Equation

I will present here how to solve the Laplace equation using finite differences. First let’s look at the one dimensional case:

In this case it is easy to solve explicitly the differential equation and get that . However, sometimes it is not so easy or not possible at all to find an explicit solution, and there are some numerical methods which can be used to approximate our solution. In the one dimensional case, or in the case of a rectangular domain or a box in superior dimensions, the method of finite differences can be sucessfully used.

Pick a step , where is a positive integer. Then we use the approximations and .

We replace our function by a discretization and the derivative becomes . The boundary conditions translate to . Now on the points situated in the interior of we have the relations:

and these translate to a system of equations

where is an matrix

and is a column vector of length of ones. Since is invertible (a recurrence relation can be found for ) the presented system has a unique solution, which will be the desired approximation by finite differences of our function.

We can change the boundary conditions to and by changing and using the formulas

In a similar way we can solve numerically the equation

where is, for example, an arbitrary continuous function.

Below I present a simple Matlab code which solves the initial problem using the finite difference method and a few results obtained with the code.

`function u = laplacefd1(n);`

x=linspace(0,1,n+1);

A=sparse(diag(2*ones(n-1,1))+diag((-1)*ones(n-2,1),1)+diag((-1)*ones(n-2,1),-1));

left=0;

right=0;

f=ones(n-1,1);

f(1)=f(1)+(n)^2*left;

f(n-1)=f(n-1)+(n)^2*right;

u=(n)^2*A\f;

where do these formulas: \displaystyle f_1=1+N^2a,\ f_{N-1}=1+N^2 b come from??

is this based from solving \displaystyle \frac{1}{h^2}(2u_i-u_{i+1}-u_{i-1})=1, for i=1 and i=N ??

how did you get a “+N^2” in each formula?

Yes, the formulas come from the discrete Laplacian formulation. Note that so . That’s why you get an squared in the formulas.

To see more precisely why it is enough to change just write the discrete Laplacian for . You’ll get . Now if you pass the term with to the other side you see that you have the same matrix equation like in the zero boundary condition, but with different .

Hi, in your first equation you have the problem -u”=1 with boundary conditions u(0) = u(1) = 0. You state that the analytical solution to this problem is 0.5*(1-x^2).

Now when I insert the boundary condition into your analytical solution, I get u(0) = 0.5*(1-0^2) = 0.5.

Are you sure the boundary conditions in your problem are correct?

You are right. The solution in my post is, in fact, on the interval [-1,1]. In order for it to work on [0,1] you need to change it a bit. I guess 0.5*x*(x-1) should work instead.