Open In Colab

Finite Difference Methods for the Laplacian Equation#

John S Butler john.s.butler@tudublin.ie Course Notes Github#

Overview#

This notebook will focus on numerically approximating a homogenous second order Poisson Equation which is the Laplacian Equation.

The Differential Equation#

The general two dimensional Poisson Equation is of the form:

(648)#2uy2+2ux2=f(x,y),   (x,y)Ω=(0,1)×(0,1),

with boundary conditions

(649)#U(x,y)=g(x,y),   (x,y)δΩ - boundary.

Homogenous Poisson Equation#

This notebook will implement a finite difference scheme to approximate the homogenous form of the Poisson Equation f(x,y)=0:

(650)#2uy2+2ux2=0.

with the Boundary Conditions:

(651)#u(x,0)=sin(2πx),     0x1, lower,
(652)#u(x,1)=sin(2πx),     0x1, upper,
(653)#u(0,y)=2sin(2πy),     0y1, left,
(654)#u(1,y)=2sin(2πy),     0y1, right.
# LIBRARY
# vector manipulation
import numpy as np
# math functions
import math 

# THIS IS FOR PLOTTING
%matplotlib inline
import matplotlib.pyplot as plt # side-stepping mpl backend
import warnings
warnings.filterwarnings("ignore")
from IPython.display import HTML
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

Discete Grid#

The region Ω=(0,1)×(0,1) is discretised into a uniform mesh Ωh. In the x and y directions into N steps giving a stepsize of

(655)#h=10N,

resulting in

(656)#x[i]=0+ih,   i=0,1,...,N,$

and

(657)#x[j]=0+jh,   j=0,1,...,N,

The Figure below shows the discrete grid points for N=10, the known boundary conditions (green), and the unknown values (red) of the Poisson Equation.

N=10
h=1/N
x=np.arange(0,1.0001,h)
y=np.arange(0,1.0001,h)
X, Y = np.meshgrid(x, y)
fig = plt.figure()
plt.plot(x[1],y[1],'ro',label='unknown');
plt.plot(X,Y,'ro');
plt.plot(np.ones(N+1),y,'go',label='Boundary Condition');
plt.plot(np.zeros(N+1),y,'go');
plt.plot(x,np.zeros(N+1),'go');
plt.plot(x, np.ones(N+1),'go');
plt.xlim((-0.1,1.1))
plt.ylim((-0.1,1.1))
plt.xlabel('x')
plt.ylabel('y')
plt.gca().set_aspect('equal', adjustable='box')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.title(r'Discrete Grid $\Omega_h,$ h= %s'%(h),fontsize=24,y=1.08)
plt.show();
../_images/bad082261897d194b9975208fbd8a24db212c0257bb59f4a9c7404c9ee453f22.png

Boundary Conditions#

The discrete boundary conditions are

(658)#wi0=w[i,0]=sin(2πx[i]), for i=0,...,10, upper,
(659)#wiN=w[i,N]=sin(2πx[i]), for i=0,...,10, lower,
(660)#w0j=w[0,j]=2sin(2πy[j]), for j=0,...,10, left,
(661)#wNj=w[N,j]=2sin(2πy[j]), for i=0,...,10, right.

The Figure below plots the boundary values of w[i,j].

w=np.zeros((N+1,N+1))

for i in range (0,N):
        w[i,0]=np.sin(2*np.pi*x[i]) #left Boundary
        w[i,N]=np.sin(2*np.pi*x[i]) #Right Boundary

for j in range (0,N):
        w[0,j]=2*np.sin(2*np.pi*y[j]) #Lower Boundary
        w[N,j]=2*np.sin(2*np.pi*y[j]) #Upper Boundary

        
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot a basic wireframe.
ax.plot_wireframe(X, Y, w,color='r', rstride=10, cstride=10)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('w')
plt.title(r'Boundary Values',fontsize=24,y=1.08)
plt.show()
../_images/4ac2e749c220d5100e341a3bc7563742a9f953f0eb7dbdf4b1f4f0eea83822c4.png

Numerical Method#

The Poisson Equation is discretised using δx2 is the central difference approximation of the second derivative in the x direction

(662)#δx2=1h2(wi+1j2wij+wi1j),

and δy2 is the central difference approximation of the second derivative in the y direction

(663)#δy2=1h2(wij+12wij+wij1).

The gives the Poisson Difference Equation,

(664)#(δx2wij+δy2wij)=fij  (xi,yj)Ωh,
(665)#wij=gij  (xi,yj)Ωh,

where wij is the numerical approximation of U at xi and yj. Expanding the the Poisson Difference Equation gives the five point method,

(666)#(wi1j+wij14wij+wij+1+wi+1j)=h2fij

for i=1,...,N1 and j=1,...,N1 which can be written

(667)#h2wij=fij.

Matrix form#

This can be written as a system of (N1)×(N1) equations can be arranged in matrix form

(668)#Aw=r,

where A is an (N1)2×(N1)2 matrix made up of the following block tridiagonal structure

(669)#(TI00...ITI00............0ITI....0IT),

where I denotes an N1×N1 identity matrix and T is the tridiagonal matrix of the form:

(670)#T=(4100...14100............0141....014).

The plot below shows the matrix A and its inverse A1 as a colourplot.

N2=(N-1)*(N-1)
A=np.zeros((N2,N2))
## Diagonal            
for i in range (0,N-1):
    for j in range (0,N-1):           
        A[i+(N-1)*j,i+(N-1)*j]=-4

# LOWER DIAGONAL        
for i in range (1,N-1):
    for j in range (0,N-1):           
        A[i+(N-1)*j,i+(N-1)*j-1]=1   
# UPPPER DIAGONAL        
for i in range (0,N-2):
    for j in range (0,N-1):           
        A[i+(N-1)*j,i+(N-1)*j+1]=1   

# LOWER IDENTITY MATRIX
for i in range (0,N-1):
    for j in range (1,N-1):           
        A[i+(N-1)*j,i+(N-1)*(j-1)]=1        
        
        
# UPPER IDENTITY MATRIX
for i in range (0,N-1):
    for j in range (0,N-2):           
        A[i+(N-1)*j,i+(N-1)*(j+1)]=1
Ainv=np.linalg.inv(A)   
fig = plt.figure(figsize=(12,4));
plt.subplot(121)
plt.imshow(A,interpolation='none');
clb=plt.colorbar();
clb.set_label('Matrix elements values');
plt.title('Matrix A ',fontsize=24)
plt.subplot(122)
plt.imshow(Ainv,interpolation='none');
clb=plt.colorbar();
clb.set_label('Matrix elements values');
plt.title(r'Matrix $A^{-1}$ ',fontsize=24)

fig.tight_layout()
plt.show();
../_images/2ba6064f5b8c9dda96ccb44e4169e9d195aad2bb829d33290cc74d9d0e4e4f7e.png

The vector w is of length (N1)×(N1) which made up of N1 subvectors wj of length N1 of the form

(671)#wj=(w1jw2j..wN2jwN1j).

The vector r is of length (N1)×(N1) which made up of N1 subvectors of the form rj=h2fjbxjbyj, where bxj is the vector of left and right boundary conditions

(672)#bxj=(w0j0..0wNj),

for j=1,..,N1, where byj is the vector of the lower boundary condition for j=1,

(673)#by1=(w10w20..wN20wN10),

upper boundary condition for j=N1

(674)#byN1=(w1Nw2N..wN2NwN1N),

for j=2,...,N2

(675)#byj=0,

and

(676)#fj=(00..00)

for j=1,...,N1.

r=np.zeros(N2)

# vector r      
for i in range (0,N-1):
    for j in range (0,N-1):           
        r[i+(N-1)*j]=-h*h*0      
# Boundary        
b_bottom_top=np.zeros(N2)
for i in range (0,N-1):
    b_bottom_top[i]=np.sin(2*np.pi*x[i+1]) #Bottom Boundary
    b_bottom_top[i+(N-1)*(N-2)]=np.sin(2*np.pi*x[i+1])# Top Boundary
      
b_left_right=np.zeros(N2)
for j in range (0,N-1):
    b_left_right[(N-1)*j]=2*np.sin(2*np.pi*y[j+1]) # Left Boundary
    b_left_right[N-2+(N-1)*j]=2*np.sin(2*np.pi*y[j+1])# Right Boundary
    
b=b_left_right+b_bottom_top

Results#

To solve the system for w invert the matrix A

(677)#Aw=r,

such that

(678)#w=A1r.

Lastly, as w is in vector it has to be reshaped into grid form to plot.

The figure below shows the numerical approximation of the homogeneous Equation.

C=np.dot(Ainv,r-b)
w[1:N,1:N]=C.reshape((N-1,N-1))

fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111, projection='3d');
# Plot a basic wireframe.
ax.plot_wireframe(X, Y, w,color='r');
ax.set_xlabel('x');
ax.set_ylabel('y');
ax.set_zlabel('w');
plt.title(r'Numerical Approximation of the Poisson Equation',fontsize=24,y=1.08);
plt.show();
../_images/019d1d46a97086f1055e135ff2271c15a41b5fea202911dd39e769229c0b1d99.png

Consistency and Convergence#

We now ask how well the grid function determined by the five point scheme approximates the exact solution of the Poisson problem.

Consistency#

Consitency (Definition)#

Let

(679)#h2(φ)=(φi1j+φij14φij+φij+1+φi+1j)

denote the finite difference approximation associated with the grid Ωh having the mesh size h, to a partial differential operator

(680)#2(φ)=2φx2+2φy2

defined on a simply connected, open set ΩR2. For a given function φC(Ω), the truncation error of h2 is

(681)#τh(x)=(2h2)φ(x)

The approximation h2 is consistent with 2 if

(682)#limh0τh(x)=0,

for all xD and all φC(Ω). The approximation is consistent to order p if τh(x)=O(hp).

In other words a method is consistent with the differential equation it is approximating.

Proof of Consistency#

The five-point difference analog h2 is consistent to order 2 with 2.

Proof

Pick φC(D), and let (x,y)Ω be a point such that (x±h,y),(x,y±h)ΩΩ. By the Taylor Theorem

φ(x±h,y)=φ(x,y)±hφx(x,y)+h22!2φx2(x,y)±h33!3φx3(x,y)+h44!4φx4(ζ±,y)

where ζ±(xh,x+h). Adding this pair of equation together and rearranging , we get

(683)#1h2[φ(x+h,y)2φ(x,y)+φ(xh,y)]2φx2(x,y)=h24![4φx4(ζ+,y)+4φx4(ζ,y)]

By the intermediate value theorem

(684)#[4φx4(ζ+,y)+4φx4(ζ,y)]=24φx4(ζ,y),

for some ζ(xh,x+h). Therefore,

(685)#δx2(x,y)=2φx2(x,y)+h22!4φx4(ζ,y)

Similar reasoning shows that

(686)#δy2(x,y)=2φy2(x,y)+h22!4φy4(x,η)

for some η(yh,y+h). We conclude that τh(x,y)=(h)φ(x,y)=O(h2).

Convergence#

Definition#

Let h2w(xj)=f(xj) be a finite difference approximation, defined on a grid mesh size h, to a PDE 2U(x)=f(x) on a simply connected set DRn. Assume that w(x,y)=U(x,y) at all points (x,y) on the boundary Ω. The finite difference scheme converges (or is convergent) if

(687)#maxj|U(xj)w(xj)|0 as h0.

Theorem (DISCRETE MAXIMUM PRINCIPLE).#

If h2Vij0 for all points (xi,yj)Ωh, then

(688)#max(xi,yj)ΩhVijmax(xi,yj)ΩhVij,

If h2Vij0 for all points (xi,yj)Ωh, then

(689)#min(xi,yj)ΩhVijmin(xi,yj)ΩhVij.

Propositions#

  1. The zero grid function for which Uij=0 for all (xi,yj)ΩhΩh is the only solution to the finite difference problem

(690)#h2Uij=0 for (xi,yj)Ωh,
(691)#Uij=0 for (xi,yj)Ωh.
  1. For prescribed grid functions fij and gij, there exists a unique solution to the problem

(692)#h2Uij=fij for (xi,yj)Ωh,
(693)#Uij=gij for (xi,yj)Ωh.

Definition#

For any grid function V:ΩhΩhR,

(694)#||V||Ω=max(xi,yj)Ωh|Vij|,
(695)#||V||Ω=max(xi,yj)Ωh|Vij|.

Lemma#

If the grid function V:ΩhΩhR satisfies the boundary condition Vij=0 for (xi,yj)Ωh, then

(696)#||V||Ω18||h2V||Ω.

Given these Lemmas and Propositions, we can now prove that the solution to the five point scheme h2 is convergent to the exact solution of the Poisson Equation 2.

Convergence Theorem#

Let U be a solution to the Poisson equation and let w be the grid function that satisfies the discrete analog

(697)#h2wij=fij   for (xi,yj)Ωh,
(698)#wij=gij   for (xi,yj)Ωh.

Then there exists a positive constant K such that

(699)#||Uw||ΩKMh2,

where

(700)#M={||4Ux4||,||4Uy4||}

Proof

The statement of the theorem assumes that UC4(Ω¯). This assumption holds if f and g are smooth enough. \begin{proof} Following from the proof of the Proposition we have

(701)#(h22)Uij=h212[4Ux4(ζi,yj)+4Uy4(xi,ηj)],

for some ζ(xi1,xi+1) and ηj(yj1,yj+1). Therefore,

(702)#h2Uij=fijh212[4Ux4(ζi,yj)+4Uy4(xi,ηj)].

If we subtract from this the identity equation h2wij=fij and note that Uw vanishes on Ωh, we find that

(703)#h2(Uijwij)=h212[4Ux4(ζi,yj)+4Uy4(xi,ηj)].

It follows that

(704)#||Uw||Ω18||h2(Uw)||ΩKMh2.