Project 10: Numerical Solutions of the 2D Elliptic Equation

Objective

To numerically solve the 2D Piosson equation using finite difference methods and analyze the results.

Problem Description

The 2D Laplacian equation, often appearing in contexts like heat conduction, electrostatics, and fluid dynamics, is given by:

Δu=f(x,y)

where Δu=2u=2ux2+2uy2, f(x,y) is a given function and when f(x,y)=0, it is the Laplacian equation.

  • Domain: Let’s consider a rectangular domain Ω defined by the coordinates (x,y) where 0x1 and 0y1.

  • Boundary condition: we use Dirichlet boundary condition for this problem

    1. Left Edge (x = 0): Let’s set f1(y) to be a linear function of y. f1(y)=2sin(2πy)

    2. Right Edge (x = 1): For f2(y), let’s use a constant function. f2(y)=2sin(2πy)

    3. Bottom Edge (y = 0): Let g1(x) be a sinusoidal function. g1(x)=sin(2πx)

    4. Top Edge (y = 1): Finally, for g2(x), we can choose a quadratic function. g2(x)=sin(2πx)

Step 1: Discretization and initialization

Discretize the domain Ω into a grid of (N+1)×(N+1) points, where Nx and Ny are the number of grid points in the x and y directions, respectively. The grid spacing in the x and y directions are Δx=1/N and Δy=1/N, respectively. The grid points are indexed by i and j in the x and y directions, respectively, as shown in the figure below.

import numpy as np
N = 10
x = np.linspace(0,1,N+1) # N+1 points in x direction
y = np.linspace(0,1,N+1)
dx = x[1]-x[0]
dy = y[1]-y[0]
Step 3: Boundary Condition Function

Define the boundary condition functions f1(y), f2(y), g1(x), and g2(x).

def f1(y):
    return 
def f2(y):
    return 
def g1(x):
    return 
def g2(x):
    return 
Step 4: Numerical Solution

Use the finite difference method to discretize the 2D Laplacian equation and obtain the numerical solution u(x,y) at x=xi and y=yj for i=1,2,,N1 and j=1,2,,N1.

The Poisson Equation is discretized using δx2 is the central difference approximation of the second derivative in the x direction (1)δx2=1h2(ui+1j2uij+ui1j), and δy2 is the central difference approximation of the second derivative in the y direction (2)δy2=1h2(uij+12uij+uij1). This gives the Poisson Difference Equation, (3)(δx2uij+δy2uij)=fij  (xi,yj)Ωh, (4)uij=gij  (xi,yj)Ωh, where uij is the numerical approximation of U at xi and yj. Expanding the Poisson Difference Equation gives the five-point method, (5)(ui1j+uij14uij+uij+1+ui+1j)=h2fij for i=1,...,N1 and j=1,...,N1 which can be written h2uij=fij

The understanding of this five-point method is important to construct the matrix A and the right-hand side vector b in the linear system (6)Au=b u is the vector of unknowns uij, A is the matrix of coefficients of the unknowns, and b is the right-hand side vector.

This can be written as a system of (N1)×(N1) equations can be arranged in matrix form (7)Au=r,, where A is an (N1)2×(N1)2 matrix made up of the following block tridiagonal structure

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

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

T=(4100...14100............0141....014).
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

After we assemble the matrix A in Eq. (6), we need to construct the right-hand side vector b in Eq. (6). The right-hand side vector b is a vector of length (N1)2, where bi is the right-hand side of the i-th equation in Eq. (6). The right-hand side vector b is constructed by evaluating the function f(x,y) at the grid points (xi,yj) for i=1,2,,N1 and j=1,2,,N1.

# Construct the right-hand side vector b 
b = np.zeros(N2)

Step 5: Plot the Numerical Solution

To solve the system for u invert the matrix A (8)Au=r, such that (9)u=A1r. Lastly, as u is in vector it has to be reshaped into grid form to plot.

C=np.dot(np.linalg.inv(A),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();
Step 6: Analyze the Numerical Solution

We only study a simple boundary case, where f1(y)=f2(y)=g1(x)=g2(x)=0. In this case, the exact solution is u(x,y)=0. We can compute the error between the numerical solution and the exact solution.

error = np.linalg.norm(u)
print('Error = ', error)

Deliverables

Have a jupyter-notebook to show your code and results. Based on the results, prepare a 4-minute presentation to show your results and answer the question.

Reference: Finite Difference Methods for the Poisson Equation