Using finite difference method for solving an elliptic PDE with MATLAB

In summary, the finite difference method is a numerical technique used for solving differential equations by discretizing a continuous domain into a grid of discrete points and approximating derivatives using finite differences. It works by creating a system of linear equations which can be solved using numerical methods. An elliptic PDE is a type of differential equation that involves a second-order derivative and describes a steady-state or equilibrium situation. MATLAB is commonly used for solving PDEs using the finite difference method due to its built-in functions, user-friendly interface, and efficient algorithms. The finite difference method has advantages such as simplicity, accuracy, versatility, and robustness, making it a popular choice for solving elliptic PDEs in various applications.
  • #1
Elekko
17
0

Homework Statement



Given that we the following elliptic problem on a rectangular region:

[itex]\nabla^2 T=0, \ (x,y)\in \Omega[/itex]

[itex]T(0,y)=300, \ T(4,y)=600, \ 0 \leq y \leq 2[/itex]

[itex]\frac{\partial T}{\partial y}(x,0)=0, \frac{\partial T}{\partial y}(x,2) = 0, \ 0\leq x \leq 4[/itex]

We want to solve this problem using finite difference method with stepsize h = 0.2, M divisions on x-axis and N on y-axis.
My question is how can i find the Neuman BC in "discretized form?
Since we need to exclude the boundaries, how many internal unknown equations will there be inside the region?
Also, exactly how should the implementation be in Matlab code?

Homework Equations



I found the discretizion of the Laplace equation to be (using standard difference formulas for the equation), after simplification:

[itex]-4T_{i,j}+T_{i-1,j}+T_{i+1,j}+T_{i,j-1}+T_{i,j+1}=0[/itex]

For the MATLAB code, I have an example code that solves [itex]\nabla^2 u = 1[/itex] with BC [itex]u=0[/itex] at all boundaries.

Code:
clear all;

N = 100;                    %Number of inner x and y-points
                            
hx=1/(N+1); hy=1/(N+1);     %stepsize in x- and y-direction

x=0:hx:1; y=0:hy:1;         %gridpoints
d=4*ones(N*N,1);            %main diagonal of A
d_1=-ones(N*N,1);
d1=-ones(N*N,1);            %sub and superdiagonals

for i=N:N:N*N-1
    d_1(i)=0
    d1(i+1)=0
end

d_2=-ones(N*N,1);           %subdiagonal from unit matrix
d2=-ones(N*N,1);            %superdiagonal
a = spdiags([d_2 d_1 d d1 d2], [-N, -1:1,N],N*N,N*N);   
                            %sparse matrix A generated
f=ones(N*N,1);              %right hand side
uinner=a\f;                 %solution at inner points
Uinner=zeros(N,N);          
            
for i=1:N                   %inner points as a matrix
    Uinner(i,1:N)=uinner((i-1)*N+[1:N]);
end
U=zeros(N+2,N+2);           %add BCs to inner solution
for i = 1:N
    U(i+1,2:N+1)=Uinner(i,1:N);
end
mesh(x,y,U)                 %3D plot of the solution
title('Solution of \Laplace u = 1 on the unit square')
    xlabel('x')
    ylabel('y')
    zlabel('u(x,y)')

The Attempt at a Solution



Since in the problem we cannot include the boundary points, I calculate, the inner points as [itex](N-1)(M-1)[/itex] which is all the unknown equations to be solved right?

For the Dirichlet BC: i set that:

[itex]T_{0,j}=300, \ j=1,2,3...N[/itex] and
[itex]T_{4,j}=600, \ j=1,2,3...N[/itex]

Now for the Neuman ones i just by analogy from lecture notes write down the conditions, but need to get confirmation if it is correct that:

[itex]\frac{T_{i,0}-T_{i,2}}{2h}=0[/itex] and
[itex]\frac{T_{i,0}-T_{i,2}}{2h}=0[/itex]

Can anyone confirm if neuman conditions are done right?

Also, in the MATLAB code I know that:
Code:
for i=N:N:N*N-1
    d_1(i)=0
    d1(i+1)=0
end

corresponds to the boundary conditions but I do not understand what this for-loop actually does because at the end the -ones is still a vector with N*N rows with -1

It was a long question..
Hope someone can help me.

Thanks
 
Last edited:
Physics news on Phys.org
  • #2
for your post and question! It seems like you have a good understanding of the discretization and how to implement it in MATLAB. To answer your questions:

1. Yes, you are correct that the number of internal unknown equations will be (N-1)(M-1). This is because we are excluding the boundary points, which leaves us with (N+1) points in the x-direction and (M+1) points in the y-direction. Multiplying these together and subtracting the 4 boundary points gives us (N-1)(M-1).

2. Your Neumann boundary conditions look correct. Remember that Neumann boundary conditions represent the flux at the boundary, which is the derivative of the temperature in this case. So setting the derivative to 0 at the boundaries means that there is no heat flow in or out of the system at those points.

3. The for-loop in the MATLAB code is setting the sub and superdiagonals to 0 at the boundary points. This is because we do not need to include these points in our discretization, so we set them to 0. This is just a way to simplify the code and make it more efficient.

Hope this helps clarify things for you! Keep up the good work.
 

1. What is the finite difference method?

The finite difference method is a numerical technique used for solving differential equations. It involves discretizing a continuous domain into a grid of discrete points and approximating the derivatives at each point using finite differences. This method is commonly used in scientific and engineering applications for solving a variety of differential equations.

2. How does the finite difference method work?

The finite difference method works by dividing a continuous domain into a grid of points and approximating the derivatives at each point using finite differences. These approximations are then used to create a system of linear equations, which can be solved using numerical methods such as Gaussian elimination or iterative methods. The solution obtained from this method is an approximation to the exact solution of the differential equation.

3. What is an elliptic PDE?

An elliptic partial differential equation (PDE) is a type of differential equation that involves a second-order derivative with respect to two or more independent variables. It typically describes a steady-state or equilibrium situation, where the solution does not change with respect to time. Examples of elliptic PDEs include the Poisson equation and the Laplace equation.

4. Why is MATLAB commonly used for solving PDEs using the finite difference method?

MATLAB is a popular software tool used for scientific computing and numerical analysis. It provides a wide range of built-in functions and tools for solving differential equations, including the finite difference method. MATLAB also has a user-friendly interface, making it easier for scientists and engineers to implement and visualize their solutions. Additionally, MATLAB has efficient algorithms for solving large systems of linear equations, which are commonly encountered when using the finite difference method.

5. What are the advantages of using the finite difference method for solving elliptic PDEs?

The finite difference method has several advantages for solving elliptic PDEs. Firstly, it is a simple and easy-to-implement method, making it accessible to a wide range of users. It also has good accuracy, especially when using a fine grid. Additionally, the finite difference method is versatile and can be applied to a variety of boundary conditions, making it suitable for solving a wide range of problems. Finally, it is a robust method that can handle irregular or complex geometries, making it a popular choice in many scientific and engineering applications.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
827
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
884
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
956
  • Engineering and Comp Sci Homework Help
Replies
7
Views
737
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
Replies
5
Views
393
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top